views:

422

answers:

8

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.

+1  A: 

MSDN XPath syntax

Arseny
Tks. Looks very complete.
Sylverdrag
+6  A: 

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

Skilldrick
+1 your too fast ;)
lasseespeholt
Looks like a great tutorial. Thanks!
Sylverdrag
+2  A: 

http://www.w3schools.com/xpath/

There is a tutorial in the top and also xpath reference.

lasseespeholt
+2  A: 

I have always found this tutorial/reference to be very useful... http://zvon.org/comp/r/tut-XPath_1.html#

UPDATE
I used your code and XML document and was able to retrieve the target elements without issue. I did have to close your <body>, <file> and <doc> elements at the end of your XML document, but I'm assuming that's a cut and paste issue. I guess my question would be (and this is overly obvious), are you sure the XML document is getting loaded and that the one that's load actually has the content you're going after? I copied your XML document and hard-coded the path in the XPathDocument constructor, and everything worked fine (meaning, the StringBuilder had the text from the <target> elements).

Also tried both ANSI and UTF-8 encoding -- no issue.

I'm using VS2010 for my development environment.

David Hoerster
+1  A: 

I found that the tutorials on zvon are quite good.

Here is the XPath tutorial.

Oded
A: 

There's nothing wrong with your XPath query. I copied your code nearly exactly (closing the necessary tags in the XML file and changing the StringBuilder.Append for a Console.WriteLine) and it works just fine. Perhaps you're calling the method concurrently and the debugger is jumping between threads?

Richard Poole
A: 

JQuery supports basic XPath expression, you can use it to practice writing XPath selectors.

Satoru.Logic
Ja! Ja! We always see answers to technical questions about javascript (and even about CSS) recommending to use a library like JQuery. But I've never seen it recommended as the tutorial!
Alejandro
@Alejandro :P ..
Satoru.Logic
A: 

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.

Martin Honnen
Thanks Martin. You are right, there was a namespace on the original which I removed when "simplifying" the XML file, and of course, now it works.
Sylverdrag
@Sylverdrag: And how this answer serves your question title **"Good tutorial to learn xpath"**. I think you should edit that.
Alejandro
@Alejandro: It was a 2-in-1 question, and I kind of have to hand it over to the person who manages to spot the underlying problem, although I did upvote the good tutorial answers.
Sylverdrag