Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / October 2003

Tip: Looking for answers? Try searching our database.

XPath Expressions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
david_b_mcconnell@hotmail.com - 21 Oct 2003 09:58 GMT
Hi,

I am having a problem using XPath when I validate Xml documents with a schema. Basically what happens is that an XPath query doesn't return the nodes I require if a schema is used to validate the XML. I'll try and explain it as briefly as possible here. For example, look at the following XML.

<?xml version="1.0"?>
<!-- Test Comment -->
<!-- Test Comment 2 -->
<Books>
<Book>
<Title>XML Book One</Title>
<Publisher>Publisher</Publisher>
</Book>
<Book>
<Title>XML Book Two</Title>
<Publisher>Publisher</Publisher>
</Book>
</Books>

When I execute the following code 2 nodes are returned by the XPath query and this output is produced in the debug pane;

Count = 2
XML Book One
XML Book Two

XmlTextReader reader = new XmlTextReader(@"C:\books.xml");
XmlValidatingReader vreader = new XmlValidatingReader(reader);
vreader.ValidationType = ValidationType.Schema;

XPathDocument xmldoc = new System.Xml.XPath.XPathDocument(vreader);
XPathNavigator nav = xmldoc.CreateNavigator();
nav.MoveToRoot();
nav.MoveToFirstChild();

XmlNamespaceManager nsMgr = new XmlNamespaceManager(nav.NameTable);
nsMgr.AddNamespace( "ns" , nav.NamespaceURI );

XPathExpression xPathExpr = nav.Compile("//Publisher[. = 'Publisher']/parent::node()/Title");
xPathExpr.SetContext(nsMgr);
XPathNodeIterator iterator = nav.Evaluate(xPathExpr) as XPathNodeIterator;
System.Diagnostics.Debug.WriteLine("Count = " + iterator.Count.ToString());
while ( iterator.MoveNext() )
System.Diagnostics.Debug.WriteLine(iterator.Current.Value);

However, if i validate the same Xml with a schema as illustrated below;

<?xml version="1.0"?>
<!-- Test Comment -->
<!-- Test Comment 2 -->
<Books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="uri://books Books.xsd" xmlns="uri://books">
<Book>
<Title>XML Book One</Title>
<Publisher>Publisher</Publisher>
</Book>
<Book>
<Title>XML Book Two</Title>
<Publisher>Publisher</Publisher>
</Book>
</Books>

The XML is validated by the schema without error but no nodes are returned by the XPath query. What is going wrong?

Thanks.
Oleg Tkachenko - 21 Oct 2003 13:01 GMT
> However, if i validate the same Xml with a schema as illustrated below;
>
[quoted text clipped - 3 lines]
> <Books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="uri://books Books.xsd" xmlns="uri://books">

This namespace declaration xmlns="uri://books" completely changes
meaning of your XML document. All elements in this XML are in
uri://books namespace, so XPath expression good for non-namespaced XML
doesn't work anymore. Read about default namespaces and XPath.
Signature

Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.