[英]XSLT following-sibling output not as expected
I want to get all elements between the first sale and the next sale. 我希望在第一次销售和下一次销售之间获得所有元素。 Initial XML 初始XML
<Parent>
<Sale SeqNo="1"/>
<Discount SeqNo="2"/>
<Coupon SeqNo="4"/>
<CouponDetail SeqNo="5"/>
<Sale SeqNo="6"/>
<Sale SeqNo="7"/>
<Sale SeqNo="8"/>
<Payment SeqNo="9"/>
</Parent>
Desired XML: 期望的XML:
<Discount SeqNo="2"/>
<Coupon SeqNo="4"/>
<CouponDetail SeqNo="5"/>
I currently have the following applying to a template 我目前有以下申请模板
following-sibling::*[(number(@SeqNo) < number(following::Sale[1]/@SeqNo))]
Output I am currently getting: 我目前得到的输出:
<Discount SeqNo="2"/>
<Coupon SeqNo="4"/>
<CouponDetail SeqNo="5"/>
<Sale SeqNo="6"/>
<Sale SeqNo="7"/>
<Sale SeqNo="8"/>
<Payment SeqNo="9"/>
If I hardcode in the sequence number of the next sale item I get the correct output 如果我硬编码下一个销售项目的序列号,我得到正确的输出
following-sibling::*[(number(@SeqNo) < number(6))]
Output: 输出:
<Discount SeqNo="2"/>
<Coupon SeqNo="4"/>
<CouponDetail SeqNo="5"/>
What am i missing? 我错过了什么? Any help appreciated. 任何帮助赞赏。
The issue is the context in which your predicate is being evaluated - it's the particular element child of Parent
that is being tested for whether it should be included in the resulting node-set. 问题是正在评估谓词的上下文 - 它正在测试Parent
的特定元素子元素是否应该包含在结果节点集中。 So in that context, following::Sale[1]
gives you the first Sale
element following the element being tested, which will always have a higher @SeqNo
, even if it is itself a Sale
element. 所以在这个上下文中, following::Sale[1]
为你提供了被测试元素后面的第一个Sale
元素,它总是有一个更高的@SeqNo
,即使它本身就是一个Sale
元素。
What you need to do is store the sequence number of interest in an xsl:variable
prior to doing your select, for instance: 您需要做的是在执行选择之前将感兴趣的序列号存储在xsl:variable
,例如:
<xsl:variable name="end_seq_no" select="following-sibling::Sale[1]/@SeqNo"/>
<xsl:apply-templates select="following-sibling::*[@SeqNo < $end_seq_no]"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.