有没有办法禁用lxml中锚点属性的URL编码?

5 投票
2 回答
2062 浏览
提问于 2025-04-16 09:55

我正在使用 lxml 2.2.8,想把一些现有的 HTML 文件转换成 Django 模板。现在我遇到的唯一问题是,lxml 会对锚点名称和链接属性进行 URL 编码。

<xsl:template match="a">
<!-- anchor attribute href is urlencoded but the title is escaped -->
<a href="{{{{item.get_absolute_url}}}}" title="{{{{item.title}}}}">
    <!-- name tag is urlencoded -->
    <xsl:attribute name="name">{{item.name}}</xsl:attribute>
    <!-- but other attributes are not -->
    <xsl:attribute name="nid">{{item.nid}}</xsl:attribute>
    <xsl:attribute name="class">{{item.class_one}}</xsl:attribute>
    <xsl:apply-templates/>
</a>

这样生成的 HTML 看起来是这样的:

<a href="%7B%7Bitem.get_absolute_url%7D%7D"  
   title="{{item.title}}" name="%7B%7Bitem.name%7D%7D" 
   nid="{{item.nid}}" class="{{item.class_one}}">more info</a>

而我想要的是这样的:

<a href="{{item.get_absolute_url}}">more info</a>

有没有办法禁用 lxml 自动进行的 URL 编码呢?

以下是我用来生成和解析文件的代码:

from lxml import etree, html
from StringIO import StringIO

doc = StringIO(
'''<html>
<head>
    <title>An experiment</title>
</head>
<body>
<p class="one">This is an interesting paragraph detailing the inner workings of something</p>
<p class="two">paragraph with <a href="/link/to/more">more info</a></p>
<p>posted by: me</p>
</body>
</html>''')

stylesheet = StringIO(
'''<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xhtml xsl">
<xsl:template match="p[@class='one']">
    <xsl:copy>
        <!-- when adding an attribute with the xsl:attribute tag -->
        <!-- the curly braces are not escaped, ie you dont have  -->
        <!-- to double them up -->
        <xsl:attribute name="class">{{item.class_one}}</xsl:attribute>
        <xsl:attribute name="nid">{{item.nid}}</xsl:attribute>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="p[@class='two']">
    <!-- but double 'em up in this instance -->
    <p class="{{{{item.class_two}}}}">
        <xsl:apply-templates/>
    </p>
</xsl:template>

<xsl:template match="a">
    <!-- anchor attribute href is urlencoded but the title is escaped -->
    <a href="{{{{item.get_absolute_url}}}}" title="{{{{item.title}}}}">
        <!-- name tag is urlencoded -->
        <xsl:attribute name="name">{{item.name}}</xsl:attribute>
        <!-- but oher attributes are not -->
        <xsl:attribute name="nid">{{item.nid}}</xsl:attribute>
        <xsl:attribute name="class">{{item.class_one}}</xsl:attribute>
        <xsl:apply-templates/>
    </a>
</xsl:template>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>
</xsl:stylesheet>
''')
def parse_doc():
    xsl = etree.parse(stylesheet)
    trans = etree.XSLT(xsl)
    root = html.parse(doc, etree.HTMLParser(encoding="windows-1252"))
    transformed = trans(root)
    print html.tostring(transformed)

if __name__ == '__main__':
    parse_doc()

不过这些文件的 HTML 格式都是不规范的 :)

2 个回答

3

看起来 应该html 序列化方法的正确输出。

来自 http://www.w3.org/TR/xslt#section-HTML-Output-Method

html 输出方法应该对 URI 属性值中的非 ASCII 字符进行转义,使用的是在 HTML 4.0 推荐的 B.2.1 节 中推荐的方法。

对于 XSLT 2.0,来自 http://www.w3.org/TR/xslt-xquery-serialization/#HTML_ESCAPE-URI-ATTRIBUTES

如果 escape-uri-attributes 参数的值是 yes,那么 HTML 输出方法 必须URI 转义 应用到 URI 属性值,但相对 URI 绝对不能 被转为绝对 URI。

4

也许你可以用XML来代替HTML序列化器。

>>> from lxml import etree, html
>>> 
>>> t = etree.XML('<a href="{{x}}" />')
>>> 
>>> etree.tostring(t)
'<a href="{{x}}"/>'
>>> html.tostring(t)
'<a href="%7B%7Bx%7D%7D"></a>'

撰写回答