xsl trying to ouput '<' as opposed to '<'

你离开我真会死。 提交于 2019-12-03 15:05:57
<xsl:text disable-output-escaping="yes"><![CDATA[<]]></xsl:text>

This is because < is illegal (and that's why your app is complaining). For >, you can use:

<xsl:text disable-output-escaping="yes">></xsl:text>

The original problem

An example of the input:

&amp;lt;p&amp;gt;&amp;lt;span style=&amp;quot;font-size: medium&amp;quot;&amp;gt;Product description text&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;

Intended output:

<p><span style="font-size: medium">Product description text</span></p>

What you really seem to have is double escaped input so instead of just simple outputting the text with disable-output-escaping="yes" you need some ways to do string replacing. Clearly it seems that you do the replacing with ew:replacestring() extension function. The problem is that even if you manage to replace &amp;lt; with <, the result is still only a string and the < character gets escaped on output.

Your original replacement text code was not well-formed because < as such is seen as markup. Using

<xsl:variable name="lt">
  <xsl:text><![CDATA[<]]></xsl:text>
</xsl:variable>

or

<xsl:variable name="lt">
  <xsl:text>&lt;</xsl:text>
</xsl:variable>

are equivalent and fix that problem. However this doesn't help in the problem that the replacement character still is outputted as an entity reference. Using disable-output-escaping="yes" here in the variable definition has no effect on that problem either.

<xsl:variable name="lt">
  <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
</xsl:variable>

<xsl:variable name="lt">
  <xsl:value-of select="&lt;" disable-output-escaping="yes">
</xsl:variable>

These code examples don't work because disable-output-escaping="yes" only has an effect when the value is serialized, not when a variable gets assigned.

A possible fix

What you could try to do is to store the result of your text replacing into a variable

<xsl:variable name="replaced-text">
    <xsl:copy-of select="ew:replacestring(products_description/node(),$lthex,$lt)"/>
</xsl:variable>

and then output the value of this variable with disable-output-escaping="yes"

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

Another solution without extension functions

I was able to transform your input

&amp;lt;p&amp;gt;&amp;lt;span style=&amp;quot;font-size: medium&amp;quot;&amp;gt;Product description text&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;

to this output

<p><span style="font-size: medium">Product description text</span></p>

with a slightly modified version of Jeni Tennison's acronym replacement template. In your case the document containing the acronym titles would look like this

<acronyms>
    <acronym acronym="&amp;lt;">&lt;</acronym>
    <acronym acronym="&amp;gt;">></acronym>
    <acronym acronym="&amp;amp;">&amp;</acronym>
    <acronym acronym="&amp;apos;">'</acronym>
    <acronym acronym="&amp;quot;">"</acronym>
</acronyms>

The required modification would be to change this part of the code example

<acronym title="{$acronyms[1]}">
    <xsl:value-of select="$acronym" />
</acronym>

to look like this

<xsl:value-of select="$acronyms[1]" disable-output-escaping="yes"/>

That template should work in every environment because it is XSLT 1.0 and it doesn't require any extension functions.

Christoffer Hammarström

Edit: It looks like you're trying to take a serialization of an XML or HTML serialization as input instead of an XML or HTML serialization, and expect it to output an XML or HTML serialization?

The correct thing would be to fix your broken input, but see How to unescape XML characters with help of XSLT?


You have some kind of misunderstanding here. XSL takes XML as input and outputs HTML or XML. < is represented as &lt; in serialized HTML and XML.

An HTML or XML file that contains a < that does not start a tag is an incorrect serialization (not wellformed).

&lt; is the correct representation of < in an HTML or XML file.

An HTML or XML file is only a serialization of the data it represents. That is, it's one level below the data it represents. You should think of the contents of the HTML or XML as what the data represents (<), not as the serialization of the data (&lt;).

Try instead to explain why you think having &lt; is a problem, so we can correct the misunderstanding?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!