building django template files with xslt

2019-07-20 15:22发布

问题:

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

回答1:

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