Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Navigating namespace declarations

Reply
Thread Tools

Navigating namespace declarations

 
 
=?iso-8859-1?q?S=E9bastien_Ros?=
Guest
Posts: n/a
 
      05-10-2007
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

 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      05-10-2007
In article <(E-Mail Removed) .com>,
Sébastien Ros <(E-Mail Removed)> wrote:

>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
 
 
 
Pavel Lepin
Guest
Posts: n/a
 
      05-10-2007
SĂ©bastien Ros <(E-Mail Removed)> wrote in
<(E-Mail Removed) .com>:
> 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.

--
Pavel Lepin
 
Reply With Quote
 
Alain Ketterlin
Guest
Posts: n/a
 
      05-10-2007
Sébastien Ros <(E-Mail Removed)> writes:

> 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.
 
Reply With Quote
 
=?iso-8859-1?q?S=E9bastien_Ros?=
Guest
Posts: n/a
 
      05-10-2007
Exactly what I needed, thank you (voisin).

Sébastien

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DOM2 API (Java): how to get namespace declarations? Simon Brooke XML 8 02-11-2006 07:38 PM
Using DOM Library to Add Namespace Declarations to Root Element Tag eric.jester@gmail.com Java 5 04-20-2005 10:30 PM
dropping namespace declarations in output Keith Davies XML 2 08-15-2004 06:20 PM
control over location of namespace declarations in XSLT Christian Hoertnagl XML 1 05-22-2004 10:32 AM
Namespace declarations appearing in output. Mads Orbesen Troest XML 0 04-19-2004 05:53 PM



Advertisments