1

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.

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
Abadi
  • 71
  • 2
  • 9
  • 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 Sep 02 '15 at 08:48
  • I am using chrome and FireFox – Abadi Sep 02 '15 at 08:50
  • Then you're not using XSLT 2.0. And I don't see why the (unescaped) result of `AT&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 Sep 02 '15 at 09:17
  • The result will be displayed in csv file and not in the browsers – Abadi Sep 02 '15 at 10:41
  • 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 Sep 02 '15 at 11:22
  • did not work.The result will be written to csv file in javascript side – Abadi Sep 02 '15 at 11:35
  • It works for me: http://xsltransform.net/jyH9rNp -- "*The result will be written to csv file in javascript side*" That's not related to XSLT. – michael.hor257k Sep 02 '15 at 11:40
  • @michael.hor257k: I think what he means to say is that his output is HTML (see comment under Michael Kay's answer). If my (or Michael's) answer doesn't help, Abadi should provide us with a more complete example, as to the best of my knowledge, these answers solve the issue (if it needs solving to begin with). See also [mcve]. – Abel Sep 02 '15 at 11:47
  • Abadi, the link you provide does not contain valid XSLT. Can you use http://xsltransform.net, or better yet, provide us with a simple example _inside your question, one that runs and that we can repeat_. – Abel Sep 02 '15 at 11:49
  • It is working on http://xslt.online-toolz.com/tools/xslt-transformation.php and xsltransform.net,.But there is a problem on chrome and FireFox.I will supply more details – Abadi Sep 02 '15 at 11:58
  • I added this link that contains the xml document,xsl style sheet and the result after applying it on http://xsltransform.net/.Here is the link:http://jsfiddle.net/Abadi/mx9fyt3j/ – Abadi Sep 02 '15 at 12:36
  • It is working on http://xsltransform.net/,but on chrome,the resulting document contains:AT&T – Abadi Sep 02 '15 at 12:37
  • The xsl style sheet that I provided is working,csv file is generated successfully except the problem in the character escape. – Abadi Sep 02 '15 at 12:39

2 Answers2

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

Abel
  • 56,041
  • 24
  • 146
  • 247
  • I need help in placing your code in the xsl document to test it. – Abadi Sep 02 '15 at 07:57
  • @Abadi, that will be difficult, as currently your stylesheet is illegal and cannot possibly run (you declare the xsl prefix twice and use the xsl prefix for output elements). But you already do have the `match="/"` in the top, so after you fix it, wrap that in a variable and apply templates on the result, as I showed you above. – Abel Sep 02 '15 at 08:06
0

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

Michael Kay
  • 156,231
  • 11
  • 92
  • 164