【发布时间】:2011-04-01 09:32:28
【问题描述】:
我有一个受 XSLT 约束的 XML 文档。结构类似于:
<root>
<item value="1">
<object/>
</item>
<item value="2" />
<object/>
</item>
</root>
我的目标是最终得到一个转换后的 XML,类似于:
<root>
<parent>
<object-one value-one="1"/>
</parent>
<parent>
<object-two value-two="2"/>
</parent>
</root>
我的 XSLT 类似于:
<xsl:apply-templates select="object" />
<xsl:template match="object">
<xsl:call-template name="1" />
<xsl:call-template name="2" />
</xsl:template>
<xsl:template name="1" match="object[item/@value = '1'">
<xsl:element name="object-one" namespace="http://something.org">
<xsl:attribute name="_Description">
<xsl:value-of select="@_Type"/>
</xsl:attribute>
<xsl:attribute name="_Type">
<xsl:value-of select="@_Amount"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
<xsl:template name="2" match="object[item/@value = '2'">
<xsl:element name="object-two" namespace="http://something.org">
<xsl:attribute name="OriginalAmount">
<xsl:value-of select="@_Amount"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
问题是所有项目节点都应用了相同的模板。如何将模板仅应用于特定节点?
【问题讨论】: