Output attribute value with XPath 1.0
Question
Example:
<div class='known' name='unknown'>
How can I return name
attribute value ('unknown') based on known class
attribure value, with XPath 1.0?
While looking for an answer I found that XPath 2.0 has instrument for this:
//div[@class='known']/@name/string()
But can't find XPath 1.0 analogy
Solution
Use:
string(//div[@class='known']/@name)
This produces the string value of the name
attribute of the first in document order div
element such that the string value of its class
attribute is "unknown"
.
If //div[@class='known']
selects more than one div
element and you want the value of the name
attribute for the k-th selected div
, use:
string((//div[@class='known'])[$k]/@name)
where $k
has to be substituted with the wanted integer, or otherwise the variable $k
must be in the evaluation context for the XPath expression.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow