2
我有大约4,000个html文档,我尝试使用xslt将其转换为django模板。我遇到的问题是,当我尝试在属性标记中包含模板变量时,xslt正在转义模板变量的{{}花括号; 我的XSLT文件看起来是这样的:用xslt构建django模板文件
<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解析器独自离开大括号
BTW你应该放弃所有的`禁用输出escaping`的属性,他们是错的,你使用它们。 – Tomalak 2011-01-13 00:03:47
第一个是需要的,因为`>`会被转义并导致django在解析模板的时候崩溃,或者有另一种方式吗?我会删除其他人,谢谢! – AnvilRockRoad 2011-01-13 00:27:29