HighTechTalks DotNet Forums  

XPathNavigator.Select with a document that has default namespace only

Dotnet XML microsoft.public.dotnet.xml


Discuss XPathNavigator.Select with a document that has default namespace only in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Jiho Han
 
Posts: n/a

Default XPathNavigator.Select with a document that has default namespace only - 08-09-2006 , 03:27 PM






I have an xml document as follows:

<?xml version="1.0" encoding="utf-8" ?>
<entityConfiguration xmlns="http://schemas.infinityinfo.com/entityconfiguration"
version="1.0">
<entity name="account">
<attribute name="accountid" />
</entity>
</entityConfiguration>

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.CreateNavigator().Compile("/entityConfiguration/entity");
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("", SchemaNamespace);
_configExpr.SetContext(ns);

// then in a method (xvr is a XmlValidatingReader for the above xml)
XPathDocument xpath = new XPathDocument(xvr);
XPathNavigator nav = xpath.CreateNavigator();
XPathNodeIterator itr = nav.Select(_configExpr); // this returns zero nodes.

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

XPathNodeIterator itr = nav.SelectDescendants("entity", SchemaNamespace,
false);

Then, I will get the correct list of entity element nodes. I could also
move to entityConfiguration 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 (AT) infinityinfo (DOT) com
www.infinityinfo.com



Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: XPathNavigator.Select with a document that has default namespaceonly - 08-10-2006 , 08:29 AM








Jiho Han wrote:

Quote:
I have an xml document as follows:

?xml version="1.0" encoding="utf-8" ?
entityConfiguration
xmlns="http://schemas.infinityinfo.com/entityconfiguration" version="1.0"
entity name="account"
attribute name="accountid" /
/entity
/entityConfiguration

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.CreateNavigator().Compile("/entityConfiguration/entity");
_configExpr =
doc.CreateNavigator().Compile("/pf:entityConfiguration/pf:entity");

Quote:
XmlNamespaceManager ns = new XmlNamespaceManager(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
http://JavaScript.FAQTs.com/


Reply With Quote
  #3  
Old   
Jiho Han
 
Posts: n/a

Default Re: XPathNavigator.Select with a document that has default namespace only - 08-11-2006 , 11:06 AM



Hello Martin,

That worked perfectly. Thank you.

Jiho

Quote:
Jiho Han wrote:

I have an xml document as follows:

?xml version="1.0" encoding="utf-8" ?
entityConfiguration
xmlns="http://schemas.infinityinfo.com/entityconfiguration"
version="1.0"
entity name="account"
attribute name="accountid" /
/entity
/entityConfiguration
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.CreateNavigator().Compile("/entityConfiguration/entity");
_configExpr =
doc.CreateNavigator().Compile("/pf:entityConfiguration/pf:entity");

XmlNamespaceManager ns = new XmlNamespaceManager(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




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.