Selecting updated timestamp using XPath - xpath

XPath noob question:
I have an HTML file that contains
<p class="postinginfo">Updated: <time datetime="2013-11-21T12:39:50-0600">2013-11-21, 12:39PM CST</time></p>
<p class="postinginfo">Posted: <time datetime="2013-11-20T12:39:50-0600">2013-11-20, 12:39PM CST</time></p>
I'm trying to extract the time using C# and HtmlAgilityPack
var htmldocs = doc.DocumentNode.SelectNodes("//*[#class=\"postinginfo\"]Updated:/text()");
This did not work. Reading through the XPath tutorial on w3schools.com it wasn't obvious how the functions should be used. I'd appreciate some guidance from a more experienced XPath user.

I'd suggest you install firefox+firebug+firepath to test xpath expression.
Try this xpath:
//p[#class='postinginfo' and contains(., 'Updated:')]/time/#datetime

Related

It is possible to use Xpath function in order to read the date value

Since i am new to Xpath i want to ask if it is possible to use Xpath function in order to read the date value in the following
<div id="qa-case">
<time itemprop="datePublished" datetime="2015-01-12T02:41:00Z"></time>
</div>
What I want is the value in the datetime. Is that possible to read it using something like this
//*[#id="qa-case"]fn:string(datetime)
What i expect is to have an text showing me 2015-01-12T02:41:00Z
Thanks in advance
We do not know what version of XPath you are using. In XPath 1.0, functions must be wrapped around everything else because they cannot be steps in a path expression:
string(//div[#id = 'qa-case']/time/#datetime)
This only works with exactly one time element node and thus with one datetime attribute.
In XPath 2.0 you could also do
//div[#id = 'qa-case']/time/#datetime/string()
The result, in both cases, is
2015-01-12T02:41:00Z
To give a more specific answer, we would need to know more about the environment you use XPath in (say, XSLT).
To get just the text, you don't really need any date functions, just //div[#id="qa-case"]/#datetime
If you want to convert to a more readable format you cna then use date-specific XPath functions, for example:
<p>Today is <xsl:value-of="fn:day-from-dateTime(#datetime)" </p>

Xpath command in importXML

I need a some help in getting a ImportXML command to return the result I'm looking for.
I'm using Google Sheets.
The XML I'm using looks like this:
<DocumentElement>
<BuyOrder>
<itemName>Name of item 1</itemname>
<itemID>1</itemID>
<maxPrice>1000</maxPrice>
<quantity>100</quantity>
</BuyOrder>
<BuyOrder>
<itemName>Name of item 2</itemname>
<itemID>2</itemID>
<maxPrice>2000</maxPrice>
<quantity>200</quantity>
</BuyOrder>
</DocumentElement>
And so on.
What I need is an XPath expression I can use to get maxPrice on a specific itemID.
The ID for itemID comes from another cell in the sheet.
I think this xpath will help you
xpath = "//itemID[contains(.,'1')]/following-sibling::maxPrice"
Try something like the following:
//DocumentElement/BuyOrder[itemID=2]/maxPrice
http://www.w3schools.com/XPath/xpath_examples.asp has some nice examples for learning XPath. they also have an online XPath tester.
http://www.xpathtester.com/xpath is another online XPath tester.

Use Xpath to find element using starts-with in tag

My html:
<span script-data="MY_Product-4425"> This is my Product Name </span>
I'm trying to write an xpath that will find the text: "This is my Product Name, using the "script-data" tag, but the 4 digit value after My_Product is a dynamic number, so the only thing known is that the tag will start with: script-data="MY_Product-"
I've tried this, but its not working..
//tag[starts-with('script-data',"MY_Product-")]
How can I use starts-with for this? Is there a better option?
If the answer from Arup Rakshit is not working, you probably have a default namespace.
Try this instead...
//*[local-name()='span'][starts-with(#script-data,'MY_Product-')]
Use the below XPATH 1.0
//span[starts-with(#script-data,'MY_Product')]

Select element with a changing Id string using XPath

I have a textarea control with an Id that goes something like this:
<textarea id="NewTextArea~~51887~~1" rows="2"/>
And the xpath that has worked before has been
//textarea[#id, "NewTextArea~~51887~~1"]
But now the '51887' portion of the id is become diverse (changing every time) so I need to select the NewtextArea~~*~~1 element without actually specifying the number. Is there a way I can wildcard part of the string so that it will match a particular pattern? I tried using starts-with and ends-with but couldn't get it to work:
//textarea[starts-with(#id, 'NewTextArea~~') and ends-with(#name, '~~1')]
Bare in mind there are other fields with the difference being the number on the end.
Any advice or guidance would be greatly appreciated :)
I tried using starts-with and ends-with but couldn't get it to work:
//textarea[starts-with(#id, 'NewTextArea~~') and ends-with(#name, '~~1')]
ends-with() is available as a standard function only in XPath 2.0 and you seem to be using XPath 1.0.
Use:
//textarea
[starts-with(#id, 'NewTextArea~~')
and
substring(#id, string-length(#id) - 2) = '~~1'
]
Explanation:
See the answer to this question, for how to implement ends-with() in XPath 1.0:
https://stackoverflow.com/a/405507/36305

How to match the brother of a certain XML element in ruby?

I played around with nokogiri in ruby and the XML searching feature, e.g.:
a = Nokogiri.XML(open 'a.xml')
x = a.search('//div[#class="foo"]').text
which works quite nice.
But how can I specify to match the next (brother) element on the same level (and only the next)?
For example for this input:
<div>
<div>...</div>
<div>...</div>
<div class="foo"></div>
<div>EXTRACT ME</dev>
...
</div>
The actual input is some non-XHTML html, but so far Nokogiri.XML does not complain.
Btw, what filter syntax f.search actually expects? xpath?
Taking the hint from Brian Agnew and DevNull I guess that f.search actually expects xpath syntax and using the following-sibling predicate the following expression matches what was asked:
a = x.search('//div[#class="foo"]/following-sibling::div[1]')
I think you want XPath's following-sibling predicate.

Resources