2015-09-11 46 views
0

我一直在爲這個問題掙扎了兩天,現在我找不到答案,但我相信我會接近它。XSL - 只顯示一些列表

我有這個(簡體)自定義代碼:

<chapter><subchapter><subchaptersection><module> 
      <topic> 
       <title>Title1</title> 
       <para>Para1.1</para> 
       <list id="1"> 
        <listitem> 
         <para>Item1.1</para> 
        </listitem> 
        <listitem> 
         <para>Item2.2</para> 
        </listitem> 
       </list> 

       <para>Para2</para> 
       <list id="2"> 
        <para>Para2.1</para> 
        <listitem> 
         <para>Item2.1</para> 
        </listitem> 
        <listitem> 
         <para>Item2.2</para> 
        </listitem> 
       </list> 
</subchaptersection></subchapter></chapter> 

我有這樣的XSL轉換自定義代碼來部分XML(我還有一個XSL然後完成完美轉換爲XML,併爲您的信息,列表也可以出現在「小節」部分):

<xsl:template match="chapter"><Chapter><title><xsl:value-of select="title"/></title> 
<xsl:apply-templates select="module/topic|subchapter"/> 
</Chapter> 
</xsl:template> 

<xsl:template match="module/topic"> 

     <topic><xsl:attribute name="id"> 
      <xsl:value-of select="normalize-space(title)"/> 
     </xsl:attribute><title><xsl:value-of select="title"/></title> 

     <body><xsl:apply-templates select="para|*/para|warning//para|list/listitem/para|introduction//para"/> 
     </body></topic> 

</xsl:template> 

<xsl:template match="subchapter"> 

     <topic><xsl:attribute name="id"> 
      <xsl:value-of select="normalize-space(title)"/> 
     </xsl:attribute><title><xsl:value-of select="title"/></title> 

     <body><xsl:apply-templates select="introduction//para|subchaptersection|module/topic"/> 
     </body></topic> 

</xsl:template> 


<xsl:template match="subchaptersection"> 
     <topic><xsl:attribute name="id"> 
      <xsl:value-of select="normalize-space(title)"/> 
     </xsl:attribute><title><xsl:value-of select="title"/></title> 
     <xsl:apply-templates select="introduction//para|module/topic"/></topic> 
</xsl:template> 


<xsl:template match="para|*/para|warning//para|list/listitem/para|introduction//para"> 

    <xsl:if test="not(ancestor::list) and not (ancestor::warning)"> 
    <p><xsl:value-of select="."/></p> 
    </xsl:if> 

    <xsl:if test="ancestor::list"> 
    <xsl:call-template name="LIST"/> 
    </xsl:if> 

</xsl:template> 

<xsl:template name="LIST"> 
    <xsl:if test="not (../preceding-sibling::listitem/para)"> 
    <xsl:if test="following-sibling::listitem"> 
    <p><xsl:value-of select="."/></p> 
    </xsl:if> 
    <ul> 
    <xsl:for-each select="self::para/following-sibling::listitem"> 
     <li> 
      <xsl:value-of select="."/> 
     </li> 
     </xsl:for-each> 
    </ul> 
    </xsl:if> 
</xsl:template> 

我得到的問題是,清單2中顯示,但清單1不(僅空UL是輸出)。從其他幾個列表中,我可以看到只顯示包含list和first listitem之間的段的列表(與列表2的情況相同)。

我的XSL在哪裏出錯?我總是可以在列表和第一個listitem之間手動插入para,但我確定有一個XSL解決方案。

非常感謝您的幫助。

+0

我相信你沒有一套模板匹配'list'和應用它犯了一個錯誤。相反,你可以調用一個命名模板來處理來自前一個兄弟'para'的上下文中的'list'。這增加了代碼的可怕複雜性。 –

+0

非常感謝Michael,我已經設法使它的工作得益於你的建議。 我已經創建鏈接到子章和模塊/主題部分的列表模板。它簡化了我的列表模板:

    \t \t <的xsl:for-每個選擇=」 列表項[父::列表] 「>
  • <的xsl:for-每個選擇=」 ./對「>

' – Frost

+0

Frost,在註釋裏面輸入XML是很難讀的......我已經把你的XML轉換成了一個答案。請注意,如果您自己找到解決方案,則可以在StackOverflow中回答自己的問題。 – Abel

回答

0

作爲參考,這個問題被回答Michael's suggestion和OP的工作,通過使用:

<xsl:template match="list"> 
    <xsl:if test="para[parent::list]"> 
     <p> 
      <xsl:value-of select="para[parent::list]" /> 
     </p> 
    </xsl:if> 
    <ul> 
     <xsl:for-each select="listitem[parent::list]"> 
      <li> 
       <xsl:for-each select="./para"> 
        <p> 
         <xsl:value-of select="." /> 
        </p> 
       </xsl:for-each> 
      </li> 
     </xsl:for-each> 
    </ul> 
</xsl:template>