How do I retrieve an XML Element where an attribute is either X or Y?
-
20-09-2019 - |
Question
Consider this xml:
<parent>
<child name="alpha" />
</parent>
and also this xml
<parent>
<child name="beta" />
</parent>
There should be only a sinlge node at /parent/child with either alpha or beeta as it's name value.
To clarrify... there will never be 2 child nodes one each named alpha and beta
I'm trying to create a single xpath query which will return the value of name in each of these 2 cases.
I tried this...
/parent/child[@name='alpha' | @name='beta']/@name
...but it does not work.
How should I return this value..?
Update: I ammendedd the samples to ensure they were well formed as one answer indicated they were not.
Solution
Try with:
/parent/child[(@name='alpha') or (@name='beta')]/@name
It should also work without the parentheses.
OTHER TIPS
The XML doesn't appear to be well-formed, it should be:
<parent>
<child name="alpha"/>
</parent>
<parent>
<child name="beta"/>
</parent>
If the XML isn't well-formed, I wouldn't expect much else to work..
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow