Xslt transform on special characters

Tag: xslt Author: xinz0526 Date: 2009-10-04

I have an XML document that needs to pass text inside an element with an '&' in it. This is called from .NET to a Web Service and comes over the wire with the correct encoding &

e.g.

T&O

I then need to use XSLT to create a transform but need to query SQL server through a SP without the encoding on the Ampersand e.g T&O would go to the DB.

(Note this all has to be done through XSLT, I do have the choice to use .NET encoding at this point)

Anyone have any idea how to do this from XSLT?

Note my XSLT knowledge isn’t the best to say the least!

Cheers

Best Answer

If you have the choice to use .NET you can convert between an HTML-encoded and regular string using (this code requires a reference to System.Web):

string htmlEncodedText = System.Web.HttpUtility.HtmlEncode("T&O");
string text = System.Web.HttpUtility.HtmlDecode(htmlEncodedText);

Update

Since you need to do this in plain XSLT you can use xsl:value-of to decode the HTML encoding:

<xsl:variable name="test">
    <xsl:value-of select="'T&amp;O'"/>
</xsl:variable>

The variable string($test) will have the value T&O. You can pass this variable as an argument to your extension function then.

comments:

Hi, Sorry that was a typo on my behalf, I don’t have the option to use .NET, I would have done what you suggested otherwise :) Thanks

Other Answer1

<xsl:text disable-output-escaping="yes">&amp;<!--&--></xsl:text>

More info at: http://www.w3schools.com/xsl/el_text.asp

comments:

You have to write it as &amp; in the XSLT source, and it produces invalid XML, but apart from this it works just like <xsl:output/>.
My XML source document is coming in from BizTalk so it needs to be a valid XML document hence the encoding of the Ampersand. Before I create my output document I need to query the DB based on what was in the source document without the encoding in it. <submissiontitle>T&amp;O</submissiontitle> If I use the <xsl:text disable-output-escaping="yes">&</xsl:text> can this be used on the whole of the stylesheet, I was thinking of creating a variable as I call the DB thru a SP in many places during the transform? But need to have the correct value of T&O in this case.

Other Answer2

Supposing your XML looks like this:

<root>T&amp;O</root>

you can use this XSLT snippet to get the text out of it:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text" />

  <xsl:template match="root"> <!-- Select the root element... -->
    <xsl:value-of select="." /> <!-- ...and extract all text from it -->
  </xsl:template>

</xsl:stylesheet>

Output (from Saxon 9, that is):

T&O

The point is the <xsl:output/> element. The defauklt would be to output XML, where the ampersand would still be encoded.