使用 xslt 构建 django 模板文件
我有大约 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 解析器单独保留大括号
I have about 4,000 html documents that i am trying to convert into django templates using xslt. The problem that I am having is that xslt is escaping the '{' curly braces for template variables, when I try to include a template variable inside of an attribute tag;
my xslt file looks like this:
<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>
the output looks something like this:
<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>
I transform the file with lxml.etree and then pass the string to a django template object, and render it.
I just dont seem to understand how to get the xslt parser to leave the curly braces alone
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XSLT 对于花括号有其自己的用途 - 它们用于属性值模板,如下所示:
要将 literal 大括号放入 XSLT 中的属性值中,您需要对它们进行转义,这是通过将它们加倍来完成的:
因此,如果您想输出文字 double 大括号,你需要(猜猜是什么):
XSLT has its own purpose for curly braces - they are used in Attribute Value Templates, like this:
To get literal curly braces into attribute values in XSLT, you need to escape them, which is done by doubling them:
So if you want to output literal double curly braces, you need (guess what):