Xslt transform on special characters

2019-08-19 09:02发布

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

标签: xslt
3条回答
劳资没心,怎么记你
2楼-- · 2019-08-19 09:21

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.

查看更多
▲ chillily
3楼-- · 2019-08-19 09:29
<xsl:text disable-output-escaping="yes">&amp;<!--&--></xsl:text>

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

查看更多
Melony?
4楼-- · 2019-08-19 09:43

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.

查看更多
登录 后发表回答