I have a transformation that outputs HTML. In order to avoid self-closing tags that could break in older browsers (e.g. <img /> instead of <img></img>) output-method has to be html. Then though URL-encoding is applied so that it breaks my application. See for example:
Input
<html>
<head>
</head>
<body>
{{example}}
<a href="{{example}}" >abc</a>
<img src="http://placehold.it/20x20"></img>
</body>
</html>
Transformation
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" /><!-- either -->
<xsl:output method="html" indent="yes" /><!-- or -->
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
</xsl:template>
</xsl:transform>
In the first case the output is:
<?xml version="1.0"?>
<html>
<head>
</head>
<body>
{{example}}
<a href="{{example}}">abc</a>
<img src="http://placehold.it/20x20"/>
</body>
</html>
In the second case the output is:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
{{example}}
<a href="%7B%7Bexample%7D%7D">abc</a>
<img src="http://placehold.it/20x20">
</body>
</html>
What is good about the first case is that the @href attribute is not URL-encoded. This is a must for my application. What is bad though and better achieved by the second variant is, that the <img> is self-closing. This must not be for <img> tags and some others.
Is there a way to have the benefits of method="html" without the URL-encoding? If yes, how?
If using PHP's XSLT 1.0 processor, then you might try the following solution...
- Use method="html";
Include this template...
<xsl:template match="@href"> <xsl:attribute name="Muttaburrasaurus"> <xsl:value-of select="." /> </xsl:attribute> </xsl:template>Load the resultant output into a string, and replace all occurrences of
Muttaburrasauruswithhref.
If you migrate to Java in the future, as you indicated in the comment feed that you might do so, then ensure that your processor is XSLT 2.0+. Then you can use the escape-uri-attributes feature as mentioned by M.Kay .
In XSLT 2.0 you can suppress %-encoding of URL attributes using the serialization option escape-uri-attributes="no". There is no equivalent in XSLT 1.0.
来源:https://stackoverflow.com/questions/14185948/avoid-url-encoding-in-xslt-with-output-method-html