estoy usando lxml 2.2.8 y tratando de transformar algunos archivos HTML existentes en las plantillas de Django. el único problema que estoy teniendo es que lxml urlencodes el nombre de anclaje y atributos href. por ejemplo:¿Hay una manera de desactivar URLEncoding de atributos de anclaje en lxml
<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>
produce html así:
<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>
lo que estoy tratando de decir esto:
<a href="{{item.get_absolute_url}}">more info</a>
hay una manera de desactivar el (automático) urlencoding que lxml está haciendo?
aquí es (básicamente) el código que estoy utilizando para generar y analizar el archivo:
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()
con la excepción de que estos archivos son todos HTML incorrecto :)
¿Esto todavía sucede si declara ' ' en su programa XSLT? Sólo –
Tomalak
Altova puede reproducir este método de serialización con 'html'. MSXSL 3/4 y Saxon no. –
@Tomalak Yo trate de cambiar el método de salida de XML y se dio tienen ningún efecto – AnvilRockRoad