Include Elements from other namespace in own XML

  • Thread starter Benjamin Kalytta
  • Start date
B

Benjamin Kalytta

Hello,

I'm wondering if it is possible to include elements from other
namespaces in own xml files.

My Schema file looks like:

[...]
<xs:element name="string">
<xs:complexType mixed="true">
<xs:sequence minOccurs="0">
<xs:any namespace="http://www.w3.org/1999/xhtml"
processContents="strict" />
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>

Corresponding xml could be:

[...]
<string id="xyz">This is a <html:strong>test</html:strong></string>

You see, what I want is first to allow such xml tags within xml file and
second transform it into corresponding <strong>.

If I use some XML DOM API like:

element = document.getElementbyId("xyz");

element should not contain of one textnode "This is a test" only, but it
should contain the text node "This is a", and one child element
"strong". Is this possible?

The document if loaded and validated would look like:

<string name="xyz">
This is a
<strong>
test
</strong>
</string>

Any Idea?

regards

Benjamin Kalytta
 
M

Martin Honnen

Benjamin said:
I'm wondering if it is possible to include elements from other
namespaces in own xml files.

Yes, that is certainly possible.
My Schema file looks like:

[...]
<xs:element name="string">
<xs:complexType mixed="true">
<xs:sequence minOccurs="0">
<xs:any namespace="http://www.w3.org/1999/xhtml"
processContents="strict" />

If you use processContents="strict" then you need to provide/use a
schema for the XHTML namespace http://www.w3.org/1999/xhtml. If you use
processContents="lax" then the parser can validate if a schema is available.
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>

Corresponding xml could be:

[...]
<string id="xyz">This is a <html:strong>test</html:strong></string>

You use an attribute named 'id' but your schema defines one named 'name'.
You see, what I want is first to allow such xml tags within xml file and
second transform it into corresponding <strong>.

If I use some XML DOM API like:

element = document.getElementbyId("xyz");

For document.getElementById to work for XML documents the attribute used
needs to be defined as type xs:ID.
 
B

Benjamin Kalytta

If you use processContents="strict" then you need to provide/use a
schema for the XHTML namespace http://www.w3.org/1999/xhtml. If you
use processContents="lax" then the parser can validate if a schema is
available.
So what does it mean? Is it possible to create a DOM Tree like I
described said:
You use an attribute named 'id' but your schema defines one named 'name'.
Yes sorry, it was just an example.
For document.getElementById to work for XML documents the attribute
used needs to be defined as type xs:ID.
Thanks, I know, forgot to change this.

Benjamin Kalytta
 
M

Martin Honnen

Benjamin said:
So what does it mean? Is it possible to create a DOM Tree like I
described, i.e. which contain <strong> as child of <string> for example?

Yes, of course. The DOM tree does not depend in any way on the existance
of a schema. As long as your XML is well-formed XML with namespaces the
parser can deal with it.
 
B

Benjamin Kalytta

Martin said:
Yes, of course. The DOM tree does not depend in any way on the
existance of a schema. As long as your XML is well-formed XML with
namespaces the parser can deal with it.
Ok, nice to hear that. I ask because my parser (PHP 5 DOM XML) wasn't
able to do that. Either the parser ignored inner <html:> tags completely
or stopped with the error.

