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 "&" with no tags around it then the output of the document is "&" with no change. If I type <xsl:value-of select="&" /> or <xsl:value-of select="&" /> I also get an error. Is this possible? Note: I have also tried "&amp;" with no success.
views:
1817answers:
9try: <xsl:value-of select="&" disable-output-escaping="yes"/>
Sorry if the formatting is messed up.
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 spec: http://www.w3.org/TR/xslt#disable-output-escaping
If you are creating a query string as part of a larger URL in an attribute of some other tag (like "embed"), then you actually want the & to be escaped as &. While all browsers will figure out what you mean and Do The Right Thing, if you were to pass your generated doc to a validator it would flag the un-escaped & in the attribute value.
Are you expressing the URI in HTML or XHTML? e.g. <tag attr="http://foo.bar/?key=value&amp;key2=value2&amp;..."/>
If so, "&
" is the correct way to express an ampersand in an attribute value, even if it looks different from than literal URI you want. Browsers will decode "&
" and any other character entity reference before either loading them or passing them to Flash. To embed a literal, lone "&
" directly in HTML or XHTML is incorrect.
I also personally recommend learning more about XML in order to think about these kinds of things in a clearer way. For instance, try using the W3C DOM more (for more than just trivial Javascript); even if you don't use it day-to-day, learning the DOM helped me think about the tree structure of XML correctly and how to think about correctly encoding attributes and elements.
If you are trying to produce an xml file as output, you will want to produce &
(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="&" />
should produce &
You should note, that you can use disable-output-escaping within the value of node or string/text like:
<xsl:value-of select="/node/here" disable-output-escaping="yes" />
or
<xsl:value-of select="'&'" disable-output-escaping="yes" />
<xsl:text disable-output-escaping="yes">Texas A&M</xsl:text>
Note the single quotes in the xsl:value-of.
However you cannot use disable-output-escaping on attributes. I know it's completely messed up but, that's the way things are in XSLT 1.0. So the following will NOT work:
<xsl:value-of select="/node/here/@ttribute" disable-output-escaping="yes" />
Because in the fine print is the quote:
Thus, it is an error to disable output escaping for an or element that is used to generate the string-value of a comment, processing instruction or attribute node;
emphasis mine.
Taken from: http://www.w3.org/TR/xslt#disable-output-escaping
You can combine disable-output-escaping with a CDATA section. Try this:
<xsl:text disable-output-escaping="yes"><![CDATA[&]]></xsl:text>
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.