使用 xslt 构建 django 模板文件

发布于 2024-10-11 21:09:37 字数 2675 浏览 8 评论 0原文

我有大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月打碎记忆 2024-10-18 21:09:37

XSLT 对于花括号有其自己的用途 - 它们用于属性值模板,如下所示:

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

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

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

因此,如果您想输出文字 double 大括号,你需要(猜猜是什么):

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

XSLT has its own purpose for curly braces - they are used in Attribute Value Templates, like this:

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

To get literal curly braces into attribute values in XSLT, you need to escape them, which is done by doubling them:

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

So if you want to output literal double curly braces, you need (guess what):

<div title="{{{{$someVariableOrExpression}}}}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文