Cannot get xslt to output an (&) even after escapi

2020-02-10 12:28发布

I am trying to create a query string of variable assignments separated by the & symbol (ex: "var1=x&var2=y&..."). I plan to pass this string into an embedded flash file.

I am having trouble getting an & symbol to show up in XSLT. If I just type & with no tags around it, there is a problem rendering the XSLT document. If I type &amp; with no tags around it, then the output of the document is &amp; with no change. If I type <xsl:value-of select="&" /> or <xsl:value-of select="&amp;" /> I also get an error. Is this possible? Note: I have also tried &amp;amp; with no success.

13条回答
乱世女痞
2楼-- · 2020-02-10 12:39

If your transform is emitting an XML document, you shouldn't disable output escaping. You want markup characters to be escaped in the output, so that you don't emit malformed XML. The XML object that you're processing the output with (e.g. a DOM) will unescape the text for you.

If you're using string manipulation instead of an XML object to process the output, you have a problem. But the problem's not with your XSLT, it's with the decision to use string manipulation to process XML, which is almost invariably a bad one.

If your transform is emitting HTML or text (and you've set the output type on the <xsl:output> element, right?), it's a different story. Then it's appropriate to use disable-output-escaping='yes' on your <xsl:value-of> element.

But in any case, you'll need to escape the markup characters in your XSLT's text nodes, unless you've wrapped the text in a CDATA section.

查看更多
欢心
3楼-- · 2020-02-10 12:40

org.apache.xalan.xslt.Process, version 2.7.2 outputs to the "Here is a runnable, short and complete demo how to produce such URL" mentioned above:

<?xml version="1.0" encoding="UTF-8"?>http://www.myUrl.com/?vA=a&amp;vX=x&amp;vY=y&amp;vZ=z11522

The XML declaration is suppressed with an additional omit-xml-declaration="yes", but however with output method="text" escaping the ampersands is not justifiable.

查看更多
等我变得足够好
4楼-- · 2020-02-10 12:41

If you are trying to produce an XML file as output, you will want to produce &amp; (as & on it's own is invalid XML). If you are just producing a string then you should set the output mode of the stylesheet to text by including the following as a child of the xsl:stylesheet

<xsl:output method="text"/>

This will prevent the stylesheet from escaping things and <xsl:value-of select="'&amp;'" /> should produce &.

查看更多
乱世女痞
5楼-- · 2020-02-10 12:46

Use disable-output-escaping="yes" in your value-of tag

查看更多
Animai°情兽
6楼-- · 2020-02-10 12:46

Using the disable-output-escaping attribute (a boolean) is probably the easiest way of accomplishing this. Notice that you can use this not only on <xsl:value-of/> but also with <xsl:text>, which might be cleaner, depending on your specific case.

Here's the relevant part of the specification: http://www.w3.org/TR/xslt#disable-output-escaping

查看更多
姐就是有狂的资本
7楼-- · 2020-02-10 12:46

try: <xsl:value-of select="&amp;" disable-output-escaping="yes"/>

Sorry if the formatting is messed up.

查看更多
登录 后发表回答