XPATH query to filter values on certain attributes only

50,529

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"
]
Share:
50,529

Related videos on Youtube

klzlk
Author by

klzlk

Updated on July 09, 2022

Comments

  • klzlk
    klzlk almost 2 years

    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?