tags:

views:

335

answers:

3

Hi Guys,

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

A: 

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.

0xA3
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
Conor
A: 

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"&gt;

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

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

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

Ivan Zlatanov
Boldewyn
Conor