2

I am trying to generate URLs from a base XML file via XSLT using Saxon 9. However, the resulting URLs are to be used for a GET HTTP request (executed via Java, following the generation), and will contain ampersands ('&') to delimit parametres.

I wrote the XSLT to make the conversion, which worked when using Eclipse to test.

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

However, running the same XSLT file using Saxon with Java, it doesn't behave the same. It instead inserts the escape code &amp; into the generated URL.

I tried using an XSLT variable instead, but the result was the same. Neither of the following attempts worked.

<xsl:variable name="and"><![CDATA[&]]></xsl:variable>

<xsl:variable name="and" select="'&'" />

I also saw that a solution for C# is to have a variable with the value &amp;amp;, but this did nothing in Java other than to insert that text literally into the generated URL.

So my question is : is it possible to have an unescaped ampersand generated via XSLT? Or will I have to generate an escaped character string and then do a substitution later in Java?

And if it is not possible (due to XML not allowing unescaped ampersands), how is it that Eclipse could generate it, whereas Saxon cannot?

2
  • 1
    Please tell us exactly what kind of result you are trying to create with Saxon, is that a plain text document (output method="text") or HTML document (output method="html") or an XML document (output method="xml")? Inside HTML or XML the ampersand needs to be escaped. Commented Dec 6, 2013 at 10:50
  • Sorry, I forgot to specify that. The result will be an XML document. And yes, I know that normally ampersands need to be escaped. It's why I asked why Eclipse, using the same XSLT document, would generate non-escaped ampersands. Does it use a different XSL engine, or does it not adhere to the XML standards? Commented Dec 6, 2013 at 10:56

1 Answer 1

3

is it possible to have an unescaped ampersand generated via XSLT?

Not without using disable-output-escaping (and that can only work when the XSLT engine is responsible for serializing the output XML tree, it will be ignored when the output is a DOM, for example), and if you could then the output you generate wouldn't be XML and your downstream components wouldn't be able to parse it.

But it shouldn't be necessary anyway, because if you have an XML document

<url>http://example.com/query?year=2013&amp;month=12</url>

then when you read the document with an XML parser the value you will get out for the url element is the unescaped http://example.com/query?year=2013&month=12. You don't need to care how the url is represented in the XML serialization, the value you get out of the parser will be the one you expect.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. I didn't know that the XML parser would translate automatically, since when I displayed the whole Node using the toString() method, it displayed the ampersands as &amp;. But getting the child and then the text content, it displays the ampersand as I want it. Thanks again.
As Ian says, the correct representation of an ampersand in XML is &amp;, whether it's in a URI or anywhere else. Disabling escaping will produce an unescaped '&' if the output is produced using the Saxon serializer, but the unescaped '&' will generally cause errors downstream unless the format you want isn't really XML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.