Reputation: 23
I'm an XPath novice and I have the following xml:
`<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<colors>
<color id="1">green</color>
<color id="2">red</color>
<color id="3">yellow</color>
</colors>
<items>
<item id="095">
<title>pencil</title>
<colorId>3</colorId>
</item>
<item id="100">
<title>pen</title>
<colorId>1</colorId>
</item>
<item id="200">
<title>ruler</title>
<colorId>2</colorId>
</item>
</items>
</catalog>`
As you can see, colorId values correspond to the color id attributes, so "pencil" is "yellow" (3), "pen" is "green" (1) and "ruler" is "red" (2). The question is how do I get value for each item via colorId value?
Thanks in advance.
Upvotes: 2
Views: 7258
Reputation: 23637
This will return the title
based on the id
:
/catalog/items/item[colorId = /catalog/colors/color/@id]/title
This will return the color
corresponding to the colorId
:
/catalog/colors/color[@id = /catalog/items/item/colorId]
You can then add position predicates to select specific relationships.
Upvotes: 4
Reputation: 8058
Assuming I understand the question -- that you want to look for <item>
s that are a specific color -- what you're looking for is XPath Predicates. For example:
/catalog/items/item[colorId=2]
will find the s whose child's contents equal 2.
Upvotes: 0