2011-01-12 64 views
2

我有大約4,000個html文檔,我嘗試使用xslt將其轉換爲django模板。我遇到的問題是,當我嘗試在屬性標記中包含模板變量時,xslt正在轉義模板變量的{{}花括號; 我的XSLT文件看起來是這樣的:用xslt構建django模板文件

<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解析器獨自離開大括號

+0

BTW你應該放棄所有的`禁用輸出escaping`的屬性,他們是錯的,你使用它們。 – Tomalak 2011-01-13 00:03:47

+0

第一個是需要的,因爲`>`會被轉義並導致django在解析模板的時候崩潰,或者有另一種方式嗎?我會刪除其他人,謝謝! – AnvilRockRoad 2011-01-13 00:27:29

回答

7

XSLT對大括號自己的目的 - 他們在Attribute Value Templates使用,就像這樣:

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

要獲得文字花括號到XSLT屬性值,你需要逃避他們,這是他們加倍完成:

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

所以,如果你想輸出文字做uble大括號,你需要(猜測):

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