Anv*_*oad 2 xslt lxml escaping django-templates
我有大约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>
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样:
<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>
Run Code Online (Sandbox Code Playgroud)
我用lxml.etree转换文件,然后将字符串传递给django模板对象,然后渲染它.我似乎不明白如何让xslt解析器单独留下花括号
XSLT有自己的花括号用途 - 它们用于属性值模板,如下所示:
<!-- $someVariableOrExpression will be evaluated here -->
<div title="{$someVariableOrExpression}" />
Run Code Online (Sandbox Code Playgroud)
要获得字面花括号到XSLT属性值,你需要逃避他们,这是他们加倍完成:
<!-- the title will be "{$someVariableOrExpression}" here -->
<div title="{{$someVariableOrExpression}}" />
Run Code Online (Sandbox Code Playgroud)
所以如果你想输出文字双花括号,你需要(猜猜是什么):
<div title="{{{{$someVariableOrExpression}}}}" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
802 次 |
| 最近记录: |