2014-06-25 78 views
0

我有下面的XML數據。模板調用進入另一個模板

<para><content-style font-style="bold">1/4 4.—</content-style>(1) In these Rules, unless the context otherwise requires, the following expressions have the meanings hereby respectively assigned to them, namely: 
<orderedlist type="manual"> 
<item><para>「attend」 includes the appearance by any person using electronic, mechanical or other means permitted by the Court;</para></item> 
<item><para>「bailiff」 includes the registrar, any clerk or other officer of the Court charged with the duties of a bailiff in the Subordinate Courts;</para></item> 
<item><para>「Civil Procedure Convention」 means the conventions set out in Appendix C to these Rules and includes any convention, treaty or agreement of any description or any provision thereof between different States relating to civil procedure in the court;</para></item> 
<item><para>「folio」 means 100 words, each figure being counted as one word;</para></item> 
<item><para>「Form」 means a form set out in Appendix A to these Rules, and a form so numbered in the Appendix;</para></item> 
<item><para>「Judge」 means a judge of the High Court or District Judge and includes, in cases where he is empowered to act, a Magistrate or the Registrar, as the case may require;</para></item> 
<item><para>「officer」 means an officer of the Supreme Court or Subordinate Courts;</para></item> 
<item><para>「originating process」 means a writ of summons or an originating summons;</para></item> 
<item><para>「originating summons」 means every summons for the commencement of proceedings other than a writ of summons;</para></item> 
<item><para>「pleading」 does not include an originating summons or preliminary act;</para></item> 
<item><para>「receiver」 includes a manager or consignee;</para></item> 
<item><para>「Registry」 means the Registry of the Supreme Court or the Registry of the Subordinate Courts, as the case may be, and references to the Registrar shall be construed accordingly;</para></item> 
<item><para>「scheduled territories」 has the meaning assigned to it by the Exchange Control Act (Chapter 99);</para></item> 
<item><para>「Sheriff」 includes a bailiff of the Subordinate Courts;</para></item> 
<item><para>「sign」, in relation to the signing of documents by the Registrar, includes the affixing of a facsimile signature;</para></item> 
<item><para>「solicitor」 has the same meaning as in the Legal Profession Act (Chapter 161);</para></item> 
<item><para>「summons」 means every summons in a pending cause or matter.</para></item> 
<item><para>「working day」 means any day other than a Saturday, Sunday or public holiday;</para></item> 
<item><para>「writ」 means a writ of summons.</para></item> 
</orderedlist></para> 

這裏還有一個paraorderedlist,當我跑我的模板,該orderedlist越來越對位即orderedlist裏面的內容para內,後在orderedlist模板獲取調用,我是來了很困惑爲什麼會發生這種情況。

這裏是DEMO,請讓我知道我該如何解決這個問題。

感謝

回答

1

你還沒有告訴我們這導致你想,但根據您的匹配模式,它看起來就好像你現在想應用的模板上content-style但你call-template過程para元素的字符串值。

因此,我已經改變的代碼如下:

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output indent="yes"/> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 



    <xsl:template name="para" match="para"> 

<xsl:apply-templates select="./node()[1][self::page]" mode="first"/> 

     <div> 
<xsl:choose> 
    <xsl:when test="./@align"> 
<xsl:attribute name="class"> 
          <xsl:text>para align-</xsl:text> 
          <xsl:value-of select="./@align"/> 
         </xsl:attribute>  
         </xsl:when> 
         <xsl:otherwise> 
         <xsl:attribute name="class"> 
          <xsl:text>para</xsl:text> 

         </xsl:attribute>  

         </xsl:otherwise> 
</xsl:choose> 
<xsl:choose> 

      <xsl:when test="contains(./content-style[1],'/')"> 

      <div class="para"> 

      <xsl:apply-templates/> 
     </div> 
      </xsl:when> 

    <xsl:otherwise> 
<xsl:apply-templates/> 
    </xsl:otherwise> 
