使用xslt构建django模板文件 [英] building django template files with xslt

查看:128
本文介绍了使用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> //这样工作正常
< / span>
< div>
< xsl:attribute name =class> HControl< / xsl:attribute>
< xsl:text disable-output-escaping =yes> {%if node_count> 0%}< / xsl:text> //因为'>'
< div class =comment-list>
{%get_comment_list for thing'< xsl:value-of select =$ node_id/>'as node_comments%}
{%在node_comments%中的评论}
< div class =comment {{comment.object_id}}> //这得到转义
< a>
< xsl:attribute name =name> c {{comment.id}}< / xsl:attribute> //这样
< / 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。违反本节的人有1级轻罪。< / span>
< / p>
< span>
< a href => 1< / a>
< / span>
< div class =HControl>
< div class =comment-list>
< div class =comment'{comment.object_id}'> //这应该是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> //这应该是一个href的评论
permalink的评论#1
< / a>
< div>
那么你应该表现出一些尊重!
< / div>
< / div>
< / div>
< / div>



我用lxml转换文件.etree然后将该字符串传递给django模板对象,并将其渲染。
我似乎不明白如何让xslt解析器单独留下花括号

解决方案

XSLT有自己的大括号的目的 - 它们用于属性值模板,如这个:

 <! -  $ someVariableOrExpression将在这里进行评估 - > 
< div title ={$ someVariableOrExpression}/>

要在XSLT中将文字大括号转换为属性值,您需要逃避他们,这是通过加倍来完成的:

 <! - 标题将是{$ someVariableOrExpression} - > 
< div title ={{$ someVariableOrExpression}}/>

所以如果你想输出文字双重花括号,你需要猜测什么):

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


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

解决方案

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}}}}" />

这篇关于使用xslt构建django模板文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