XSLT条件在foreach中只发生一次

XSLT条件在foreach中只发生一次,xslt,Xslt,考虑以下示例: XML Please let know xslt experts on the best way to do this ? 船 2. 拒绝 4. 粗俗的 3. 可接受 5. 船 2. 拒绝 4. 粗俗的 3. 可接受 船 拒绝 4. 粗俗的 3. 可接受 0 XSLT1.0 <Lots> <Lot> <LotDetails> <GCode>Ship</GCode>

考虑以下示例:

XML

  Please let know xslt experts on the best way to do this ?

船
2.
拒绝
4.
粗俗的
3.
可接受
5.
船
2.
拒绝
4.
粗俗的
3.
可接受
船
拒绝
4.
粗俗的
3.
可接受
0
XSLT1.0

<Lots>
    <Lot>
        <LotDetails>
            <GCode>Ship</GCode>
            <ProductQuantity>2</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Reject</GCode>
            <ProductQuantity>4</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Gross</GCode>
            <ProductQuantity>3</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Acceptable</GCode>
            <ProductQuantity>5</ProductQuantity>
        </LotDetails>
    </Lot>
    <Lot>
        <LotDetails>
            <GCode>Ship</GCode>
            <ProductQuantity>2</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Reject</GCode>
            <ProductQuantity>4</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Gross</GCode>
            <ProductQuantity>3</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Acceptable</GCode>
        </LotDetails>
    </Lot>
    <Lot>
        <LotDetails>
            <GCode>Ship</GCode>
        </LotDetails>
        <LotDetails>
            <GCode>Reject</GCode>
            <ProductQuantity>4</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Gross</GCode>
            <ProductQuantity>3</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Acceptable</GCode>
            <ProductQuantity>0</ProductQuantity>
        </LotDetails>
    </Lot>
</Lots>

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:strip-space elements="*"/>

<xsl:template match="/Lots">
    <xsl:copy>
        <xsl:for-each select="Lot">
            <xsl:copy>
                <Quantity>
                    <xsl:choose>
                        <xsl:when test="LotDetails[GCode='Acceptable' and ProductQuantity > 0]">
                            <xsl:value-of select="LotDetails[GCode='Acceptable']/ProductQuantity"/>
                        </xsl:when>
                        <xsl:when test="LotDetails[GCode='Ship' and ProductQuantity > 0]">
                            <xsl:value-of select="LotDetails[GCode='Ship']/ProductQuantity"/>
                        </xsl:when>
                        <xsl:when test="LotDetails[GCode='Gross' and ProductQuantity  >0]">
                            <xsl:value-of select="LotDetails[GCode='Gross']/ProductQuantity"/>
                        </xsl:when>
                        <xsl:otherwise>0</xsl:otherwise>
                    </xsl:choose>
                </Quantity>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

5.
2.
3.

考虑以下示例:

XML

  Please let know xslt experts on the best way to do this ?

船
2.
拒绝
4.
粗俗的
3.
可接受
5.
船
2.
拒绝
4.
粗俗的
3.
可接受
船
拒绝
4.
粗俗的
3.
可接受
0
XSLT1.0

<Lots>
    <Lot>
        <LotDetails>
            <GCode>Ship</GCode>
            <ProductQuantity>2</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Reject</GCode>
            <ProductQuantity>4</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Gross</GCode>
            <ProductQuantity>3</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Acceptable</GCode>
            <ProductQuantity>5</ProductQuantity>
        </LotDetails>
    </Lot>
    <Lot>
        <LotDetails>
            <GCode>Ship</GCode>
            <ProductQuantity>2</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Reject</GCode>
            <ProductQuantity>4</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Gross</GCode>
            <ProductQuantity>3</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Acceptable</GCode>
        </LotDetails>
    </Lot>
    <Lot>
        <LotDetails>
            <GCode>Ship</GCode>
        </LotDetails>
        <LotDetails>
            <GCode>Reject</GCode>
            <ProductQuantity>4</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Gross</GCode>
            <ProductQuantity>3</ProductQuantity>
        </LotDetails>
        <LotDetails>
            <GCode>Acceptable</GCode>
            <ProductQuantity>0</ProductQuantity>
        </LotDetails>
    </Lot>