</xsl:choose> 
     </div> 



    </xsl:template> 

    <xsl:template match="text()"> 

    <xsl:analyze-string select="." regex="(([Cc]hapter)\s(\d+))"> 

     <xsl:matching-substring> 

      <xsl:value-of select="."/> 

     </xsl:matching-substring> 

      <xsl:non-matching-substring> 

      <xsl:analyze-string select="." regex="http://[^ ]+"> 
      <xsl:matching-substring> 
     <a href="{.}"> 
          <xsl:value-of select="."/> 
            </a> 

      </xsl:matching-substring> 
      <xsl:non-matching-substring> 

      <xsl:value-of select="."/> 
      </xsl:non-matching-substring> 

     </xsl:analyze-string> 
     </xsl:non-matching-substring> 
    </xsl:analyze-string> 
    </xsl:template> 




    <xsl:template match="para/content-style[1]"> 

<xsl:analyze-string select="." regex="([0-9]+)/([0-9]+)/([0-9]+)"> 
<xsl:matching-substring> 
<span class="phrase"> 
     <a name="{concat('P',regex-group(1),'-',regex-group(2),'-',regex-group(3))}"/> 

    <xsl:value-of select="."></xsl:value-of> 
</span> 
</xsl:matching-substring> 
<xsl:non-matching-substring> 
<xsl:analyze-string select="." regex="([0-9]+)/([0-9]+)"> 
<xsl:matching-substring> 
<span class="phrase"> 
     <a name="{concat('P',regex-group(1),'-',regex-group(2))}"/> 
    <xsl:value-of select="."></xsl:value-of> 
</span> 
</xsl:matching-substring> 
<xsl:non-matching-substring> 

    <xsl:value-of select="."></xsl:value-of> 

</xsl:non-matching-substring> 
</xsl:analyze-string> 
</xsl:non-matching-substring> 
</xsl:analyze-string>  
</xsl:template> 


<xsl:template name="orderedlist" match="orderedlist"> 
     <ol class="eng-orderedlist orderedlist"> 
      <xsl:apply-templates/> 
     </ol> 
    </xsl:template> 


<xsl:template match="item"> 
<!--<xsl:apply-templates select="./para[1]/node()[1][self::page]" mode="first"/>--> 
<xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="item/para"> 

<li class="item"> 
<xsl:variable name="strl"> 
<xsl:value-of select="string-length(../@num)"/> 
</xsl:variable> 

     <xsl:for-each select="."> 
     <xsl:apply-templates select="./node()[1][self::page]" mode="first"/> 
     <div class="para"> 
     <xsl:choose> 

<xsl:when test="name(../../parent::*[1]) = 'section' and $strl &gt; '2' and not(preceding-sibling::para)"> 

    <xsl:apply-templates select="../@num" mode="next"/> 


</xsl:when> 
<xsl:otherwise> 

<xsl:if test="not(preceding-sibling::para)"> 
<span class="item-num"> 
<xsl:value-of select="../@num"/> 
</span> 
</xsl:if> 
</xsl:otherwise> 



</xsl:choose> 

     <xsl:apply-templates select="child::node()[not(self::para)]"/> 
    </div> 
</xsl:for-each> 
    </li> 

</xsl:template> 






<xsl:template mode="next" match="@num"> 



<xsl:analyze-string select="." regex="([0-9]+)\.([0-9]+)\.([0-9]+)"> 
<xsl:matching-substring> 
     <a name="{concat('P',regex-group(1),'-',regex-group(2),'-',regex-group(3))}"/> 
    <span class="phrase"> 
    <xsl:value-of select="."></xsl:value-of> 
    </span> 
</xsl:matching-substring> 
<xsl:non-matching-substring> 
<xsl:analyze-string select="." regex="([0-9]+)\.([0-9]+)"> 
<xsl:matching-substring> 
     <a name="{concat('P',regex-group(1),'-',regex-group(2))}"/> 
    <span class="phrase"> 
    <xsl:value-of select="."></xsl:value-of> 
    </span> 
