如何在XSLT的Foreach循环外获取变量值

问题描述 投票:0回答:1
  <Quote>    
        <xsl:variable name="ProductPos"></xsl:variable>
        <xsl:variable name="SubtotalPos"></xsl:variable>
        <xsl:for-each select="Configuration"> 

            <ConfigItem>
                <xsl:variable name="ConfigurationPos" select="position()*10"/> 
                 <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>
                <Subtotal><xsl:value-of select="$ProductPos"/></Subtotal>
            </ConfigItem>
        </xsl:for-each>
    </Quote>  

在上面的示例中,我需要用$ prodPos + 10填充小计标签值

xslt+xml
1个回答
0
投票

您只需要对您的代码进行少量更改。如果不立即关闭标签,则可以写入变量。这样可以保留您的结构,但可以使用代码进行访问。

<xsl:variable name="content">
    <Configuration>
        <ProductLineItem></ProductLineItem>
    </Configuration>
    <Configuration>
        <ProductLineItem></ProductLineItem>
        <ProductLineItem></ProductLineItem>
    </Configuration>
</xsl:variable>
<Quote>    
    <xsl:variable name="GeneratedStructure">
        <xsl:for-each select="$content/Configuration"> 
            <ConfigItem>
                <xsl:variable name="ConfigurationPos" select="position()*10"/> 
                <xsl:variable name="ProductLineItem">
                    <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:copy-of select="$ProductLineItem"/>
                <Subtotal><xsl:value-of select="sum($ProductLineItem/ProductLineItem/ProductPostionNumber)"/></Subtotal>
            </ConfigItem>
        </xsl:for-each>
    </xsl:variable>
    <xsl:copy-of select="$GeneratedStructure"/> <!-- This will output the content of the variable -->
    <xsl:for-each select="$GeneratedStructure/ConfigItem/Subtotal">
        <xsl:element name="Subtotal{position()}">
            <xsl:value-of select="number(.) + 10"/><!-- You did not specify if it should simply add 10 or add how often the for each loop repeats. Fix this for your needs. -->
        </xsl:element>
    </xsl:for-each>
</Quote>

当前输出:

<ConfigItem>
    <ProductLineItem>
        <ProductPostionNumber>20</ProductPostionNumber>
    </ProductLineItem>
    <Subtotal>20</Subtotal>
</ConfigItem>
<ConfigItem>
    <ProductLineItem>
        <ProductPostionNumber>30</ProductPostionNumber>
    </ProductLineItem>
    <ProductLineItem>
        <ProductPostionNumber>40</ProductPostionNumber>
    </ProductLineItem>
    <Subtotal>70</Subtotal>
</ConfigItem>
<Subtotal1>30</Subtotal1>
<Subtotal2>80</Subtotal2>

[请考虑检查您的拼写,因为如果用其他名称来称呼它,很难假设是什么意思。

编辑:我确实根据您的需要进行了编辑。虽然目前尚不清楚您到底想做什么。如果您显示一些输入和预期的输出,我可以解决此问题。

© www.soinside.com 2019 - 2024. All rights reserved.