Output Attribute Value with XPath 1.0

Example:

<div class='known' name='unknown'> 

How can I return the value of the name ('unknown') attribute based on the known attribute of the attribute class with XPath 1.0?

When searching for an answer, I found that XPath 2.0 has a tool for this:

  //div[@class='known']/@name/string() 

But Unable to Find XPath 1.0 Analogy

+4
source share
1 answer

Using

 string(//div[@class='known']/@name) 

This creates the string value of the name attribute of the first order in the div element of the document, so the string value of its class attribute is "unknown" .

If //div[@class='known'] selects more than one div element and you need the value of the name attribute for the k-th div selected, use:

 string((//div[@class='known'])[$k]/@name) 

where $k must be replaced with the required integer, otherwise the variable $k must be in the evaluation context for the XPath expression.

+5
source

Source: https://habr.com/ru/post/1393559/


All Articles