Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPATH query to filter values on certain attributes only

Tags:

xpath

I have following XML:

<Library>  <Item>   <Field Name="Name">smooth</Field>   <Field Name="File Type">mid</Field>   <Field Name="File Size">60026</Field>  </Item>  <Item>   <Field Name="Name">mid</Field>   <Field Name="File Type">mp3</Field>   <Field Name="File Size">4584972</Field>  </Item> </Library> 

I'd like to get all item names of items of the file type "mid". My XPATH query looks as

/Library/Item/Field     [     @Name="Name" and      (../Field[@Name="File Type" and ../Field[.="mid"]])     ] 

But unfortunately both items are returned from that query.

smooth mid 

Seems that the last condition is not checked against fields with the attribute "File Type" only, but all fields. Which results in a return of the name "mid", even if this item is of file type "mp3".

How can I restrict the comparison with "mid" to the value of the field with the attribute Name="File Type"?

Can anybody suggest me an XPATH syntax which works as expected?

like image 235
klzlk Avatar asked Jun 08 '11 17:06

klzlk


People also ask

How do I filter in XPath?

Filtering with XPath First, add your filtering rules by specifying the rule criteria and clicking the Add Rule button. Then click Apply Filters to XPath to generate an XPath expression. You can also manually write an XPath expression to filter your file.

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.


1 Answers

XPath predicates can be applied anywhere, this would be more straight-forward:

/Library/Item[Field[@Name="File Type"] = "mid"]/Field[@Name="Name"] 

Your own expression would be correct as

/Library/Item/Field[   @Name="Name"    and ../Field[@Name="File Type"] = "mid" ] 
like image 118
Tomalak Avatar answered Sep 28 '22 09:09

Tomalak