0

I'm quite a novice with Xpath.

Suppose I have an xml catalog to import goods into an e-shop:

<categories>
    <category id="26">Jackets</category>
    <category id="27">Jeans</category>
    <category id="28">Sweaters</category>
    <category id="29">Shirts</category>
    ...
</categories>

<goods>
    <good id="123">
        <categoryId>26</categoryId>
        <label>D&G</label>
        <color>Black</color>
        <size>M, XL</size>
    </good>
    ...
<goods>

The first part of the catalog is a list of goods' categories, the second part is a list of the goods. Each good has a <categoryId> which value equals the "id" attribute of some <category> from the <categories> list.

From the code above I need to get a good's description like this: Category: Jackets; Label: D&G; Color: Black; Size: M, XL.

Label, Color and Size can be picked directly from the <good> context, and it's not the problem. But for the Category I have only an id which is 26 and which matches the id of the Jackets category from the <categories> context.

So my goal is to select the value of the <category id="26">Jackets</category> node using the value of the <categoryId>26</categoryId> node.

Thank you much.

Mikh Bor
  • 1
  • 1
  • 1
    What language are you using to do this? C#? PHP? XSLT? Can you show us some code? – JLRishe Jan 08 '15 at 04:23
  • possible duplicate of [XPath to select Element with attribute value](http://stackoverflow.com/questions/14248063/xpath-to-select-element-with-attribute-value) – JLRishe Jan 08 '15 at 04:29
  • @JLRishe I use Drupal CMS and Feeds XPath Parser module. – Mikh Bor Jan 08 '15 at 23:07

2 Answers2

1

To selected the category associated with a good that has an @id of 123, use this XPath:

//categories/category[@id = //goods/good[@id='123']/categoryId]
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thanks, but it works for only one good. The catalog I work with has 1000+ goods, I need something universal which would work for all of them. – Mikh Bor Jan 08 '15 at 23:09
  • This XPath universally would select the `category` associated with any `good` specified via its `@id`, regardless of the number of `good` elements. '123' was merely an example; use any @id there you'd like. You did ask to be able to "*select the value of the `Jackets` node using the value of the `26` node.*" How else would you like to specify the `good` of interest other than via its `@id`? – kjhughes Jan 09 '15 at 00:34
0

Assuming you have retrieved the value 26 from the categoryId element already, the XPath to select the element with "Jackets" in it would be along the lines of:

//categories/category[@id = "26"]

Depending on what language you're using, you may need to incorporate that 26 using string concatenation, or in XSLT, you can use a variable in the expression directly:

//categories/category[@id = $categoryId]

(However, in XSLT, a better approach would be to use keys).

JLRishe
  • 99,490
  • 19
  • 131
  • 169