2013-03-19 56 views
-1

我真的很新xslt,但我收到了一個任務。這裏實際上下面的xml需要轉換成表格格式。請幫助我。還請提供一些很好的網站來參考。創建xslt文件

 <toc-div> 
      <toc-item> 
       <toc-title>CHAPTER 1 INTRODUCTION</toc-title> 
       <toc-subitem num="1."> 
        <toc-title>The British Virgin Islands</toc-title> 
        <toc-pg>1.001</toc-pg> 
       </toc-subitem> 
       <toc-subitem num="2."> 
        <toc-title>History and early constitutional developments</toc-title> 
        <toc-pg>1.003</toc-pg> 
       </toc-subitem> 
       <toc-subitem num="3."> 
        <toc-title>Development as a financial centre</toc-title> 
        <toc-pg>1.008</toc-pg> 
       </toc-subitem> 
       <toc-subitem num="4."> 
        <toc-title>Common Law and Equity</toc-title> 
        <toc-pg>1.015</toc-pg> 
       </toc-subitem> 
       <toc-subitem num="5."> 
        <toc-title>Statutes</toc-title> 
        <toc-pg>1.017</toc-pg> 
       </toc-subitem> 
       <toc-subitem num="6."> 
        <toc-title>Taxation</toc-title> 
        <toc-pg>1.022</toc-pg> 
       </toc-subitem> 
      </toc-item> 
</toc-div> 

我想在第3和TOC-PG在最後與冠軍表格輸出1,數量第二的另一種稱謂。

下面是我已經嘗試過(這部分,我被卡住了)

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ntw="Number2Word.uri" exclude-result-prefixes="ntw" version="1.0"> 
    <xsl:output method="html"/> 
    <xsl:variable name="ThisDocument" select="document('')"/> 
    <xsl:template match="/"> 
     <html> 
      <head> 
       <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/> 
       <title> 
        <xsl:value-of select="chapter/title"/> 
       </title> 
       <link href="er:#css" rel="stylesheet" type="text/css"/> 
      </head> 
      <body> 
       <xsl:apply-templates/> 

      </body> 
     </html> 
    </xsl:template> 
    <xsl:template match="toc-item" name="x"> 
     <table> 
      <tr> 
       <td> 
       <xsl:value-of select="toc-title"/> 

    </td> 
    <xsl:apply-templates/> 
      </tr> 
     </table> 

    </xsl:template> 
    <xsl:template match="toc-subitem/*" mode="a" name="a"> 
     <td><xsl:value-of select="toc-title"/></td> 
    <xsl:apply-templates></xsl:apply-templates> 
    </xsl:template> 
    <xsl:template match="toc-subitem" name="b"> 
     <xsl:variable name="z"> 
      <xsl:value-of select="@num"/> 
     </xsl:variable> 
     <td><xsl:value-of select="$z"/></td> 
    <xsl:call-template name="c"></xsl:call-template> 
</xsl:template> 


    <xsl:template match="toc-subitem" name="c" mode="c"> 
     <xsl:for-each select="toc-title"> 
     <td> 
      <xsl:value-of select="current()"/> 
     </td> 
     </xsl:for-each> 
    <xsl:apply-templates/> 
</xsl:template> 
    <xsl:template match="toc-pg" name="pg"> 
     <td> 
    <xsl:value-of select="current()"/>  
    </td> 
    </xsl:template> 
    <!-- Namespace ntw--> 
    <ntw:nums num="1" word="first"/> 
    <ntw:nums num="2" word="second"/> 
    <ntw:nums num="3" word="third"/> 
    <ntw:nums num="4" word="forth"/> 
    <ntw:nums num="5" word="fifth"/> 
    <ntw:nums num="6" word="sixth"/> 
    <ntw:nums num="7" word="seventh"/> 
    <ntw:nums num="8" word="eighth"/> 
    <ntw:nums num="9" word="nighth"/> 
    <ntw:nums num="10" word="tenth"/> 
    <!-- Namespace ntw ends --> 
</xsl:stylesheet> 

我想我的HTML將在下面的格式

