从xslt中的每个循环中断

时间:2012-06-29 09:44:39

标签: xslt for-loop break

如果for-each不等于0或100,我必须从@PercentOfAmountActive循环中断。

这是我的XML:

<SamplePointSet Id="1" StartDate="2012-01-01T04:00:00Z" CalendarId="1" Cyclic="6" ForAttribute="0" ObjectId="0" ProbabilityFunctionId="0" TableNumber="0" TimePeriodId="4" ParentId="1">
    <SamplePoint NumberOfActiveTimePeriods="30" PercentOfAmountActive="100" Sequence="1" SamplePointSetId="1" />
    <SamplePoint NumberOfActiveTimePeriods="30" PercentOfAmountActive="0" Sequence="2" SamplePointSetId="1" />
    <SamplePoint NumberOfActiveTimePeriods="30" PercentOfAmountActive="100" Sequence="3" SamplePointSetId="1" />
    <SamplePoint NumberOfActiveTimePeriods="30" PercentOfAmountActive="0" Sequence="4" SamplePointSetId="1" />
    <SamplePoint NumberOfActiveTimePeriods="30" PercentOfAmountActive="100" Sequence="5" SamplePointSetId="1" />
    <SamplePoint NumberOfActiveTimePeriods="30" PercentOfAmountActive="0" Sequence="6" SamplePointSetId="1" />
    <SamplePoint NumberOfActiveTimePeriods="30" PercentOfAmountActive="100" Sequence="7" SamplePointSetId="1" />
    <SamplePoint NumberOfActiveTimePeriods="30" PercentOfAmountActive="0" Sequence="8" SamplePointSetId="1" />
    <SamplePoint NumberOfActiveTimePeriods="30" PercentOfAmountActive="100" Sequence="9" SamplePointSetId="1" />
    <SamplePoint NumberOfActiveTimePeriods="30" PercentOfAmountActive="0" Sequence="10" SamplePointSetId="1" />
    <SamplePoint NumberOfActiveTimePeriods="30" PercentOfAmountActive="100" Sequence="11" SamplePointSetId="1" />
    <SamplePoint NumberOfActiveTimePeriods="30" PercentOfAmountActive="0" Sequence="12" SamplePointSetId="1" />
  </SamplePointSet>

下面是xslt代码

<xsl:for-each select=".../CM:SamplePointSet/CM:SamplePoint">
  <xsl:variable name="varActiveTimePeriod" select="./@NumberOfActiveTimePeriods * $varMultiple"/>
  <xsl:variable name="varPercentOfAmountActive" select="./@PercentOfAmountActive"/>

  <!-- . . . some Condition To break if (percent of amount active) not 0 or 100 -->

  <xsl:value-of select="CMXsltExtObject:SetRecurenceRule($varActiveTimePeriod, $varPercentOfAmountActive, $varCalendarFrequency)"/>
</xsl:for-each>

有什么办法吗?

2 个答案:

答案 0 :(得分:3)

避免在XSLT中使用每个循环。相反,在可能的情况下,将节点应用于模板,使用XPath仅定位那些合适的节点。

您可以通过仅将模板应用于这些节点来实现中断效果......

  • @PercentOfAmountActive属性等于0或100
  • 其前任兄弟姐妹中没有一个@PercentOfAmountActive属性不等于0或100。

以下是一个简化示例,您可以在this XMLPlayground运行。

<强> XML

<root>
    <node attr='0'>hello 1</node>
    <node attr='100'>hello 2</node>
    <node attr='0'>hello 3</node>
    <node attr='100'>hello 4</node>
    <node attr='1'>hello 5</node>
    <node attr='0'>hello 6</node>
    <node attr='100'>hello 7</node>
</root>

<强> XSLT

<xsl:template match='/'>
    <ul>
        <xsl:apply-templates select='root/node[(@attr = 0 or @attr = 100) and not(preceding-sibling::*[@attr != 0 and @attr != 100])]' />
    </ul>
</xsl:template>

<xsl:template match='node'>
    <li><xsl:value-of select='.' /></li>
</xsl:template>

只输出前四个节点,一旦我们点击了一个不合适的节点,就会模拟“中断”效果。

答案 1 :(得分:2)

xsl:for-each指令不是循环,它是从输入序列到输出序列的映射。您在程序术语中描述为“循环”中的“中断”实际上是说您希望映射在第一个属性值不等于1或100之前选择输入序列中的那些项。 / p>

最有效的解决方案可能是使用兄弟递归:

<xsl:template match="SamplePointSet">
    <xsl:apply-templates select="SamplePoint[1]"/>
</xsl:template>

<xsl:template match="SamplePoint">
  ... some processing ...
  <xsl:if test="@A = 1 or @A = 100">
    <xsl:apply-templates select="following-sibling::SamplePoint[1]"/>
  </xsl:if>
</xsl:template>