找到 xpath 来选择以下同级,而不是在另一个与 xsl 中的当前标签相同的标签之后 [英] find xpath to pick following sibling not after another tag same as current in xsl
问题描述
当我在下面匹配 A 或 A1 模板时,我想获得以下兄弟 C,仅用于第二个 A 或 A1:
I would like to get the following sibling C when I'm matching A or A1 template in the following, for the second A or A1 only:
<root>
<A/> <!-- first A -->
<B/>
<A/> <!-- second A -->
<C/>
<A1/>
<B/>
<A1/>
<B/>
<C/>
</root>
如果我匹配第一个 A,我不必检索任何东西,因为在我的 C 标记之前还有另一个 A.
If I'm matching the first A, I have not to retrieve anything as there is another A before my C tag.
如果我匹配第二个 A,我必须检索 C 标签.
If I'm matching the second A, I have to retrieve the C tag.
我尝试了以下方法但没有成功:
I tried the following without success:
<xsl:template match="A|A1"> <xsl:if test="following-sibling::C[preceding-sibling::A|A1[1] = .]">
似乎在每种情况下都是如此,但为什么呢?
It seems to be true in every case but why?
推荐答案
在 XSLT 2.0 中,您可以使用类似
In XSLT 2.0 you could use a test like
following-sibling::C[ (preceding-sibling::A|preceding-sibling::A1)[last()] is current()]
找出所有最接近的前面的
A
或A1
是您首先想到的 C.对于 XSLT 1.0,您必须比较generate-id
值而不是使用is
to find all the Cs whose nearest preceding
A
orA1
is the one you first thought of. For XSLT 1.0 you'd have to comparegenerate-id
values instead of usingis
following-sibling::C[ generate-id((preceding-sibling::A|preceding-sibling::A1)[last()]) = generate-id(current())]
这将选择
<root> <A/> <!-- nothing --> <B/> <A/> <!-- C1 --> <C/> <!-- this is C1 --> <A1/> <!-- nothing --> <B/> <A1/> <!-- C2 --> <B/> <C/> <!-- this is C2 --> </root>
<小时>
您的原始测试
Your original test
following-sibling::C[preceding-sibling::A|A1[1] = .]
相当于
following-sibling::C[ ( preceding-sibling::A | (child::A1[1]) ) = .]
并且会选择所有后面的
C
兄弟姐妹,对于他们(a)任何他们前面的A
兄弟姐妹或(b)他们的第一个A1
子元素具有与C
本身相同的字符串值.文档中的所有C
元素都满足此谓词,因为每个元素都具有空字符串值.and would select all following
C
siblings for whom either (a) any of their precedingA
siblings or (b) their firstA1
child has a string value the same as that of theC
itself. All theC
elements in your document satisfy this predicate as every element has the empty string value.这篇关于找到 xpath 来选择以下同级,而不是在另一个与 xsl 中的当前标签相同的标签之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!