views:

399

answers:

3

I need an XPath that will elect all parent nodes a child with anattribute that constains at least one of a list of values.

Example XML

<mynodes>
  <theParentNode>
    <theChildNode name="one two" />
  </theParentNode>
    <theParentNode>
    <theChildNode name="one all" />
  </theParentNode>
    <theParentNode>
    <theChildNode name="two" />
  </theParentNode>
    <theParentNode>
    <theChildNode name="all" />
  </theParentNode>
</mynodes>

I want to select all nodes where name contains "one" OR "all" (or any other combination for that matter). So that the returned nodeList is:

<theParentNode>
  <theChildNode name="one two" />
</theParentNode>
<theParentNode>
  <theChildNode name="one all" />
</theParentNode>
<theParentNode>
  <theChildNode name="all" />
</theParentNode>

So far my query looks like this (note im not using a schema):

//theChildNode[contains(tokenize(@name, '\s'), "one")]

Which will get me all the vendor elements containing "one" in their name attribute. But im unsure how to go about supplying multiple values in place of the "one", and then the best way to step back up to theParentNode. I could always just do all of this in php but id rather just do it with a XPath if possible.

+2  A: 

A bit redundant, but this should do it:

//theChildNode[contains(tokenize(@name, '\s'), "one") or contains(tokenize(@name, '\s'), "all")]
TheDon
Definitely worked for me. Thanks!
prodigitalson
+1  A: 

Not sure about getting the multiple attribute values, but change your selector to

//theParentNode[theChildNode]

to get all parents with the child node.

//theParentNode[theChildNode[contains....]]

No need to step up, select only parents that have the child.

soconnor
Definitely. Id dont know why i thoughr i couldnt that. Thanks!
prodigitalson
+1  A: 

I don't have an XML test environment available at present, but you might pursue this possibility:

//theChildNode[intersect(tokenize(@name, '\s+'),('one','all'))]
Jim Garrison
That would be the syntax i would expect and tried initially (only i use `|` instead of `,` thinking of an or operator). but got an invalid query (with the `,` sep as well).
prodigitalson