</xsl:matching-substring> 
<xsl:non-matching-substring> 

    <xsl:value-of select="."></xsl:value-of> 

</xsl:non-matching-substring> 
</xsl:analyze-string> 
</xsl:non-matching-substring> 
</xsl:analyze-string> 


<!-- <xsl:apply-templates select="../para"/></div> 
     </li>--> 

</xsl:template> 

<xsl:template match="page[not(preceding-sibling::node()[not(self::text()) or normalize-space()])]"/> 






</xsl:transform> 

這導致http://xsltransform.net/eiZQaEP/1,其輸出

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <div class="para"> 
     <div class="para"> 
     <span class="phrase"> 
      <a name="P1-4"/>1/4</span> 4.—(1) In these Rules, unless the context otherwise requires, the following expressions have the meanings hereby respectively assigned to them, namely: 
<ol class="eng-orderedlist orderedlist"> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「attend」 includes the appearance by any person using electronic, mechanical or other means permitted by the Court;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「bailiff」 includes the registrar, any clerk or other officer of the Court charged with the duties of a bailiff in the Subordinate Courts;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「Civil Procedure Convention」 means the conventions set out in Appendix C to these Rules and includes any convention, treaty or agreement of any description or any provision thereof between different States relating to civil procedure in the court;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「folio」 means 100 words, each figure being counted as one word;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「Form」 means a form set out in Appendix A to these Rules, and a form so numbered in the Appendix;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「Judge」 means a judge of the High Court or District Judge and includes, in cases where he is empowered to act, a Magistrate or the Registrar, as the case may require;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「officer」 means an officer of the Supreme Court or Subordinate Courts;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「originating process」 means a writ of summons or an originating summons;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「originating summons」 means every summons for the commencement of proceedings other than a writ of summons;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「pleading」 does not include an originating summons or preliminary act;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「receiver」 includes a manager or consignee;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「Registry」 means the Registry of the Supreme Court or the Registry of the Subordinate Courts, as the case may be, and references to the Registrar shall be construed accordingly;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「scheduled territories」 has the meaning assigned to it by the Exchange Control Act (Chapter 99);</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「Sheriff」 includes a bailiff of the Subordinate Courts;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「sign」, in relation to the signing of documents by the Registrar, includes the affixing of a facsimile signature;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「solicitor」 has the same meaning as in the Legal Profession Act (Chapter 161);</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「summons」 means every summons in a pending cause or matter.</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「working day」 means any day other than a Saturday, Sunday or public holiday;</div> 
      </li> 
      <li class="item"> 
       <div class="para"> 
        <span class="item-num"/>「writ」 means a writ of summons.</div> 
      </li> 
     </ol> 
     </div> 
    </div> 
</root> 

主要的變化是簡化

<div class="para"> 
    <xsl:call-template name="phrase"/><!-- 

    <xsl:variable name="content" select="generate-id(content-style[1])" />--> 
    <xsl:apply-templates select="child::node()[not(self::content-style)]"/> 

<div class="para"> 
     <xsl:apply-templates/> 
    </div> 

,然後再消除對<xsl:template name="phrase" match="para/content-style[1]" mode="phrase">名稱和模式屬性得到<xsl:template name="phrase" match="para/content-style[1]">。這樣一切都落在了正確的位置。所以基本上重要的一步是儘可能地使用apply-templates和匹配模板,而不是試圖使用call-template。如果您需要或想要使用call-template,請記住,它不會更改上下文節點,因此在匹配para a call-template的模板中與指定的模板繼續處理該para元素。

+0

哇@Martin,謝謝你,這個作品很有魅力,請你詳細解釋一下我出錯的地方以及你如何解決這個問題?再次感謝 – user2423959

+0

我已經對我所做的更改添加了一些解釋。 –

相關問題