Navigating namespace declarations

Discussion in 'XML' started by =?iso-8859-1?q?S=E9bastien_Ros?=, May 10, 2007.

  1. 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
     
    =?iso-8859-1?q?S=E9bastien_Ros?=, May 10, 2007
    #1
    1. Advertising

  2. In article <>,
    Sébastien Ros <> 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.
     
    Richard Tobin, May 10, 2007
    #2
    1. Advertising

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

    Pavel Lepin Guest

    Sébastien Ros <> wrote in
    <>:
    > 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
     
    Pavel Lepin, May 10, 2007
    #3
  4. Sébastien Ros <> 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.
     
    Alain Ketterlin, May 10, 2007
    #4
  5. Exactly what I needed, thank you (voisin).

    Sébastien
     
    =?iso-8859-1?q?S=E9bastien_Ros?=, May 10, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    14,543
  2. Mads Orbesen Troest

    Namespace declarations appearing in output.

    Mads Orbesen Troest, Apr 19, 2004, in forum: XML
    Replies:
    0
    Views:
    463
    Mads Orbesen Troest
    Apr 19, 2004
  3. Christian Hoertnagl
    Replies:
    1
    Views:
    389
    Martin Honnen
    May 22, 2004
  4. Keith Davies
    Replies:
    2
    Views:
    346
    Keith Davies
    Aug 15, 2004
  5. Simon Brooke
    Replies:
    8
    Views:
    4,217
    Simon Brooke
    Feb 11, 2006
Loading...

Share This Page