Storing html tags within an xsl variable

2020-04-19 19:04发布

Sorry if this is a dumb question, but it is possible to store, and retrieve, a HTML snippet within an xsl 1.0 variable? EG:

<xsl:variable name="something"><p>Hi there</p><p>How are you today?</p></xsl:variable>

<xsl:value-of disable-output-escaping="yes" select="$something"/>

It just when I try, it seems to strip the HTML tags out. Thanks.

标签: xslt xslt-1.0
2条回答
趁早两清
2楼-- · 2020-04-19 19:41

You need to use <xsl:copy-of select="$something"/> instead of xsl:value-of.

查看更多
SAY GOODBYE
3楼-- · 2020-04-19 19:59

I'll add some explanation of what's happening :)

The reason you're not getting the html tags is that the $something variable contains a dom fragment, not a string: the value-of element extracts the content of the node(s) the same way as the string() function does, so does not serialize the nodes.

This would provide, instead, a string representation of the html string you have and you can then print it out with value-of and disable-output-escaping:

<xsl:variable name="something"><![CDATA[<p>Hi there</p><p>How are you today?</p>]]></xsl:variable>

(see https://msdn.microsoft.com/en-us/library/ms256181(v=vs.110).aspx "The results are converted to a string, as by a call to the string() function")

查看更多
登录 后发表回答