使用输出法html避免XSLT中的URL编码 [英] Avoid URL-encoding in XSLT with output method html

查看:118
本文介绍了使用输出法html避免XSLT中的URL编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输出HTML的转换。为了避免旧版浏览器中可能会自动关闭的标签(例如< img /> ,而不是< img>< img> )output-method必须是 html 。然后,虽然URL编码被应用,所以它打破了我的应用程序。例如:

输入

 < HTML> 
< head>
< / head>
< body>
{{example}}
< a href ={{example}}> abc< / a>
< img src =http://placehold.it/20x20>< / img>
< / body>
< / html>

转型

 <?xml version =1.0encoding =utf-8?> 
< xsl:output method =xmlindent =yes/><! - either - >
< xsl:output method =htmlindent =yes/><! - 或 - >
< xsl:template match =@ * | node()>
< xsl:copy>< xsl:apply-templates select =@ * | node()/>< / xsl:copy>
< / xsl:template>
< / xsl:transform>

在第一种情况下,输出为:

 <?xml version =1.0?> 
< html>
< head>
< / head>
< body>
{{example}}
< a href ={{example}}> abc< / a>
< img src =http://placehold.it/20x20/>
< / body>
< / html>

在第二种情况下,输出为:

 < HTML> 
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< / head>
< body>
{{example}}
< a href =%7B%7Bexample%7D%7D> abc< / a>
< img src =http://placehold.it/20x20>
< / body>
< / html>

第一种情况的好处在于 @href 属性是不是URL编码的。这是我的申请必须的。什么是坏的,而第二个变体更好地实现的是,< img> 是自闭的。这不能用于< img> 标签和其他一些标签。 有没有一种方法可以获得好处 method =html没有URL编码?如果是,如何使用PHP的XSLT 1.0处理器,那么你可以尝试下面的解决方案...

>


  1. 使用method =html;

  2. 包含此模板...

     < xsl:template match =@ href> 
    < / xsl:attribute>
    < / xsl:template>


  3. 将结果输出加载到一个字符串中,并替换所有出现的 Muttaburrasaurus with href


如果您将来迁移到Java,如您在评论Feed中指出的那样,那么确保您的处理器是XSLT 2.0+。然后你可以使用M.Kay提到的 escape-uri-attributes 功能。

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...

  1. Use method="html";
  2. Include this template...

    <xsl:template match="@href">
      <xsl:attribute name="Muttaburrasaurus">
        <xsl:value-of select="." />
      </xsl:attribute>
    </xsl:template>
    

  3. Load the resultant output into a string, and replace all occurrences of Muttaburrasaurus with href.

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 .

这篇关于使用输出法html避免XSLT中的URL编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