0

如果不等于 0 或 100,我必须从for-each循环中中断。@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>

有什么办法吗?

4

2 回答 2

3

避免 XSLT 中的 for-each 循环。相反,在可能的情况下,将您的节点应用于模板,使用 XPath 仅针对那些合适的节点。

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

  • @PercentOfAmountActive属性等于 0 或 100
  • 其前面的兄弟姐妹都@PercentOfAmountActive没有不等于 0 或 100 的属性。

这是一个简化的示例,您可以在此 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>

仅输出前四个节点,模拟一旦我们遇到不合适的节点时的“中断”效果。

于 2012-06-29T10:02:00.810 回答
2

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

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

<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>
于 2012-06-29T11:31:06.093 回答