1
votes

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&amp;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.

2
Please select either XSLT 1.0 or 2.0, not both. Your question is also tagged with Google Chrome; are you using a browser to do the transformation? - michael.hor257k
I am using chrome and FireFox - Abadi
Then you're not using XSLT 2.0. And I don't see why the (unescaped) result of AT&amp;T is a problem: any browser will render it to screen as AT&T without you having to do anything (other than fix your stylesheet, as explained in the comment given by Abel below). - michael.hor257k
The result will be displayed in csv file and not in the browsers - Abadi
That is confusing. If you're "using chrome and FireFox", how will the result "be displayed in csv file"? And if you want a csv file as the result, why don't you set the output method to text - which will solve the escaping problem? - michael.hor257k

2 Answers

1
votes

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&amp;T. Any XML having AT&amp;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.

0
votes

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?).