Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using XPATH to access XML elements (was: Good tutorial to learn xpath)

Tags:

c#

xml

xpath

I am trying to learn XPath. The theory seems extremely simple, except for the fact that it doesn't work.

I am trying to get the content of every target element

XPathDocument doc = new XPathDocument(sPath);
XPathNavigator nav = doc.CreateNavigator();
XPathExpression expr;
expr = nav.Compile("/doc/file/body/trans-unit/target");
XPathNodeIterator iterator = nav.Select(expr);

while (iterator.MoveNext())
{
    XPathNavigator nav2 = iterator.Current.Clone();
    sbDoc.Append(nav2.InnerXml);
}

The XML doc looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<doc version="1.2">
  <file original="affiliate.php" source-language="EN-US" target-language="FR-FR" datatype="php">
    <header>
      <skl>
        <external-file href="affiliate.php"/>
      </skl>
    </header>
    <body>
      <trans-unit id="tu1">
        <source xml:lang="EN-US">Your Program Details</source>
        <target xml:lang="FR-FR">Your Program Details</target>
      </trans-unit>
      <trans-unit id="tu2">
        <source xml:lang="EN-US">Status</source>
        <target xml:lang="FR-FR">Status</target>
      </trans-unit>

This is nearly word for word from a tutorial, but I can't get it to work. When the iterator is created, in debug mode, I can see that the document is loaded, but iterator finds no result and skips the While loop.

I am probably doing something extremely stupid, but what?

Anyone knows where I can find a good, reliable XPATH tutorial?


Thanks all. Turns out I ignored the fact that there was a namespace (which I removed while simplifying the XML code as I didn't realize it was important), and with the addition of a namespace manager, the code works fine.

I am now studying the XPATH tutorials proposed and they look good.

like image 603
Sylverdrag Avatar asked Aug 06 '10 12:08

Sylverdrag


People also ask

What is the use of XPath in XML?

The XML Path Language (XPath) is used to uniquely identify or address parts of an XML document. An XPath expression can be used to search through an XML document, and extract information from any part of the document, such as an element or attribute (referred to as a node in XML) in it.

What is difference between XPath and XML?

XPath doesn't allow projection of new types. It can only return collections of nodes from the tree, whereas LINQ to XML can execute a query and project an object graph or an XML tree in a new shape. LINQ to XML queries can do much more than XPath expressions.


2 Answers

Maybe the XML is not the one you posted but has a default namespace declaration. That is the main reason why XPath expressions written by beginners don't select what they want to select. You would need an XmlNamespaceManager http://msdn.microsoft.com/en-us/library/6k4x060d.aspx in that case.

like image 180
Martin Honnen Avatar answered Nov 15 '22 15:11

Martin Honnen


I'd go for the classic W3Schools tutorial. That's how I learnt, and it did me fine. Definitely covers all the basics.

like image 25
Skilldrick Avatar answered Nov 15 '22 13:11

Skilldrick