且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用xslt构建django模板文件

更新时间:2023-11-02 13:49:04

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

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

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

 <! - 标题将是{$ someVariableOrExpression} - &GT; 
< 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}}}}" />