开发者

How to select a node referenced by an other node with XPath?

开发者 https://www.devze.com 2023-02-12 07:13 出处:网络
I would like to select the purchase-node with the attribute pgnr, which has the value from another pgnr attribute, startig with \"KEY\", concatinated with \"c\".

I would like to select the purchase-node with the attribute pgnr, which has the value from another pgnr attribute, startig with "KEY", concatinated with "c".

Example:

<root>
<purchact hhid="xxx">
  <purchase pgnr="41">
   <purchvalues field_name="Number" field_value="1"/>
  </purchase>
  <purchase pgnr="KEY9802">
   <开发者_运维问答;purchvalues field_name="Number" field_value="2"/>
  </purchase>
 <purchase pgnr="9802c">
   <purchvalues field_name="Number" field_value="3"/>
  </purchase>
 </purchact>
</root>

In this case, I am looking for the purchase-node with the pgnr-attribute "9802c", because the purchase-node with the pgnr-attribute starting with "KEY" has as the following characters "9802".

I tried

root/purchact/purchase[@pgnr=concat(substring-after(@pgnr, "KEY"), "c")]

but it doesn't work.

Could anybody help? Thanks so much!


This XPath expression:

root/purchact/purchase[
   @pgnr[substring(.,string-length()) = 'c']
][
   concat(
      'KEY',
      substring-before(
         @pgnr,
         'c'
      )
   ) = ../purchase/@pgnr
] 

Meaning: a purchase element having an @pgnr attribute ending with 'c' and for wich there is at least one other @pgnr attribute belonging to a sibling purchase element and being equal to the concatenation of 'KEY' and the string of the given @pgnr before 'c'.


root/purchact/purchase[
    @pgnr = concat(
        substring-after(
            ../purchase[
                contains(@pgnr, 'KEY')
                ]/@pgnr,
            'KEY'
            )
        , 'c')
    ]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号