How to match several different elements in the middle of XPath?
-
Consider this xml:
<rrx id="someother">...</rrx> <rrx id="Zones"> <f name="NM"> <p name="xoxoxo"/> </f> <s name="GB"> <p name="xexexe"/> <p name="xaxaxa"/> </s> <a name="SP"> <p name="hmhmhm"/> </a> <b name="Alien"> <p name="opsops"/> </b> </rrx>
I need to select all "p" elements in "f","s" or "a" with a given "name" but not "b" which are in "rrx" with particular "id". So I'm trying to come up with or statement for f,s,a in the middle of XPath. Using XSLT 2.0 and Xpath 2.0 this works fine: //rrx[@id='Zones']/(f|s|a)[@name='GB']/p - I should get xexexe and xaxaxa and if //rrx[@id='Zones']/(f|s|a)[@name='SP']/p - I need hmhmhm But good old 1.0 says it's invalid. That () are not allowed there. How else would the XPath then look? :omg: :zzz: :doh: -
Consider this xml:
<rrx id="someother">...</rrx> <rrx id="Zones"> <f name="NM"> <p name="xoxoxo"/> </f> <s name="GB"> <p name="xexexe"/> <p name="xaxaxa"/> </s> <a name="SP"> <p name="hmhmhm"/> </a> <b name="Alien"> <p name="opsops"/> </b> </rrx>
I need to select all "p" elements in "f","s" or "a" with a given "name" but not "b" which are in "rrx" with particular "id". So I'm trying to come up with or statement for f,s,a in the middle of XPath. Using XSLT 2.0 and Xpath 2.0 this works fine: //rrx[@id='Zones']/(f|s|a)[@name='GB']/p - I should get xexexe and xaxaxa and if //rrx[@id='Zones']/(f|s|a)[@name='SP']/p - I need hmhmhm But good old 1.0 says it's invalid. That () are not allowed there. How else would the XPath then look? :omg: :zzz: :doh:The feature you try to use is new to XPath 2.0 One of the ways you could solve it in XPath 1.0 would be: instead of: //rrx[@id='Zones']/(f|s|a)[@name='SP']/p write: //p[(parent::f[(@name='SP')and(parent::rrx[@id='Zones'])]) or (parent::s[(@name='SP')and(parent::rrx[@id='Zones'])]) or (parent::a)[(@name='SP')and(parent::rrx[@id='Zones'])]] It does not look nice, but would be an example.