*Warning*: DOMDocument::load() [function.DOMDocument-load
<http://smartftp/generator/function.DOMDocument-load>]: Namespace prefix
html on a is not defined in file.xml, line: 367 in xyz.php

Benjamin Kalytta
 
B

Benjamin Kalytta

Sorry this is the real warning:

*Warning*: DOMDocument::schemaValidate()
[function.DOMDocument-schemaValidate
<function.DOMDocument-schemaValidate>]: Element
'{http://www.w3.org/1999/xhtml}a': No matching global element
declaration available, but demanded by the strict wildcard.

What does this warning mean?

Benjamin Kalytta
 
M

Martin Honnen

Benjamin said:
Sorry this is the real warning:

*Warning*: DOMDocument::schemaValidate()
[function.DOMDocument-schemaValidate
<function.DOMDocument-schemaValidate>]: Element
'{http://www.w3.org/1999/xhtml}a': No matching global element
declaration available, but demanded by the strict wildcard.

What does this warning mean?

So you are trying to use schema validation. Your schema demanded
<xs:any namespace="http://www.w3.org/1999/xhtml"
processContents="strict" />
meaning you allow any element in the namespace
http://www.w3.org/1999/xhtml but require a schema for those elements. As
I said in my initial response, consider to use
processContents="lax"
as that way the parser will look for a schema but will continue parsing
if it does not find one.
If you want to use
processContents="strict"
then you need to supply a schema for the XHTML elements.
 
B

Benjamin Kalytta

Yes, first I tried to use "lax", but the parser just ignored the inner
xhtml like tags. Ignore means the tags was just completely removed. Only
textNode was added. It shouldn't behavior like that isn't it?
If you want to use processContents="strict"
then you need to supply a schema for the XHTML elements.
Are you talking about following:

*<xsd:import namespace="*http://www.w3.org/1999/xhtml*" schemaLocation="http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd"/>*

I don't get any error, but here was the same behavior.

<stringtable version="1.0" language="de"
xmlns:stringtable="http://www.domain.com/ns"
xmlns:html="http://www.w3.org/1999/xhtml">
....
<string id="someid">Text <html:a href="#">Link</html:a></string>

The parser generated just removed the tags:

<string id="someid">Text Link</string>

I then tried to add following in my stringtable root element:

<stringtable .... xsi:schemaLocation="http://www.w3.org/1999/xhtml
http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd" ....>

I assume the PHP/XML DOM parser is broken?

Benjamin Kalytta
Benjamin said:
Sorry this is the real warning:

*Warning*: DOMDocument::schemaValidate()
[function.DOMDocument-schemaValidate
<function.DOMDocument-schemaValidate>]: Element
'{http://www.w3.org/1999/xhtml}a': No matching global element
declaration available, but demanded by the strict wildcard.

What does this warning mean?

So you are trying to use schema validation. Your schema demanded
<xs:any namespace="http://www.w3.org/1999/xhtml"
processContents="strict" />
meaning you allow any element in the namespace
http://www.w3.org/1999/xhtml but require a schema for those elements.
As I said in my initial response, consider to use
processContents="lax"
as that way the parser will look for a schema but will continue
parsing if it does not find one.
If you want to use
processContents="strict"
then you need to supply a schema for the XHTML elements.
 
P

Pavel Lepin

Please don't top-post.

Benjamin Kalytta said:
Yes, first I tried to use "lax", but the parser just
ignored the inner xhtml like tags. Ignore means the tags
was just completely removed. Only textNode was added. It
shouldn't behavior like that isn't it?

How do you know? Unless you provide a minimal, complete,
runnable example there's no way to tell precisely what your
problem is, but this sure sounds like your problem lies
elsewhere. PHP 5 DOM works just fine.

pavel@debian:~/dev/php/xml$ xmllint test.xml
<?xml version="1.0"?>
<stringtable xmlns:stringtable="http://www.domain.com/ns"
xmlns:html="http://www.w3.org/1999/xhtml" version="1.0"
language="de">
<string id="someid">Text <html:a
href="#">Link</html:a></string>
</stringtable>
pavel@debian:~/dev/php/xml$ cat prs.php
#!/usr/bin/php
<?php
$xml = new DOMDocument ( ) ;
$xml -> load ( 'test.xml' ) ;
$str = $xml -> saveXML ( ) ;
print ( $str ) ;
?>
pavel@debian:~/dev/php/xml$ ./prs.php
<?xml version="1.0"?>
<stringtable xmlns:stringtable="http://www.domain.com/ns"
xmlns:html="http://www.w3.org/1999/xhtml" version="1.0"
language="de">
<string id="someid">Text <html:a
href="#">Link</html:a></string>
</stringtable>
pavel@debian:~/dev/php/xml$ php -v
PHP 5.2.0-10 (cli) (built: Mar 7 2007 22:42:14)
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2006 Zend
Technologies
pavel@debian:~/dev/php/xml$
 
B

Benjamin Kalytta

Please don't top-post.
Anybody can make a mistake.
[...] PHP 5 DOM works just fine.
That's true. I made a stupid mistake. I always tried to get nodeValue of
<string> which of course always get entire text content including those
of all child elements. saveXML() now will output correct tree like
expected. (Text <html:a href="#">Link</html:a>)

But is it possible to remove each namespace prefix from XML DOM Tree so
that only <a ...> left? I think there is no XML conform way to do this.

Benjamin Kalytta
 
J

Joe Kesselman

Benjamin said:
But is it possible to remove each namespace prefix from XML DOM Tree so
that only <a ...> left? I think there is no XML conform way to do this.

Removing namespace prefixes and declarations may change the meaning of
the document. It can sometimes be done, by switching to using the
default namespace declaration -- but not always; prefixes are sometimes
used in the document's content as well.

Thus, if you want to do this, it has to be done by application code
which _understands_ the document's meaning and knows what changes are or
aren't appropriate. There's no standard solution; you have to write your
own.
 
P

Pavel Lepin

Benjamin Kalytta said:
[...] PHP 5 DOM works just fine.
That's true. I made a stupid mistake. I always tried to
get nodeValue of <string> which of course always get
entire text content including those of all child elements.
saveXML() now will output correct tree like expected.
(Text <html:a href="#">Link</html:a>)

But is it possible to remove each namespace prefix from
XML DOM Tree so that only a ... left? I think there is
no XML conform way to do this.

If you mean 'is there a magic switch that would make that
happen upon parsing', then no, there isn't any that I'm
aware of (I suppose that's because getting rid of
namespaces is not a particularly good idea). However, this
is trivial to do using DOM tree traversal or XSLT
stylesheet.

But as I said, I'm not sure this is a good idea at all. Why
would you want to get rid of namespaces? If you don't want
namespaces, why does your document contain them in first
place?
 
B

Benjamin Kalytta

But as I said, I'm not sure this is a good idea at all. Why
would you want to get rid of namespaces? If you don't want
namespaces, why does your document contain them in first
place?
A simple answer: I want to allow (x)html to be used in my document (in
my strings). However the application (browser) don't know what to do
with <html:a> for example.

Benjamin Kalytta
 
J

Joseph Kesselman

Benjamin said:
However the application (browser) don't know what to do
with <html:a> for example.

If you're going to use outdated applications... I'd suggest that rather
than prefixes, you try using the default-namespace convention. That is,
instead of <html:a>, use <a xmlns="http://www.w3.org/1999/xhtml/">.

If the application is really namespace-unaware, that should look like an
HTML element to it.

Note that since namespace declarations are inherited, you won't
necessarily have to re-bind xmlns on every element. A smart serializer
should do the Right Things in that regard.


However, my position is that outdated applications should be discarded
in favor of ones that support new standards -- so I'd recommend that you
solve this instead by making the whole document either HTML or XHTML
rather than mixing the two, and then use a browser (or other tool) that
understands the language you're actually working in.
 
P

Pavel Lepin

<[email protected]>:

[getting rid of namespaces for XHTML elements]
A simple answer: I want to allow (x)html to be used in my
document (in my strings). However the application
(browser) don't know what to do with <html:a> for example.

Simply getting rid of namespaces is not enough for that. You
might want to take a look at XSLT, since an XSLT stylesheet
is one of the easiest ways to convert XHTML to HTML.
Searching ctx on GG might help as well, this is one of the
most frequently asked questions on this newsgroup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top