xsl:从元素到属性的决策值

时间:2009-12-16 17:48:30

标签: xml xslt

我想使用XSL将XML转换为另一种XML

输入xml包含以下元素

<ViewSideIndicator>0</ViewSideIndicator>

需要转换为以下

<ImageViewDetail ViewSideIndicator="Front"/>

在输入文件中,如果值为“0”,则输出中应为“Front” 如果值为“1”,那么它应该在输出中“返回”

我知道我们可以使用<xsl:choose>根据决定制作价值,但我不知道如何为这种情况做这件事。

2 个答案:

答案 0 :(得分:1)

在模板中(假设当前源上下文是ViewSideIndicator元素):

<ImageViewDetail>
    <xsl:attribute name="ViewSideIndicator">
        <xsl:choose>
            <xsl:when test="text()='0'">Front</xsl:when>
            <xsl:when test="text()='1'">Back</xsl:when>
        </xsl:choose>
    </xsl:attribute>
</ImageViewDetail>

答案 1 :(得分:0)

你的意思是这样(或者它的一个版本作为模板)吗?

<ImageViewDetail>
    <xsl:choose>
        <xsl:when test="ViewSideIndicator=0">
            <xsl:attribute name="ViewSideIndicator">Front Gray</xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
            <xsl:attribute name="ViewSideIndicator">Back Gray</xsl:attribute>
        </xsl:otherwise>
    </xsl:choose>
</ImageViewDetail>
相关问题