XSLT1.0-每个都有一个选择变量,但没有给出正确的结果



输入:

<?xml version="1.0" encoding="UTF-8"?>
<Project ID="123">
<ProductPool>
<Product Type="A" ID="123" DueDate="123" Name="ABC"/>
<Product Type="B" ID="123" DueDate="123" Name="ABC"/>
<Product Type="A" ID="123" DueDate="123" Name="ABC"/>
<Product Type="B" ID="123" DueDate="123" Name="ABC"/>
<Product Type="A" ID="123" DueDate="123" Name="ABC"/>   
</ProductPool>
</Project>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:element name="Product">
<xsl:attribute name="ID">
<xsl:value-of select="//Project/@ID"/>
</xsl:attribute>
<xsl:variable name="elem-name">
<xsl:choose>
<xsl:when test='//Product[@Type="A"]'>BoundComponent</xsl:when>
<xsl:otherwise>UnboundComponent</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:for-each select="//Product">
<xsl:element name="{$elem-name}">
<xsl:attribute name="ID">
<xsl:value-of select="@Name"/>
</xsl:attribute>
<xsl:attribute name="DueDate">
<xsl:value-of select="@DueDate"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

输出:

<?xml version="1.0"?>
<Product ID="123">
<BoundComponent ID="ABC" Duedate="123"/>
<BoundComponent ID="ABC" Duedate="123"/>
<BoundComponent ID="ABC" Duedate="123"/>
<BoundComponent ID="ABC" Duedate="123"/>
<BoundComponent ID="ABC" Duedate="123"/>
</Product>

由于某种原因,只要存在一个类型="0";A";在整个输入中找到,所有元素都被称为BoundComponent,但我的目标是分离输出,所以找到的每个产品都是一个名为Bound或Unbound的元素,这取决于它在输入中的类型,我不明白为什么,因为它已经是每个循环的一个了。有什么想法吗?

您的$elem-name变量被绑定到循环外的值。如果将xsl:variable放入循环中,它将绑定到每个不同的Product的适当值。即

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:element name="Product">
<xsl:attribute name="ID">
<xsl:value-of select="//Project/@ID"/>
</xsl:attribute>
<xsl:for-each select="//Product">
<xsl:variable name="elem-name">
<xsl:choose>
<xsl:when test='@Type="A"'>BoundComponent</xsl:when>
<xsl:otherwise>UnboundComponent</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$elem-name}">
<xsl:attribute name="ID">
<xsl:value-of select="@Name"/>
</xsl:attribute>
<xsl:attribute name="DueDate">
<xsl:value-of select="@DueDate"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

相关内容

最新更新