views:

44

answers:

2

This is a piece of my XML document:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<mfVictor>
 <decal>
    <company>Victor</company>
    <title>Wood Horn with Blue Background</title>
    <image>
      <url>victor01.jpg</url>
      <width>60</width>
      <height>60</height>
      <name>Wood Horn Blue Background</name>
      <link></link>
    </image>
    <price>$15.00</price>
    <instock>In Stock</instock>
    <notes>none</notes>
  </decal>
</mfVictor>
</catalog>

I have a function that generates a list of the text from each "title" element. So then, onmousedown of each list item I want to navigate to the "decal" and access all of its children. So far I have this:

//decal[contains(.,\'"+itemName+"\')]

where the itemName is this.innerHTML passed to the function. It looks like the decal is accessed because in the console.log I can see an XMLobject with a length of 1. But I can't figure out how to access either the children of "decal" or the siblings of "title".

Thanks for reading.

A: 

I think you might want //decal[contains(./Title, 'Wood Horn with Blue Background')]

This should give you the decal node with a child node named 'Title' that contains the text 'Wood Horn with Blue Background'

To access the siblings of the Title element you would select the children of the node that this returns. (It's actually returning the decal node, not the title node.)

Toby
+1  A: 

If you need to select a decal based on his title I would prefer the following xpaths:

//decal/title[contains(.,\'"+itemName+"\')]/parent::node()
//decal/title[contains(.,\'"+itemName+"\')]/..
//decal[title[contains(.,\'"+itemName+"\')]]

to select a sibling of the title of the decal you selected you can use

//decal/title[contains(.,\'"+itemName+"\')]/following-sibling::*

If you want all the children of the decal you select you can use (maybe you need this)

//decal[title[contains(.,\'"+itemName+"\')]]/child::*

or better, if you have the complete title

//decal[title[text()=\'"+itemName+"\']]/child::*

With the square bracket you instruct the parser to walk the xml tree to make a selection based on the condition you specify but without changing the context node. Think to the square brackets as a rubber-band, once the test contained in it are satisfied the parser revert back to the starting node and evaluate the rest of the xpath expression. The first two examples are a tipical

Eineki
That explains it. I needed the last one of your examples. Thanks!
Troy