2011-07-14 119 views
1

XML數據的,我有以下標記轉換使用XSLT

 <ul> 
       <li>this is bullet list</li> 
       <li>this is another bullet</li> 
       <ul> 
        <li>this is embaded</li> 
        <li>this is embaded</li> 
        <ul> 
         <li>this is furthur embaded</li> 
        </ul> 
       </ul> 
     </ul> 

,我需要一個XSLT腳本來轉換成

<xml> 
    <unorderlist> 
    <list><text>this is bullet list</text></list> 
    <list><para><text>this is another bullet</text> 
    <unorderlist> 
     <list><text>this is embaded</text></list> 
     <list><para><text>this is embaded</text> 
      <unorderlist> 
       <list><text>this is furthur embabed</text></list> 
      </unorderlist> 
      </para></list> 
     </unorderlist> 
    </para> 
    </list> 
</unorderlist> 
</xml> 

基本上所有的嵌套項目應在最後一個節點標籤內出現。任何提示將非常感激。

回答

2

例如

<xsl:template match="/"> 
    <xml> 
    <xsl:apply-templates/> 
    </xml> 
</xsl:template> 

<xsl:template match="ul"> 
    <unorderlist> 
    <xsl:apply-templates select="li"/> 
    </unorderlist> 
</xsl:template> 

<xsl:template match="li"> 
    <list> 
    <xsl:choose> 
     <xsl:when test="following-sibling::*[1]/self::ul"> 
     <para> 
      <text> 
      <xsl:apply-templates/> 
      </text> 
      <xsl:apply-templates select="following-sibling::*[1]/self::ul"/> 
     </para> 
     </xsl:when> 
     <xsl:otherwise> 
     <text> 
      <xsl:apply-templates/> 
     </text> 
     </xsl:otherwise> 
    </xsl:choose> 
    </list> 
</xsl:template> 

如果你想支持更復雜的輸入,使用單獨的模式爲<ul>拉到前面<li>

+0

謝謝Jelovirt,剛剛想出了另一個senerio,我在embedad元素中使用了OL而不是UL。如何捕捉這個過程? – atif

+0

不用擔心我已經修改了你的腳本,非常感謝。 – atif

+0

如何處理OL和ol或組合。我的標記是使用freetextbox html編輯器生成的,因此在某些瀏覽器中,標記會生成爲OL,UL,LI,其中有些是ol,ul,li?有沒有一種方法可以擺脫這種情況下的敏感性? – atif