Different namespace on attribute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Karine Bosch

    Different namespace on attribute

    Hi,

    My xml document uses different namespaces:
    <x:root xmlns:x="x:ns:m eta/">
    <element1 xmlns:adam="htt p://ns.alfaprint.be/">
    </root>

    This gives problems when using an XPath query.

    I already created 2 namespace managers
    XmlNamespaceMan ager mgr1 = new
    XmlNamespaceMan ager(oXmlDocume nt.NameTable);
    mgr1.AddNamespa ce("x", "x:ns:meta" );
    XmlNamespaceMan ager mgr2 = new XmlNamespaceMan ager(new NameTable());
    mgr2.AddNamespa ce("adam", "http://ns.alfaprint.be/");

    I tried the following 2 things:
    XmlNode oXmlNode =
    oXmlRoot.Select SingleNode("//x:Root[@xmlns:adam='ht tp://ns.alfaprint.be/']",
    mgr1);
    This statement returns oXmlNode to be null;

    XmlNode oXmlNode =
    oXmlRoot.Select SingleNode("//x:Root[@xmlns:adam='ht tp://ns.alfaprint.be/']",
    mgr2);
    This returns an exception telling that the namespace of the root
    element is not declared.

    Does anyone can help me on this?

    Thanks in advance. Kind regards,

    Karine Bosch

  • Martin Honnen

    #2
    Re: Different namespace on attribute



    Karine Bosch wrote:

    [color=blue]
    > My xml document uses different namespaces:
    > <x:root xmlns:x="x:ns:m eta/">
    > <element1 xmlns:adam="htt p://ns.alfaprint.be/">
    > </root>
    >
    > This gives problems when using an XPath query.
    >
    > I already created 2 namespace managers
    > XmlNamespaceMan ager mgr1 = new
    > XmlNamespaceMan ager(oXmlDocume nt.NameTable);
    > mgr1.AddNamespa ce("x", "x:ns:meta" );[/color]

    [color=blue]
    > I tried the following 2 things:
    > XmlNode oXmlNode =
    > oXmlRoot.Select SingleNode("//x:Root[@xmlns:adam='ht tp://ns.alfaprint.be/']",
    > mgr1);[/color]

    Namespace nodes are not attribute nodes in the XPath data model so doing
    @xmlns does not make much sense.
    I don't get what you are looking for with that query, your element above
    is named root in the namespace with URI
    x:ns:meta/
    yet when you add a namespace to the namespace manager you use a
    different URI
    x:ns:meta
    so that way you can't find any elements in the namespace with URI
    x:ns:meta/.
    And element and attribute names are case sensitive, so having root in
    the markup but Root in the XPath expression can't find that element.


    So doing
    mgr1.AddNamespa ce("x", "x:ns:meta/");
    and then
    oXmlRoot.Select SingleNode("/x:root")
    finds you the root element with local name root in the namespace with
    namespace URI x:ns:meta/.

    If you want to find all elements with that name at all levels then you
    need the XPath
    //x:root
    but obviously then you might want to use SelectNodes.

    --

    Martin Honnen --- MVP XML

    Comment

    • Karine Bosch

      #3
      Re: Different namespace on attribute

      ok,

      I'll rephrase my question (or be more specific and more accurate).

      My xml document uses different namespaces:
      <x:root xmlns:x="x:ns:m eta/">
      <element1>a</element1>
      <element1>b</element1>
      ...
      <element1 xmlns:adam="htt p://ns.alfaprint.be/">x</element1>
      <element1>y</element1>
      <element1>z</element1>
      ...
      </root>


      This gives problems when using an XPath query.

      I already created 2 namespace managers
      XmlNamespaceMan ager mgr1 = new
      XmlNamespaceMan ager(oXmlDocume nt.NameTable);
      mgr1.AddNamespa ce("x", "x:ns:meta/");
      XmlNamespaceMan ager mgr2 = new XmlNamespaceMan ager(new NameTable());
      mgr2.AddNamespa ce("adam", "http://ns.alfaprint.be/");


      I tried the following 2 things:
      XmlNode oXmlNode =
      oXmlRoot.Select SingleNode("//x:root[@xmlns:adam='ht tp://ns.alfaprint.be/']",

      mgr1);
      This statement returns oXmlNode to be null;


      XmlNode oXmlNode =
      oXmlRoot.Select SingleNode("//x:root[@xmlns:adam='ht tp://ns.alfaprint.be/']",

      mgr2);
      This returns an exception telling that the namespace of the root
      element is not declared.

      To be clear about some points:
      - I'm not the author of the xml document. I only have to interprete
      some of the nodes. So the structure cannot be changed.
      - As the document can contain hundreds of elements, it's clear that I'm
      looking to extract that one node that is of interest for me by using a
      SelectSingleNod e. Indeed I can loop through the whole document but that
      would mean a performance decrease.

      Does anyone can help me on this, please?

      Thanks in advance. Kind regards,


      Karine Bosch

      Comment

      • Martin Honnen

        #4
        Re: Different namespace on attribute



        Karine Bosch wrote:

        [color=blue]
        > My xml document uses different namespaces:
        > <x:root xmlns:x="x:ns:m eta/">
        > <element1>a</element1>
        > <element1>b</element1>
        > ...
        > <element1 xmlns:adam="htt p://ns.alfaprint.be/">x</element1>
        > <element1>y</element1>
        > <element1>z</element1>
        > ...
        > </root>[/color]

        I see two namespace declared but only one being used as only the x:root
        element is in a namespace, the namespace with URI x:ns:meta/. I don't
        see any element or attribute in the namespace with URI
        http://ns.alfaprint.be/, there is only a declaration binding the prefix
        adam to that namespace URI.

        [color=blue]
        > I tried the following 2 things:
        > XmlNode oXmlNode =
        > oXmlRoot.Select SingleNode("//x:root[@xmlns:adam='ht tp://ns.alfaprint.be/']",[/color]
        [color=blue]
        > XmlNode oXmlNode =
        > oXmlRoot.Select SingleNode("//x:root[@xmlns:adam='ht tp://ns.alfaprint.be/']",
        >
        > mgr2);[/color]

        [color=blue]
        > - As the document can contain hundreds of elements, it's clear that I'm
        > looking to extract that one node that is of interest for me by using a
        > SelectSingleNod e.[/color]

        But which node exactly are you looking for? I have already told you if
        you want the root element in your example then
        xmlDocument.Sel ectSingleNode("/x:root", mgr1)
        will give you the root element.

        It is not clear what
        //x:root[@xmlns:adam='ht tp://ns.alfaprint.be/']
        is supposed to find in your example document.
        //x:root
        makes some sense in general but in your example markup
        /x:root
        would suffice. But use
        //x:root
        if you want/need.
        What the predicate
        @xmlns:adam='ht tp://ns.alfaprint.be/'
        however is supposed to do I don't know, as said namespace declarations
        do not show up as attribute nodes in the XPath data model so writing
        @xmlns:adam where @something in XPath alway selects an attribute does
        not make sense.

        Do an XPath evaluation of e.g.
        //@*
        or
        //*/@*
        on that example XML, it will not find any nodes, those namespace
        declarations are not attributes in terms of XPath.

        If you want to find namespace nodes then you need to use the namespace
        axis.

        So doing e.g.
        //element1[namespace::adam]
        would find you the element with the markup
        <element1 xmlns:adam="htt p://ns.alfaprint.be/">x</element1>
        in your example markup. No namespace manager is needed in .NET to
        evaluate that expression.



        --

        Martin Honnen --- MVP XML

        Comment

        • Karine Bosch

          #5
          Re: Different namespace on attribute

          Hi Martin,

          Thanks for your help. Indeed, I don't need 2 namespace managers. I used
          the syntax you suggested (//element1[namespace::adam]) and it worked!

          Thank you very much!

          Kind regards,

          Karine Bosch

          Comment

          Working...