XSL值作为属性

时间:2017-02-14 11:55:11

标签: xml xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:variable name="cdataStart"><![CDATA[ <![CDATA ]]></xsl:variable>
<xsl:variable name="cdataEnd"><![CDATA[ ]] ]]></xsl:variable>
<xsl:template match="/Prekes">
    <products>
        <xsl:for-each select="./product">
            <product>
                    <associations>
                        <association external-reference="<xsl:value-of select="./code" />">
                            <mode>replace</mode>
                        </association>
                    </associations>
                </block>
            </product>
        </xsl:for-each>
    </products>
</xsl:template>

我需要将我的值放入外部引用的属性中。问题是,如果我尝试像这样使用它,我会收到语法错误。我怎样才能正确地插入它?

1 个答案:

答案 0 :(得分:3)

您需要在这里使用Attribute Value Templates ...

 <association external-reference="{code}">

花括号表示要计算的表达式,而不是字面输出。

请注意,您也可以在此使用xsl:attribute

<association>
   <xsl:attribute name="external-reference">
      <xsl:value-of select="./code" />
   </xsl:attribute>

但是正如您所看到的,属性值模板更加简洁。例如,如果您希望有条件地创建属性,则可以使用xsl:attribute