XPathNavigator.Select with a document that has default namespace only

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jiho Han

    XPathNavigator.Select with a document that has default namespace only

    I have an xml document as follows:

    <?xml version="1.0" encoding="utf-8" ?>
    <entityConfigur ation xmlns="http://schemas.infinit yinfo.com/entityconfigura tion"
    version="1.0">
    <entity name="account">
    <attribute name="accountid " />
    </entity>
    </entityConfigura tion>

    I am trying to find all 'entity' elements to iterate and the following are
    the snippets:

    // in static constructor
    XmlDocument doc = new XmlDocument();
    _configExpr = doc.CreateNavig ator().Compile( "/entityConfigura tion/entity");
    XmlNamespaceMan ager ns = new XmlNamespaceMan ager(new NameTable());
    ns.AddNamespace ("", SchemaNamespace );
    _configExpr.Set Context(ns);

    // then in a method (xvr is a XmlValidatingRe ader for the above xml)
    XPathDocument xpath = new XPathDocument(x vr);
    XPathNavigator nav = xpath.CreateNav igator();
    XPathNodeIterat or itr = nav.Select(_con figExpr); // this returns zero nodes.

    Basically, the node set returned by the expression is 0. However, if I do:

    XPathNodeIterat or itr = nav.SelectDesce ndants("entity" , SchemaNamespace ,
    false);

    Then, I will get the correct list of entity element nodes. I could also
    move to entityConfigura tion node and use SelectChildren( "entity", SchemaNamespace )
    with success.
    Since Select is the only one that takes a compiled expression, I would like
    to know why it's not working properly.

    Thanks in advance.

    Jiho Han
    Senior Software Engineer
    Infinity Info Systems
    The Sales Technology Experts
    Tel: 212.563.4400 x216
    Fax: 212.760.0540
    jhan@infinityin fo.com



  • Martin Honnen

    #2
    Re: XPathNavigator. Select with a document that has default namespaceonly



    Jiho Han wrote:
    I have an xml document as follows:
    >
    <?xml version="1.0" encoding="utf-8" ?>
    <entityConfigur ation
    xmlns="http://schemas.infinit yinfo.com/entityconfigura tion" version="1.0">
    <entity name="account">
    <attribute name="accountid " />
    </entity>
    </entityConfigura tion>
    >
    I am trying to find all 'entity' elements to iterate and the following
    are the snippets:
    >
    // in static constructor
    XmlDocument doc = new XmlDocument();
    _configExpr = doc.CreateNavig ator().Compile( "/entityConfigura tion/entity");
    _configExpr =
    doc.CreateNavig ator().Compile( "/pf:entityConfig uration/pf:entity");
    XmlNamespaceMan ager ns = new XmlNamespaceMan ager(new NameTable());
    ns.AddNamespace ("", SchemaNamespace );
    ns.AddNamespace ("pf", SchemaNamespace );

    With XPath 1.0 you always need a prefix bound to a namespace URI to
    select elements in that namespace, even if elements in the XML document
    are in a default namespace and have no prefix. You can choose the prefix
    as you like when you use the XPath API, and no change to the input XML
    is needed

    --

    Martin Honnen --- MVP XML

    Comment

    • Jiho Han

      #3
      Re: XPathNavigator. Select with a document that has default namespace only

      Hello Martin,

      That worked perfectly. Thank you.

      Jiho
      Jiho Han wrote:
      >
      >I have an xml document as follows:
      >>
      ><?xml version="1.0" encoding="utf-8" ?>
      ><entityConfigu ration
      >xmlns="http://schemas.infinit yinfo.com/entityconfigura tion"
      >version="1.0 ">
      ><entity name="account">
      ><attribute name="accountid " />
      ></entity>
      ></entityConfigura tion>
      >I am trying to find all 'entity' elements to iterate and the
      >following are the snippets:
      >>
      >// in static constructor
      >XmlDocument doc = new XmlDocument();
      >_configExpr =
      >doc.CreateNavi gator().Compile ("/entityConfigura tion/entity");
      _configExpr =
      doc.CreateNavig ator().Compile( "/pf:entityConfig uration/pf:entity");
      >
      >XmlNamespaceMa nager ns = new XmlNamespaceMan ager(new NameTable());
      >ns.AddNamespac e("", SchemaNamespace );
      >>
      ns.AddNamespace ("pf", SchemaNamespace );
      >
      With XPath 1.0 you always need a prefix bound to a namespace URI to
      select elements in that namespace, even if elements in the XML
      document are in a default namespace and have no prefix. You can choose
      the prefix as you like when you use the XPath API, and no change to
      the input XML is needed
      >

      Comment

      Working...