</Lots>

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:strip-space elements="*"/>

<xsl:template match="/Lots">
    <xsl:copy>
        <xsl:for-each select="Lot">
            <xsl:copy>
                <Quantity>
                    <xsl:choose>
                        <xsl:when test="LotDetails[GCode='Acceptable' and ProductQuantity > 0]">
                            <xsl:value-of select="LotDetails[GCode='Acceptable']/ProductQuantity"/>
                        </xsl:when>
                        <xsl:when test="LotDetails[GCode='Ship' and ProductQuantity > 0]">
                            <xsl:value-of select="LotDetails[GCode='Ship']/ProductQuantity"/>
                        </xsl:when>
                        <xsl:when test="LotDetails[GCode='Gross' and ProductQuantity  >0]">
                            <xsl:value-of select="LotDetails[GCode='Gross']/ProductQuantity"/>
                        </xsl:when>
                        <xsl:otherwise>0</xsl:otherwise>
                    </xsl:choose>
                </Quantity>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

5.
2.
3.

您的问题不清楚。请在给定示例中显示预期结果,并在必要时添加更多示例。-仅供参考,
xsl:for each
不是循环,不能退出或继续。如果希望得到单个结果,请不要对每个@Dev使用
xsl:for,请尝试将整个条件放入If标记或模板的匹配条件中。否则,如果出于某种原因需要为每一个变量创建xsl变量并将其用作标志。显示所需的值后,将变量设置为false,以忽略任何其他值。感谢您的输入-我将更改为单个if,不考虑每个条件我需要的是在父xml的第二次迭代中使用多个元素,如下所示:Ship 2您的问题不清楚。请在给定示例中显示预期结果,并在必要时添加更多示例。-仅供参考,
xsl:for each
不是循环,不能退出或继续。如果希望得到单个结果,请不要对每个
@Dev使用
xsl:for,请尝试将整个条件放入If标记或模板的匹配条件中。否则,如果出于某种原因需要为每一个变量创建xsl变量并将其用作标志。一旦显示了所需的值,将变量设置为false以忽略任何其他值。感谢您的输入-对于每个条件,我将更改为单个if,但不考虑。我需要的是在父xml的第二次迭代中使用多个元素,如下所示:Ship 2感谢Michael的快速响应-但我没有其他问题。请允许我知道下面的copy命令是否对应于input和output元素
xsl:copy
执行当前节点的浅层复制-在我的示例中,
Lots
Lot
都被复制了。感谢Michael的快速回复-但我还有一些问题。请告诉我下面的复制命令是否对应于输入和输出元素。这是因为当我将此示例用于输入和输出具有不同标记名的实际代码时,副本会创建一个输出中不需要的输入元素标记。如果此注释不清晰,请告知预期的输出标记和实际结果不一致;不匹配。有什么想法吗?恐怕这不是进行XSLT教程的好地方。我复制了root
Lots
元素,以便生成格式良好的XML结果。如果不需要,请删除第一条
xsl:copy
指令。同样地,您可以将
替换为
,以获得所需的结果。感谢Michael的快速响应-但我没有其他问题。请告知下面的copy命令是否对应于输入和输出元素
xsl:copy
执行当前节点的浅层复制-在我的例子,
Lots
Lot
都被复制了。感谢Michael的快速回复-但我还有一些问题。请告诉我下面的复制命令是否对应于输入和输出元素。这是因为当我将此示例用于输入和输出具有不同标记名的实际代码时,副本会创建一个输出中不需要的输入元素标记。如果此注释不清晰,请告知预期的输出标记和实际结果不一致;不匹配。有什么想法吗?恐怕这不是进行XSLT教程的好地方。我已经复制了根
批次