-1

我对 xslt 真的很陌生,但我接到了一项任务。这里实际上需要将下面的 xml 转换为表格格式。请帮帮我。也请给我一些好的网站来参考。

     <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>

我想要一个标题为 1 的表格输出,第二个是数字,第三个是另一个标题,最后是 toc-pg。

以下是我尝试过的(这是部分的,因为我被卡住了)

    <?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>

谢谢

4

1 回答 1

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>
于 2013-03-19T12:36:14.293 回答