【发布时间】:2011-06-08 03:15:15
【问题描述】:
我有大约 4,000 个 html 文档,我正在尝试使用 xslt 将它们转换为 django 模板。我遇到的问题是,当我尝试在属性标记中包含模板变量时,xslt 正在转义模板变量的“{”花括号; 我的 xslt 文件如下所示:
<xsl:template match="p">
<p>
<xsl:attribute name="nid"><xsl:value-of select="$node_id"/></xsl:attribute>
<xsl:apply-templates select="*|node()"/>
</p>
<span>
{% get_comment_count for thing '<xsl:value-of select="$node_id"/>' as node_count %}
<a href="">{{ node_count }}</a> //This works as expected
</span>
<div>
<xsl:attribute name="class">HControl</xsl:attribute>
<xsl:text disable-output-escaping="yes">{% if node_count > 0 %}</xsl:text> // have to escape this because of the '>'
<div class="comment-list">
{% get_comment_list for thing '<xsl:value-of select="$node_id"/>' as node_comments %}
{% for comment in node_comments %}
<div class="comment {{ comment.object_id }}"> // this gets escaped
<a>
<xsl:attribute name="name">c{{ comment.id }}</xsl:attribute> //and so does this
</a>
<a>
<xsl:attribute name="href">
{% get_comment_permalink comment %}
</xsl:attribute>
permalink for comment #{{ forloop.counter }}
</a>
<div>
{{ comment.comment }}
</div>
</div>
{% endfor %}
</div>
{% endif %}
</div>
输出看起来像这样:
<div>
<p nid="50:1r:SB:1101S:5">
<span class="Insert">B. A person who violates this section is guilty of a class 1 misdemeanor.</span>
</p>
<span>
<a href="">1</a>
</span>
<div class="HControl">
<div class="comment-list">
<div class="comment '{ comment.object_id }'"> // this should be class="comment #c123"
<a name="c%7B%7B%20comment.id%20%7D%7D"></a> // this should name="c123"
<a href="%7B%%20get_comment_permalink%20comment%20%%7D"> //this should be an href to the comment
permalink for comment #1
</a>
<div>
Well you should show some respect!
</div>
</div>
</div>
</div>
我用 lxml.etree 转换文件,然后将字符串传递给 django 模板对象,然后渲染它。 我只是似乎不明白如何让 xslt 解析器不理会花括号
【问题讨论】:
-
顺便说一句,您应该删除所有
disable-output-escaping属性,它们在您使用它们的地方是错误的。 -
第一个是必需的,因为
>被转义并导致 django 在解析模板时崩溃,还是有其他方法?我会删除其他的,谢谢! -
是的,你是对的。第一个确实是正确的,没有其他办法。
标签: xslt django-templates escaping lxml