问题
I have the following xsl document: http://jsfiddle.net/Abadi/92ndrnut/2/. I need to add : disable-output-escaping="yes". Because when it is applied on:
<CARRIERNAME>
<![CDATA[AT&T]]>
</CARRIERNAME>.
It is producing :AT&T
.
The applied template was:
<xsl:value-of select="CARRIERNAME" />
.
When I updated it to :
<xsl:value-of select="CARRIERNAME" disable-output-escaping="yes" />
,
it worked and the output was :AT&T
.
My problem is how to add disable-output-escaping="yes" to the xsl document so that it will be applied in all templates.
The change should take place in the xsl documnet provided in the link. Or if there might be a different approach. I am new to xslt and I would appreciate your help.
回答1:
My problem is how to add disable-output-escaping="yes" to the xsl document so that it will be applied in all templates.
This is a feature of XSLT 2.0, where disable-output-escaping
has been considered deprecated and replaced by xsl:character-maps
. These character maps can be applied to the whole output.
Note that <![CDATA[AT&T]]>
is the same as AT&T
. Any XML having AT&T
will be displayed in a client as AT&T
, because it is merely a way of escaping the &
. Forcing the &
to not be escaped makes the resulting XML invalid XML. If HTML is your output, then in some cases this kind of escaping is required (i.e. in script
elements).
A workaround you can use in XSLT 1.0 is as follows. Assuming your entry point is where you start at the root node:
<xsl:template match="/">
<!-- your code here -->
</xsl:template>
Replace that with:
<xsl:template match="/">
<xsl:variable name="pre-process">
<!-- your code here -->
<xsl:variable>
<xsl:apply-templates select="exslt:node-set($pre-process)" mode="escape"/>
</xsl:template>
<xsl:template match="@* | *" mode="escape">
<xsl:copy>
<xsl:apply-templates select="node()" mode="escape"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text()" mode="escape">
<xsl:value-of select="." disable-output-escaping="yes" />
</xsl:template>
The above code simply re-processes everything and specificially processes text nodes to be escaped (the only nodes to matter when it comes down to escaping). The code depends on the availability of the extension function exslt:node-set, but just about every XSLT 1.0 processor supports it.
A few comments on the code provided in the link:
<xsl:if test="following-sibling::*"> <xsl:text></xsl:text> </xsl:if>
This has no effect.
<xsl:sort> <xsl:attribute name="select"><xsl:value-of select="meta_data//bindto"/></xsl:attribute> <xsl:attribute name="data-type"><xsl:value-of select="meta_data//sortby_type"/></xsl:attribute> <xsl:attribute name="order"><xsl:value-of select="meta_data//direction"/></xsl:attribute> </xsl:sort>
This has no effect (sorting attributes is meaningless, as attributes are always output in any order preferred by the processor).
<xsl:text><xsl:value-of select="display_precision"/></xsl:text>
This is illegal, if you still have this, you will not be able to run your stylesheet.
回答2:
It's not clear why you want to disable output escaping. If you are producing XML or HTML, the ampersand needs to be escaped for the XML or HTML to be valid. If you are producing something else (text), you should be using the text output method rather than the XML or HTML output method. Perhaps all you need is <xsl:output method="text"/>
90% of the time if beginners use disable-output-escaping
it is because they haven't found the right way to achieve the effect they are looking for. Unfortunately though you haven't really explained what you are trying to achieve (like, what is the output of your transformation?).
来源:https://stackoverflow.com/questions/32345602/how-to-add-disable-output-escaping-yes-to-my-xsl-document