Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select two attributes from the same node with one expression in XPath?

For example:

//person[@id='abc123']/@haircolor|/@weight" 

PS. there are lots of "person" records

like image 520
jacob Avatar asked May 17 '11 10:05

jacob


People also ask

How do I select the second element in XPath?

//div[@class='content'][2] means: Select all elements called div from anywhere in the document, but only the ones that have a class attribute whose value is equal to "content". Of those selected nodes, only keep those which are the second div[@class = 'content'] element of their parent.

Can you use XPath expression used to select the target node and its values?

XPath assertion uses XPath expression to select the target node and its values. It compares the result of an XPath expression to an expected value. XPath is an XML query language for selecting nodes from an XML. Step 1 − After clicking Add Assertion, select Assertion Category – Property Content.

How do I combine two Xpaths?

The | character denotes the XPath union operator. You can use the union operator in any case when you want the union of the nodes selected by several XPath expressions to be returned.


2 Answers

Try this:

//person[@id='abc123']/@*[name()='weight' or name()='haircolor'] 

If you're using an XPath 2.0 processor, you may also use a prettier option:

//person[@id='abc123']/(@haircolor|@weight)` 
like image 191
Kobi Avatar answered Sep 23 '22 01:09

Kobi


Are you wanting to search for person nodes based on the value of multiple attributes. If that's the question then you can just use ands e.g.

//person[@id='abc123' and @haircolor='blue' and @weight='...'] 

If you want to search on a single attribute, but return the values of the other attributes, I would do something like this:

 <xsl:template match="person[@id='abc123']">      <xsl:value-of select="@haircolor"/>      <xsl:value-of select="@weight"/>   </xsl:template> 
like image 41
planetjones Avatar answered Sep 23 '22 01:09

planetjones