Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top