2012-06-29 48 views
0

我不得不從for-each迴路斷線,如果@PercentOfAmountActive不等於0或100從每個迴路斷線在XSLT

這是我的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> 

有沒有辦法做到這一點?

回答

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> 

只有前四個節點輸出,模擬 '休息' 的效果,一旦我們打的是不適合的節點。

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> 
+0

我想,如果不是0或100以外的任何值比來擺脫這種對每個而不是進一步處理... – Hussey