<table> 
     <tr> 
      <td class="Main">CHAPTER 1 INTRODUCTION</td> 
      <td><tr> 
       <td class="a">1.</td> 
       <td class="b">The British Virgin Islands</td> 
       <td class="c">1.001</td> 
      </tr></td> 
      <td><tr> 
       <td class="a">2.</td> 
       <td class="b">History and early constitutional developments</td> 
       <td class="c">1.003</td> 
      </tr></td> 
     <td><tr> 
      <td class="a">3.</td> 
      <td class="b">Development as a financial centre</td> 
      <td class="c">1.008</td> 
     </tr> </td> 
     <td><tr> 
      <td class="a">4.</td> 
      <td class="b">Common Law and Equity</td> 
      <td class="c">1.015</td> 
     </tr></td> 
     <td><tr> 
      <td class="a">5.</td> 
      <td class="b">Statutes</td> 
      <td class="c">1.017</td> 
     </>tr</td> 
     <td> 
<tr> 
      <td class="a">6.</td> 
      <td class="b">Taxation</td> 
      <td class="c">1.022</td> 
    </tr> </td> 
     </tr> 
     </table> 

感謝

+0

嗨ZVZDHK,我已經用我所嘗試過的更新了我的文章。 – 2013-03-19 10:03:05

+0

你想在每一行或其他一些處理中重複使用'toc-title'嗎? – JLRishe 2013-03-19 10:13:43

+0

嗨@JLRishe:我希望在這裏有一次toc-title(第一章介紹),但是還有更多類似的toc-titles,這只是我的xml文檔的一部分。首先我想知道如何爲此做一個xslt。 – 2013-03-19 10:17:25

回答

3

這應該做到這一點:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:ntw="Number2Word.uri" 
       exclude-result-prefixes="ntw" version="1.0"> 
    <xsl:output method="html" omit-xml-declaration="yes"/> 
    <xsl:template match="/"> 
    <html> 
     <head> 
     <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/> 
     <title> 
      <xsl:value-of select="chapter/title"/> 
     </title> 
     <link href="er:#css" rel="stylesheet" type="text/css"/> 
     </head> 
     <body> 
     <table> 
      <xsl:apply-templates/> 
     </table> 
     </body> 
    </html> 
    </xsl:template> 
    <xsl:template match="toc-title"> 
    <tr> 
     <td class="Main" colspan="3"> 
     <xsl:value-of select="."/> 
     </td> 
    </tr> 
    </xsl:template> 
    <xsl:template match="toc-subitem" > 
    <tr> 
     <td class="a"> 
     <xsl:value-of select="@num"/> 
     </td> 
     <td class="b"> 
     <xsl:value-of select="toc-title"/> 
     </td> 
     <td class="c"> 
     <xsl:value-of select="toc-pg"/> 
     </td> 
    </tr> 
    </xsl:template> 
</xsl:stylesheet> 

在您的示例輸入上運行時,會生成以下代碼:

<html> 
    <head> 
    <META http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> 
    <title> 
    </title> 
    <link href="er:#css" rel="stylesheet" type="text/css"> 
    </head> 
    <body> 
    <table> 

    <tr> 
     <td class="Main" colspan="3">CHAPTER 1 INTRODUCTION</td> 
     </tr> 
    <tr> 
     <td class="a">1.</td> 
     <td class="b">The British Virgin Islands</td> 
     <td class="c">1.001</td> 
     </tr> 
    <tr> 
     <td class="a">2.</td> 
     <td class="b">History and early constitutional developments</td> 
     <td class="c">1.003</td> 
     </tr> 
    <tr> 
     <td class="a">3.</td> 
     <td class="b">Development as a financial centre</td> 
     <td class="c">1.008</td> 
     </tr> 
    <tr> 
     <td class="a">4.</td> 
     <td class="b">Common Law and Equity</td> 
     <td class="c">1.015</td> 
     </tr> 
    <tr> 
     <td class="a">5.</td> 
     <td class="b">Statutes</td> 
     <td class="c">1.017</td> 
     </tr> 
    <tr> 
     <td class="a">6.</td> 
     <td class="b">Taxation</td> 
     <td class="c">1.022</td> 
     </tr> 

</table> 
    </body> 
</html> 
+0

謝謝你,很多朋友 – 2013-03-19 13:33:24