gpt4 book ai didi

xslt - 使用 xslt 构建 django 模板文件

转载 作者:行者123 更新时间:2023-12-05 01:27:36 25 4
gpt4 key购买 nike

我有大约 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 解析器单独留下花括号

最佳答案

XSLT 对大括号有自己的用途 - 它们用于 Attribute Value Templates ,像这样:

<!-- $someVariableOrExpression will be evaluated here -->
<div title="{$someVariableOrExpression}" />

要将 literal 花括号放入 XSLT 中的属性值中,您需要将它们转义,这是通过将它们加倍来完成的:

<!-- the title will be "{$someVariableOrExpression}" here -->
<div title="{{$someVariableOrExpression}}" />

所以如果你想输出文字 double 花括号,你需要(猜猜看是什么):

<div title="{{{{$someVariableOrExpression}}}}" />

关于xslt - 使用 xslt 构建 django 模板文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4674217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com