user2653559
user2653559

Reputation: 1

XML XPATH Query Example

Can anyone help. I have the XML below:

<metadata>
  <item>
    <tag>CustomerNumber</tag>
    <value type="string">pa440309201</value>
  </item>
  <item>
    <tag>DocumentType</tag>
    <value type="string">Proof of ID</value>
  </item>
  <item>
    <tag>Branch</tag>
    <value type="string">Derby</value>
  </item>
  <item>
    <tag>StoreCode</tag>
    <value type="string">440</value>
  </item>
</metadata>

I need to extract the value of the StoreCode from the item, so need to read the value element text when the Tag of an Item is equal to StoreCode - I need to do this from multiple XML files and the order of the ITEMs is not always the same.

Thanks in advance

John

Upvotes: 0

Views: 252

Answers (2)

paul trmbrth
paul trmbrth

Reputation: 20748

Try:

'//item[tag[.="StoreCode"]]/value/text()'

or, easier to understand:

'//item[tag[text()="StoreCode"]]/value/text()'

Upvotes: 0

Lucas
Lucas

Reputation: 14969

Should be something like this:

/metadata/item[tag='StoreCode']/value

See the predicates section of this article and think of your tag element as analogous to the price element in the example.

Note: this is untested...

Upvotes: 3

Related Questions