How to do group capture in xpath -
i looking in xpath can do: //foo[@n="$1"]//bar[@n="$2"]
can return me $1
, $2
. or @ least return me both <foo>
, <bar>
here more detail, have xml document:
<xml> <foo> <a n="1"> <b n="1"/> <b n="2"/> </a> </foo> <a n="2"> <b n="1"/> </a> <a n="3"> <b n="1"/> <foo> <b n="2"/> </foo> <b n="3"/> </a> </xml>
i want generate string base on n attribute in <a>
, <b>
have xpath: //a[@n]//b[@n]
every result back, use: ./@n
, ./ancestor::a/@n
info want.
this working fine, need more intelligent, because have lot structure this, , need auto generate xpath.
so above example, looking xpath like: //a[@n="$1"]//b[@n="$2"]
return me: `(1, 1), (1, 2), (2, 1), (3, 1), (3, 2), (3, 3)
here 1 xpath 1.0 expression selects wanted n
attributes:
//a[.//b]/@n | //a//b/@n
without optimization evaluation of above expression performas @ least 2 complete traversals of xml document.
this xpath 1.0 expression may more efficient:
//*[self::a , .//b or self::b , ancestor::a]/@n
both expressions can simplified if guaranteed every a
has b
descendant.
they become, respectively:
//a/@n | //a//b/@n
and:
//*[self::a or self::b , ancestor::a]/@n
further simplification possible if guaranteed every a
has descendant b
, every b
has ancestor a
.:
//*[self::a or self::b]/@n
it impossible in single xpath 1.0 expression string values of wanted attributes. 1 needs atributes using 1 of above expressions, on each of selected attributes apply second xpath expression: string()
.
in xpath 2.0 possible single expression string values of wanted attributes -- appending each of expressions /string(.)
for example, simplest one:
//(a|b)/@n/string(.)
update:
the op has clarified question. know wants result produced:
(1, 1), (1, 2), (2, 1), (3, 1), (3, 2), (3, 3)
it isn't possible produce wanted result single xpath 1.0 expression.
the following xpath 2.0 expression produces wanted result:
for $a in //a[@n , .//b[@n]], $b in $a//b[@n] return concat('(', $a/@n, ',', $b/@n, ') ')
Comments
Post a Comment