xpath query to find the root element attribute value - c#

Most of the time i was trying to write xpath query by myself and fails all the time :(. Again i got in to problem to find the value
here is my xml
<Tail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Id="45000061" CreateDateTime="2010-03-08T16:02:52.512+11:00" Type="Actual" Sender="DRNA" Receiver="Lawson">
Am trying to find the "ID" value in my root node (that's suppose to be "45000061")
Any help please.
Thanks in Advance

The xpath expression /Tail/#Id you get you the Id attribute value.

This site has a great free tool for building XPath Expressions (XPath Builder).
http://www.bubasoft.net/
Make your xpath expression work in the tool and then put it in your code.

Related

Selenium - Returning the xpath or CSS path of an element found by LinkText

Is there a possible method or way to can grab the absolute xpath or css path from doing a FindElement by LinkText? Example:
FindElement(By.LinkText("LinkTextThat'sClickable"));
how about getting the node by using ancestors::node() and then working your way with the results?
Hey you can try getting xpath attribute of that specific element Hope this can solve your problem.
FindElement(By.LinkText("LinkTextThat'sClickable")).GetAttribute("xpath");;

Determining XPath

Total XPath noob here and it doesn't help that I have only a basic grounding in HTML/XML (Infrastructure support is my domain). Please could you help me determine a good XPath for the highlighted value (2nd Line Engineer). I managed to it for "description", extracting the text value underneath by using:
//div[#class='description'
but am unable to do so for the mentioned one. Also how does one target the below node in a statement?
"li class="position" data-section="currentPositionDetails"
Some possible solutions:
//li[#data-section='currentPositionsDetails']
//li[#data-section='currentPositionsDetails']//*[#class='item-title']//text()
//li[#data-section='currentPositionsDetails']//*[#class='item-title']//span/text()

Select "src" value with XPath to HtmlAgilityPack

I'm on a development process of a crawling engine. My program crawls websites through Xpath with HtmlAgilityPack. I need to get some image src tag's directly. You can see my simple code below which is not working correctly, thanks in advice!
PS: Please ignore " char problem, XPath patterns are provided by database.
Agility.DocumentNode.SelectSingleNode("//img[#id="product_photo"]/#src");
And this is the line i need to crawl (the *...* part shows block to extract
<img id="product_photo" src="*/images/thumb/4400/10280/st.jpg*">
Some pages provide image in meta tags so .Attributes["src"] wont work.
UPDATE: You can see my query and result here
You cann't get the value of "src" or any other attributes in using:
Agility.DocumentNode.SelectSingleNode(yourXpath);
Just by using:
string s=Agility.DocumentNode.SelectSingleNode(yourXpath).value;
It's because XPath cann't return value of an attribute by SelectSingleNode() func in HtmlAgilityPack class. So you must use SelectSingleNode(yourXpath).value or use Regex after the pharsing to get just the "src" without the outerText.

Using XPATH to access XML elements (was: Good tutorial to learn 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.
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.
I'd go for the classic W3Schools tutorial. That's how I learnt, and it did me fine. Definitely covers all the basics.
MSDN XPath syntax
I found that the tutorials on zvon are quite good.
Here is the XPath tutorial.
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.
http://www.w3schools.com/xsl/xpath_intro.asp
There is a tutorial in the top and also xpath reference.
I'm late to the party, but will post for seekers.
Get XPathbuilder. Should you wish to learn xpath, get yourself this tool, a sample twisted xml file, open the help section in xpath and give it a go.
I am using it right now and i can't believe it took me 5 minutes to select the stuff i need. (nightmare configuration file as xml).
JQuery supports basic XPath expression, you can use it to practice writing XPath selectors.
I find that strongly typed xpath support is lacking in .Net. Here is a library for generating xpath using linq-esq expressions.
http://www.syntaxsuccess.com/viewarticle/how-to-create-xpath-using-linq
Example:
var xpath = CreateXpath.Where(e => e.TargetElementName == "td" &&
e.TargetElementText.Contains("my test"));
xpath generated: //td[contains(text(),'my test')]

Xpath, retrieving node value

I get this return value from Sharepoint... which I have just included the first part of the xml snippet...
<Result ID=\"1,New\" xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">
<ErrorCode>0x00000000</ErrorCode><ID /><z:row ows_ID=\"9\"
It populates a XmlNode node object.
How using xPath can I get the value of ows_id ?
My code so far...
XmlNode results = list.UpdateListItems("MySharePointList", batch);
Update
So far I have this : results.FirstChild.ChildNodes[2].Attributes["ows_ID"].Value
But I am not sure how reliable it is, can anyone improve on it?
I don't know if its necessarily an improvement, but it might be more readable, though more verbose:
/*[local-name() = 'Result']/*[local-name() = 'row']/#ows_ID
There is probably more to the fragment you posted so this XPath query might need a fixup when used against the actual xml result.
The function, local-name(), lets you ignore namespaces, which can be both a boon and a curse. :)
When you start from root:
/Result/z:row/#ows_ID
also you can improve search if exists multiple Result:
/Result[#ID='1,New']/z:row/#ows_ID
<xsl:value-of select="Result/b:row/#ows_ID"/>
or
<xsl:value-of select="Result/b:row[#ows_ID = '9']"/>
Depending on what value you wanted
You probably need to make sure the z namespace prefix is declared correctly - that's implementation dependent. Here's how you do it in Java's XPath implementation.
Then to select the value of the ows_ID attribute, you need to navigate to the element itself, then use #ows_ID to get the value.
The specific xpath calls depend on what library you use (e.g. libxml xpath implementation).
But the generic xpath statement would be:
"//z:row[#ows_ID='9']"
This will select all z:row nodes with an attribute ows_ID of value 9.
You can modify this query to match all z:row nodes or only those with a specific attribute.
For details look here: W3Schools XPath syntax

Categories