有条件地使用xslt将boolean属性添加到标记

时间:2014-02-27 19:29:38

标签: xslt

无条件地我会写,比方说,

    <season boolean:warm="true">

我会通过xslt有条件地设置属性,但我不知道使用什么语法

    <season>
      <xsl:if ...>
        <xsl:attribute name="warm">
         true
        </xsl:attribute>
      </xsl:if>
    </season>

使用此xslt结果的程序无法处理此变体

1 个答案:

答案 0 :(得分:0)

如果要在XSLT中使用名称空间,则必须声明它们。让我们假设你的来源是这样的:

<months>
    <month>January</month>
    <month>August</month>
</months>

由于属性中需要前缀,因此必须声明命名空间。您没有显示命名空间,因此我将使用随机字符串。下面的XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:boolean="put-your-namespace-here">

    <xsl:template match="months">
        <seasons><xsl:apply-templates/></seasons>
    </xsl:template>
    <xsl:template match="month">
        <season>
            <xsl:if test="text() = 'August'">
                <xsl:attribute name="boolean:warm">true</xsl:attribute>
            </xsl:if>
            <xsl:value-of select="."/>
        </season>
    </xsl:template>
</xsl:stylesheet>

将产生以下结果:

<seasons xmlns:boolean="put-your-namespace-here">
    <season>January</season>
    <season boolean:warm="true">August</season>
</seasons>