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…
Include this 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-attributesfeature as mentioned by M.Kay .