xml 如何在 XSLT 中使用 <![CDATA[]]>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25400067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:04:58  来源:igfitidea点击:

How to use <![CDATA[]]> in XSLT?

xmlxslt

提问by Jatin Gadhiya

I have use in XSLT variable look like:

我在 XSLT 变量中使用过如下所示:

<xsl:variable name="ShortDescription" select="str[@name = 'ShortDescription']"/>
<!--Short Description field-->
       <xsl:if test="$ShortDescription !=''">

        <shortdescription><![CDATA[ <xsl:value-of select="$ShortDescription" disable-output-escaping="no"/> ]]></shortdescription>

       </xsl:if>

But its display as a text not a value of $ShortDescriptionvariable and output look like:

但它显示为文本而不是$ShortDescription变量的值,输出如下所示:

<shortdescription>
&lt;xsl:value-of select="$ShortDescription" disable-output-escaping="no"/&gt;
</shortdescription>

expected output look like:

预期输出如下:

<shortdescription>
<![CDATA[Maximum Power: 520 W<br/>+12V Rails: Dual<br/>]]>
</shortdescription>

How to use <![CDATA[]]>in XSLT?

如何<![CDATA[]]>在 XSLT 中使用?

回答by Martin Honnen

XSLT is XML so of course you can use a CDATA section in XSLT code, as you have done. However it rather seems you want the output of the XSLT code to contain a CDATA section for the shortdescriptioncontents, in that case you need

XSLT 是 XML,因此您当然可以在 XSLT 代码中使用 CDATA 部分,就像您所做的那样。但是,您似乎希望 XSLT 代码的输出包含内容的 CDATA 部分shortdescription,在这种情况下,您需要

<xsl:output method="xml" cdata-section-elements="shortdescription"/>

And the XSLT would simply stay as

并且 XSLT 将简单地保持为

<shortdescription><xsl:value-of select="$ShortDescription"/></shortdescription>