开发者

how to for every parent node select every not first child node in a tree with multiple parent nodes

开发者 https://www.devze.com 2023-01-08 09:20 出处:网络
His, I think I\'ve got a tricky questions for XPath experts. There is a node structure like this: A(1)-|

His,

I think I've got a tricky questions for XPath experts. There is a node structure like this:

A(1)-|
     |-B(1)
     |-B(2)
     |-B(3)
A(2)-|
     |-B(2.1)
     |-B(2.2)
     |-B(2.3)
...

How to, with a single XPath-expression, extract only the following nodes

A(1)-|
     |-B(2)
     |-B(3)
A(2)-|
     |-B(2.2)
 开发者_如何学编程    |-B(2.3)
...

That is for every parent node its first child element should be excluded.

I tried A/B[position() != 1] but this would filter out only B(1.1) and select B(2.1).

Thanks


This XPath expression (no preceding-sibling:: axis used):

/*/a/*[not(position()=1)]

when applied on this XML document:

<t>
 <a>
   <b11/>
   <b12/>
   <b13/>
 </a>
 <a>
   <b21/>
   <b22/>
   <b23/>
 </a>
</t>

selects the wanted nodes:

<b12 />
<b13 />
<b22 />
<b23 />

This can be verified with this XSLT transformation, producing the above result:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:copy-of select="/*/a/*[not(position()=1)]"/>
 </xsl:template>
</xsl:stylesheet>


Tricky. You could select nodes that have preceding siblings:

A/B[preceding-sibling::*]

This will fail for the first element and succeed for the rest.

0

精彩评论

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