Navigating namespace declarations

  • Thread starter =?iso-8859-1?q?S=E9bastien_Ros?=
  • Start date
?

=?iso-8859-1?q?S=E9bastien_Ros?=

I have to process the xmlns declarations in the root node of a
document.
Unfortunately, they are not recognized as elements nor as attributes.
Even with a /root/node() I can't reach them.

example:
<root xmlns="http://foo1" xmlns:a="http://foo2">
....
</root>

I'm wondering how to iterate through "xmlns" and "xmlns:a".

Regards,
Sébastien
 
R

Richard Tobin

Sébastien Ros said:
I have to process the xmlns declarations in the root node of a
document.
Unfortunately, they are not recognized as elements nor as attributes.
Even with a /root/node() I can't reach them.

I assume you're using XPath. If so, there's no way to do it, because
namespace declarations do not appear in the XPath data model.

There are namespace nodes, which can be accessed with the namespace
axis, but they correspond to the namespaces in scope for an element,
not the ones declared on it. For example, in

<foo><bar xmlns:a="1"><baz/></bar></foo>

both bar and baz have a namespace node with name "a".

-- Richard
 
P

Pavel Lepin

Sébastien Ros said:
I have to process the xmlns declarations in the root node
of a document.
Unfortunately, they are not recognized as elements nor as
attributes. Even with a /root/node() I can't reach them.

And if you think about it, it makes a lot of sense, too.
After all:

<root xmlns="http://example.org/foo">
<foo>
<bar/>
</foo>
</root>

is supposed to be equivalent to:

<foo:root xmlns:foo="http://example.org/foo">
<bar:foo xmlns:bar="http://example.org/foo">
<baz:bar xmlns:baz="http://example.org/foo"/>
</bar:foo>
</foo:root>

In a sense, xmlns declarations are merely syntactic sugar so
that we wouldn't have to write:

<{http://example.org/foo}foo>
<{http://example.org/foo}bar>
<{http://example.org/foo}baz/>
</{http://example.org/foo}bar>
</{http://example.org/foo}foo>

all the time. Because that's what both of the documents
above "really" mean. But, boy, wouldn't *that* be
cumbersome? So we have xmlns declarations, but they're
really just a serialization detail as far as many XML tools
are concerned.
 
A

Alain Ketterlin

Sébastien Ros said:
I have to process the xmlns declarations in the root node of a
document.
Unfortunately, they are not recognized as elements nor as attributes.
Even with a /root/node() I can't reach them.

example:
<root xmlns="http://foo1" xmlns:a="http://foo2">
...
</root>

I'm wondering how to iterate through "xmlns" and "xmlns:a".

XPath has a namespace axis, and XPath data model explicitly provides
"namespace nodes". So:

<xsl:template match="whateverelementtype">
<xsl:for-each select="namespace::node()">
<xsl:variable name="prefix" select="local-name()"/>
<xsl:variable name="uri" select="."/>
...
</xsl:for-each>
</xsl:template>

should work. At least, it works with libxslt's xsltproc. (You even get
an "xml" prefixed namespace with uri
"http://www.w3.org/XML/1998/namespace".)

-- Alain.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top