我有大约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解析器单独留下花括号
答案 0 :(得分:7)
XSLT有自己的花括号用途 - 它们在Attribute Value Templates中使用,如下所示:
<!-- $someVariableOrExpression will be evaluated here -->
<div title="{$someVariableOrExpression}" />
要在XSLT中将文字大括号转换为属性值,您需要将它们转义,这可以通过加倍来完成:
<!-- the title will be "{$someVariableOrExpression}" here -->
<div title="{{$someVariableOrExpression}}" />
因此,如果您想输出文字双花括号,您需要(猜猜是什么):
<div title="{{{{$someVariableOrExpression}}}}" />