XmlDocument.LoadXml and namespaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Thielen

    #1

    XmlDocument.LoadXml and namespaces

    Hi;

    Once I have loaded an xml file into a DOM, is there any way to then get any
    namespace declarations that were in the xml file? Or are those just discarded
    as the DOM is populated?

    And if you can't get it from the DOM, any other way to get the namespace(s)?
    I'm assuming doing a SAX read which returns each element including NameSpaces
    and pull it form that (and build up the DOM at the same time)?

    In my case I get a Stream so I can't read it twice as the Stream may not
    support reset. And it may have NS elements - but I don't know if there are
    any or what they are except by reading the Stream.

    --
    thanks - dave
  • Oleg Tkachenko [MVP]

    #2
    Re: XmlDocument.Loa dXml and namespaces

    David Thielen wrote:
    [color=blue]
    > Once I have loaded an xml file into a DOM, is there any way to then get any
    > namespace declarations that were in the xml file? Or are those just discarded
    > as the DOM is populated?[/color]

    Of course not. You can select namespace declarations using
    doc.SelectNodes ("//namespace::*").

    --
    Oleg Tkachenko [XML MVP, MCAD]


    Comment

    • David Thielen

      #3
      Re: XmlDocument.Loa dXml and namespaces

      Hi;

      This sort-of worked. I have 3 namespaces declared in the header of the file.
      The SelectNodes("//namespace::*") gave me 36 nodes. I did not check each one
      but it appeared to be the 3 namespaces repeated over and over.

      Any idea why I didn't get just 3?

      --
      thanks - dave


      "Oleg Tkachenko [MVP]" wrote:
      [color=blue]
      > David Thielen wrote:
      >[color=green]
      > > Once I have loaded an xml file into a DOM, is there any way to then get any
      > > namespace declarations that were in the xml file? Or are those just discarded
      > > as the DOM is populated?[/color]
      >
      > Of course not. You can select namespace declarations using
      > doc.SelectNodes ("//namespace::*").
      >
      > --
      > Oleg Tkachenko [XML MVP, MCAD]
      > http://www.xmllab.net
      > http://blog.tkachenko.com
      >[/color]

      Comment

      • David Thielen

        #4
        Re: XmlDocument.Loa dXml and namespaces

        For the xml:
        <root xmlns="http://www.test.org" xmlns:sns="http ://www.test.org/sub"
        xmlns:mns="http ://www.test.org/mini">
        <data>
        <items>
        <item id="1">
        <sns:subItem sid="11">dave</sns:subItem>
        <sns:subItem sid="12">thiele n</sns:subItem>
        </item>
        <item id="2">
        <sns:subItem sid="21">shirle y</sns:subItem>
        </item>
        </items>
        <mns:more>dav e thielen</mns:more>
        </data>
        </root>

        The nodes returned are:
        mns=http://www.test.org/mini
        sns=http://www.test.org/sub
        {default}=http://www.test.org
        xml=http://www.w3.org/XML/1998/namespace
        mns=http://www.test.org/mini
        sns=http://www.test.org/sub
        {default}=http://www.test.org
        xml=http://www.w3.org/XML/1998/namespace
        mns=http://www.test.org/mini
        sns=http://www.test.org/sub
        {default}=http://www.test.org
        xml=http://www.w3.org/XML/1998/namespace
        mns=http://www.test.org/mini
        sns=http://www.test.org/sub
        {default}=http://www.test.org
        xml=http://www.w3.org/XML/1998/namespace
        mns=http://www.test.org/mini
        sns=http://www.test.org/sub
        {default}=http://www.test.org
        xml=http://www.w3.org/XML/1998/namespace
        mns=http://www.test.org/mini
        sns=http://www.test.org/sub
        {default}=http://www.test.org
        xml=http://www.w3.org/XML/1998/namespace
        mns=http://www.test.org/mini
        sns=http://www.test.org/sub
        {default}=http://www.test.org
        xml=http://www.w3.org/XML/1998/namespace
        mns=http://www.test.org/mini
        sns=http://www.test.org/sub
        {default}=http://www.test.org
        xml=http://www.w3.org/XML/1998/namespace
        mns=http://www.test.org/mini
        sns=http://www.test.org/sub
        {default}=http://www.test.org
        xml=http://www.w3.org/XML/1998/namespace

        ??? - thanks - dave

        Comment

        • Martin Honnen

          #5
          Re: XmlDocument.Loa dXml and namespaces



          David Thielen wrote:
          [color=blue]
          > This sort-of worked. I have 3 namespaces declared in the header of the file.
          > The SelectNodes("//namespace::*") gave me 36 nodes. I did not check each one
          > but it appeared to be the 3 namespaces repeated over and over.
          >
          > Any idea why I didn't get just 3?[/color]

          Because in the XPath data model any element has in scope namespace nodes
          and that way if you for instance declare namespaces on the root element
          and do not declare/redeclare namespaces on any child elements then
          besides the root element all child elements also have those namespaces
          in scope as nodes of their own.

          --

          Martin Honnen --- MVP XML
          http://JavaScript.FAQTs.com/

          Comment

          • Oleg Tkachenko [MVP]

            #6
            Re: XmlDocument.Loa dXml and namespaces

            David Thielen wrote:
            [color=blue]
            > This sort-of worked. I have 3 namespaces declared in the header of the file.
            > The SelectNodes("//namespace::*") gave me 36 nodes. I did not check each one
            > but it appeared to be the 3 namespaces repeated over and over.
            >
            > Any idea why I didn't get just 3?[/color]

            Select unique ones (and omit default "xml" namespace):
            //namespace::*[name() != 'xml'][not(../../namespace::*=.)]

            --
            Oleg Tkachenko [XML MVP, MCAD]


            Comment

            • David Thielen

              #7
              Re: XmlDocument.Loa dXml and namespaces

              Oh right, that makes a lot of sense -- thanks - dave


              "Martin Honnen" wrote:
              [color=blue]
              >
              >
              > David Thielen wrote:
              >[color=green]
              > > This sort-of worked. I have 3 namespaces declared in the header of the file.
              > > The SelectNodes("//namespace::*") gave me 36 nodes. I did not check each one
              > > but it appeared to be the 3 namespaces repeated over and over.
              > >
              > > Any idea why I didn't get just 3?[/color]
              >
              > Because in the XPath data model any element has in scope namespace nodes
              > and that way if you for instance declare namespaces on the root element
              > and do not declare/redeclare namespaces on any child elements then
              > besides the root element all child elements also have those namespaces
              > in scope as nodes of their own.
              >
              > --
              >
              > Martin Honnen --- MVP XML
              > http://JavaScript.FAQTs.com/
              >[/color]

              Comment

              • David Thielen

                #8
                Re: XmlDocument.Loa dXml and namespaces

                Hi;

                This worked great but I don't understand it totally. I get the [name() !=
                'xml']

                But what does [not(../../namespace::*=.)] mean? I'm totally lost on how that
                works.

                Also, when you do xpath[a][b] is that saying both a and b must be true? Can
                you also do //namespace::*[name() != 'xml'] and [not(../../namespace::*=.)]
                as the same thing? Or am I not understanding this?

                I'm asking the above questions because I just want to understand xpath better.

                --
                thanks - dave


                "Oleg Tkachenko [MVP]" wrote:
                [color=blue]
                > David Thielen wrote:
                >[color=green]
                > > This sort-of worked. I have 3 namespaces declared in the header of the file.
                > > The SelectNodes("//namespace::*") gave me 36 nodes. I did not check each one
                > > but it appeared to be the 3 namespaces repeated over and over.
                > >
                > > Any idea why I didn't get just 3?[/color]
                >
                > Select unique ones (and omit default "xml" namespace):
                > //namespace::*[name() != 'xml'][not(../../namespace::*=.)]
                >
                > --
                > Oleg Tkachenko [XML MVP, MCAD]
                > http://www.xmllab.net
                > http://blog.tkachenko.com
                >[/color]

                Comment

                • Oleg Tkachenko [MVP]

                  #9
                  Re: XmlDocument.Loa dXml and namespaces

                  David Thielen wrote:

                  [color=blue]
                  > But what does [not(../../namespace::*=.)] mean? I'm totally lost on how that
                  > works.[/color]

                  In XPath data model namespace nodes are propagated/copied down the tree
                  - each node in a subtree has namespace node for each namespace in context.
                  This predicate checks that parent of the element bearing the namespace
                  node doesn't have such namespace node. Effectively that allows to select
                  only namespace nodes on elements where namespace is declared.
                  [color=blue]
                  >
                  > Also, when you do xpath[a][b] is that saying both a and b must be true?[/color]

                  Yes.
                  [color=blue]
                  > Can
                  > you also do //namespace::*[name() != 'xml'] and [not(../../namespace::*=.)]
                  > as the same thing? Or am I not understanding this?[/color]

                  Yes, that's the same.

                  --
                  Oleg Tkachenko [XML MVP, MCAD]


                  Comment

                  • David Thielen

                    #10
                    Re: XmlDocument.Loa dXml and namespaces

                    Hi;

                    Ok, that makes sense. To make sure I understand this:
                    .../../namespace::* is getting the grandparent of the node on (../../) and
                    then getting any nodes in that element that start with namespace:: - why not
                    .../namespace::* - isn't that the parent?

                    And then the =. is saying equal to the node presently on . being the node on.

                    Is that correct?

                    --
                    thanks - dave


                    "Oleg Tkachenko [MVP]" wrote:
                    [color=blue]
                    > David Thielen wrote:
                    >
                    >[color=green]
                    > > But what does [not(../../namespace::*=.)] mean? I'm totally lost on how that
                    > > works.[/color]
                    >
                    > In XPath data model namespace nodes are propagated/copied down the tree
                    > - each node in a subtree has namespace node for each namespace in context.
                    > This predicate checks that parent of the element bearing the namespace
                    > node doesn't have such namespace node. Effectively that allows to select
                    > only namespace nodes on elements where namespace is declared.
                    >[color=green]
                    > >
                    > > Also, when you do xpath[a][b] is that saying both a and b must be true?[/color]
                    >
                    > Yes.
                    >[color=green]
                    > > Can
                    > > you also do //namespace::*[name() != 'xml'] and [not(../../namespace::*=.)]
                    > > as the same thing? Or am I not understanding this?[/color]
                    >
                    > Yes, that's the same.
                    >
                    > --
                    > Oleg Tkachenko [XML MVP, MCAD]
                    > http://www.xmllab.net
                    > http://blog.tkachenko.com
                    >[/color]

                    Comment

                    • Martin Honnen

                      #11
                      Re: XmlDocument.Loa dXml and namespaces



                      David Thielen wrote:

                      [color=blue]
                      > Ok, that makes sense. To make sure I understand this:
                      > ../../namespace::* is getting the grandparent of the node on (../../) and
                      > then getting any nodes in that element that start with namespace:: - why not
                      > ../namespace::* - isn't that the parent?[/color]

                      It helps looking into the XPath spec which here
                      <http://www.w3.org/TR/xpath#namespace-nodes>
                      says about the namespace nodes:
                      "Each element has an associated set of namespace nodes, one for each
                      distinct namespace prefix that is in scope for the element (including
                      the xml prefix, which is implicitly declared by the XML Namespaces
                      Recommendation [XML Names]) and one for the default namespace if one is
                      in scope for the element. The element is the parent of each of these
                      namespace nodes"
                      thus if you have a predicate on
                      //namespace::*
                      then
                      ..
                      gives you the element the namespace node belongs to and
                      ../..
                      gives the parent node of that element so that the predicate
                      not(../../namespace::*=.)
                      compares the namespace node on one element with the namespace nodes
                      attached to the parent element.




                      --

                      Martin Honnen --- MVP XML
                      http://JavaScript.FAQTs.com/

                      Comment

                      • David Thielen

                        #12
                        Re: XmlDocument.Loa dXml and namespaces

                        Hi;

                        I think I've read the spec at least 5 times. And I have 4 books on XML/XPath
                        that I have gone through. But I still trip on stuff - and figure it's best to
                        ask and ask until I know I understand a point well. I appreciate everyone
                        explaining the why behind this.

                        As soon as you said the .. is the node containing the namespace I realized
                        why it's ../.. instead of .. - should have figured that out.

                        One thing that hit me while going through this, it seems that xpath
                        basically walks the tree node by node to determine if that node matches the
                        xpath. If it does, it adds that node to the node(s) to be returned and walks
                        on to the next one. (Obviously in implementation it does not walk nodes it
                        knows can't match.)

                        So it makes sense that you can use . in the xpath to represent the node you
                        are presently evaluating. But I hadn't really comprehended that before as a
                        SQL select has no similiar concept.

                        Anyways, thanks for the lesson - I think I understand this now and this is
                        definitely useful info.

                        --
                        thanks - dave


                        "Martin Honnen" wrote:
                        [color=blue]
                        >
                        >
                        > David Thielen wrote:
                        >
                        >[color=green]
                        > > Ok, that makes sense. To make sure I understand this:
                        > > ../../namespace::* is getting the grandparent of the node on (../../) and
                        > > then getting any nodes in that element that start with namespace:: - why not
                        > > ../namespace::* - isn't that the parent?[/color]
                        >
                        > It helps looking into the XPath spec which here
                        > <http://www.w3.org/TR/xpath#namespace-nodes>
                        > says about the namespace nodes:
                        > "Each element has an associated set of namespace nodes, one for each
                        > distinct namespace prefix that is in scope for the element (including
                        > the xml prefix, which is implicitly declared by the XML Namespaces
                        > Recommendation [XML Names]) and one for the default namespace if one is
                        > in scope for the element. The element is the parent of each of these
                        > namespace nodes"
                        > thus if you have a predicate on
                        > //namespace::*
                        > then
                        > ..
                        > gives you the element the namespace node belongs to and
                        > ../..
                        > gives the parent node of that element so that the predicate
                        > not(../../namespace::*=.)
                        > compares the namespace node on one element with the namespace nodes
                        > attached to the parent element.
                        >
                        >
                        >
                        >
                        > --
                        >
                        > Martin Honnen --- MVP XML
                        > http://JavaScript.FAQTs.com/
                        >[/color]

                        Comment

                        Working...