如何在XSLT中创建一个增加1 foreach循环的计数器,我已经嵌套了foreach循环

时间:2019-11-20 14:18:30

标签: xml xslt

<xsl:template match="Quote">
<Quote>    

        <xsl:for-each select="Configuration"> 
            <ConfigItem>
                <xsl:variable name="GeneratedStructure">
                <xsl:variable name="ConfigurationPos" select="position()+10"/> 
                <configurationPostion><xsl:value-of select = "$ConfigurationPos" /> </configurationPostion> 
                <!--  <VariableValue><xsl:value-of select="$ConfigurationPos"/></VariableValue>-->
                <Vendor><xsl:value-of select="Vendor" /></Vendor>
                <PriceListID><xsl:value-of select="PriceListID" /></PriceListID>
                <Source><xsl:value-of select="Source" /></Source>
                <xsl:for-each select="ProductLineItem">
                    <ProductLineItem>
                        <xsl:variable name="ProductPos" select="$ConfigurationPos+(position()*10)"/>
                        <ProductPostionNumber><xsl:value-of select = "$ProductPos" /></ProductPostionNumber>
                    </ProductLineItem>   
                </xsl:for-each>
                </xsl:variable>  
                <xsl:variable name="SubtotalPos" select="max($GeneratedStructure/ProductLineItem/ProductPostionNumber)"/>
                <xsl:copy-of select="$GeneratedStructure"/> 
                <xsl:variable name="max">
                    <xsl:for-each select="$GeneratedStructure/ProductLineItem/ProductPostionNumber">
                        <xsl:sort select="." data-type="number" order="descending"/>

                    </xsl:for-each>
                </xsl:variable>
                <subMax><xsl:value-of select = "number($max)+10" /></subMax>
            </ConfigItem>
        </xsl:for-each>  
</Quote>
</xsl:template>

如何在XSLT中创建一个增加1个foreach循环的计数器,我在一个foreach循环中有一个foreach循环,我已经编写了它,但是一旦父级foreach循环开始进行下一次迭代,该计数器将设置为零

1 个答案:

答案 0 :(得分:0)

如果-似乎-您要使用相同的序列号给父母和孩子编号,然后尝试使用xsl:number。例如:

XML

<Quote>
   <Configuration>
      <RowID>12333dddd</RowID>
      <ProductLineItem>
         <LineNumber>1.0.1</LineNumber>
      </ProductLineItem>
      <ProductLineItem>
         <LineNumber>2.0.1</LineNumber>
      </ProductLineItem>
   </Configuration>
   <Configuration>
      <RowID>12ss333dddd</RowID>
      <ProductLineItem>
         <LineId>5165550509</LineId>
      </ProductLineItem>
   </Configuration>
</Quote>

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="Quote">
    <Quote>    
        <xsl:for-each select="Configuration"> 
            <ConfigItem>
                <configurationPostion>
                    <xsl:number count="Configuration | ProductLineItem" level="any"/>
                </configurationPostion>
                <xsl:for-each select="ProductLineItem">
                    <ProductPostionNumber>
                        <xsl:number count="Configuration | ProductLineItem" level="any"/>
                    </ProductPostionNumber>
                </xsl:for-each>
            </ConfigItem>
        </xsl:for-each>  
    </Quote>
</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="utf-16"?>
<Quote>
  <ConfigItem>
    <configurationPostion>1</configurationPostion>
    <ProductPostionNumber>2</ProductPostionNumber>
    <ProductPostionNumber>3</ProductPostionNumber>
  </ConfigItem>
  <ConfigItem>
    <configurationPostion>4</configurationPostion>
    <ProductPostionNumber>5</ProductPostionNumber>
  </ConfigItem>
</Quote>
相关问题