1

我有下面的 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>

我正在应用下面的xslt。

<xsl:template match="toc-subitem">
    <table>
    <td>
    <xsl:value-of select="preceding-sibling::toc-title[1]"/>    
</td></table>
    <xsl:variable name="tocpg">
        <xsl:value-of select="current()/toc-pg"/></xsl:variable>
    <xsl:variable name="abc">
        <xsl:choose>
            <xsl:when test="not(contains(@num, '('))">
                <xsl:value-of select="1"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="2"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="before">
        <xsl:value-of select="normalize-space(substring-before($tocpg, '.'))"/>
    </xsl:variable>
    <xsl:variable name="after">
        <xsl:value-of select="normalize-space(substring-after($tocpg, '.'))"/>
    </xsl:variable>
    <xsl:variable name="z">
        <xsl:value-of select="current()/@num"/>
    </xsl:variable> 

    <xsl:variable name="tocpgtag" select="translate($tocpg,'.', '-')"/>

    <xsl:variable name="numa" select="number(translate(@num, '.', ''))" />

    <xsl:variable name="itemlevel">
        <xsl:value-of select="$ThisDocument//ntw:nums[@num=$abc]/@word"/>
    </xsl:variable>

    <xsl:variable name="tocitemlevel">
        <xsl:value-of select="concat('toc-item-', $itemlevel,'-level')"/>
    </xsl:variable>
    <xsl:variable name="conc">
        <xsl:value-of select="concat('er:#BVI_CH_0',$before, '/P', normalize-space($tocpgtag))"/>
    </xsl:variable>

    <table class="{$tocitemlevel}">

        <tbody>
            <tr>
                <td class="toc-subitem-num">
                    <xsl:value-of select="$z" />
                </td>
                <td class="toc-title">
                    <xsl:value-of select="current()/toc-title" />
                </td>
                <td class="toc-pg">
                    <a href="{$conc}">
                        <xsl:value-of select="current()/toc-pg" />
                    </a>
                </td>
            </tr>
        </tbody>

    </table>
</xsl:template>

当我应用这个模板时,我得到的输出如下。

<table>
     <td>CHAPTER 1 INTRODUCTION</td>
  </table>
  <table class="toc-item--level">
     <tbody>
        <tr>
           <td class="toc-subitem-num">1.</td>
           <td class="toc-title">The British Virgin Islands</td>
           <td class="toc-pg"><a href="er:#BVI_CH_01/P1-001">1.001</a></td>
        </tr>
     </tbody>
  </table>

  <table>
     <td>CHAPTER 1 INTRODUCTION</td>
  </table>
  <table class="toc-item--level">
     <tbody>
        <tr>
           <td class="toc-subitem-num">2.</td>
           <td class="toc-title">History and early constitutional developments</td>
           <td class="toc-pg"><a href="er:#BVI_CH_01/P1-003">1.003</a></td>
        </tr>
     </tbody>
  </table>

  <table>
     <td>CHAPTER 1 INTRODUCTION</td>
  </table>
  <table class="toc-item--level">
     <tbody>
        <tr>
           <td class="toc-subitem-num">3.</td>
           <td class="toc-title">Development as a financial centre</td>
           <td class="toc-pg"><a href="er:#BVI_CH_01/P1-008">1.008</a></td>
        </tr>
     </tbody>
  </table>

  <table>
     <td>CHAPTER 1 INTRODUCTION</td>
  </table>
  <table class="toc-item--level">
     <tbody>
        <tr>
           <td class="toc-subitem-num">4.</td>
           <td class="toc-title">Common Law and Equity</td>
           <td class="toc-pg"><a href="er:#BVI_CH_01/P1-015">1.015</a></td>
        </tr>
     </tbody>
  </table>

  <table>
     <td>CHAPTER 1 INTRODUCTION</td>
  </table>
  <table class="toc-item--level">
     <tbody>
        <tr>
           <td class="toc-subitem-num">5.</td>
           <td class="toc-title">Statutes</td>
           <td class="toc-pg"><a href="er:#BVI_CH_01/P1-017">1.017</a></td>
        </tr>
     </tbody>
  </table>

  <table>
     <td>CHAPTER 1 INTRODUCTION</td>
  </table>
  <table class="toc-item--level">
     <tbody>
        <tr>
           <td class="toc-subitem-num">6.</td>
           <td class="toc-title">Taxation</td>
           <td class="toc-pg"><a href="er:#BVI_CH_01/P1-022">1.022</a></td>
        </tr>
     </tbody>
  </table>

但我想得到它如下

<table>
     <td>CHAPTER 1 INTRODUCTION</td>
  </table>
  <table class="toc-item--level">
     <tbody>
        <tr>
           <td class="toc-subitem-num">1.</td>
           <td class="toc-title">The British Virgin Islands</td>
           <td class="toc-pg"><a href="er:#BVI_CH_01/P1-001">1.001</a></td>
        </tr>
     </tbody>
  </table>

  <table class="toc-item--level">
     <tbody>
        <tr>
           <td class="toc-subitem-num">2.</td>
           <td class="toc-title">History and early constitutional developments</td>
           <td class="toc-pg"><a href="er:#BVI_CH_01/P1-003">1.003</a></td>
        </tr>
     </tbody>
  </table>
  <table class="toc-item--level">
     <tbody>
        <tr>
           <td class="toc-subitem-num">3.</td>
           <td class="toc-title">Development as a financial centre</td>
           <td class="toc-pg"><a href="er:#BVI_CH_01/P1-008">1.008</a></td>
        </tr>
     </tbody>
  </table>
  <table class="toc-item--level">
     <tbody>
        <tr>
           <td class="toc-subitem-num">4.</td>
           <td class="toc-title">Common Law and Equity</td>
           <td class="toc-pg"><a href="er:#BVI_CH_01/P1-015">1.015</a></td>
        </tr>
     </tbody>
  </table>
  <table class="toc-item--level">
     <tbody>
        <tr>
           <td class="toc-subitem-num">5.</td>
           <td class="toc-title">Statutes</td>
           <td class="toc-pg"><a href="er:#BVI_CH_01/P1-017">1.017</a></td>
        </tr>
     </tbody>
  </table>
  <table class="toc-item--level">
     <tbody>
        <tr>
           <td class="toc-subitem-num">6.</td>
           <td class="toc-title">Taxation</td>
           <td class="toc-pg"><a href="er:#BVI_CH_01/P1-022">1.022</a></td>
        </tr>
     </tbody>
  </table>

请让我知道我哪里出错了。

谢谢

4

3 回答 3

0

您应该为此使用单独的模板(我还清理了一堆<xsl:variable>s):

  <xsl:template match="toc-title">
    <table>
      <tr>
        <td>
          <xsl:value-of select="preceding-sibling::toc-title[1]"/>
        </td>
      </tr>
    </table>
  </xsl:template>
  <xsl:template match="toc-subitem">
    <xsl:variable name="tocpg" select="toc-pg" />
    <xsl:variable name="abc" select="1 + contains(@num, '(')" />
    <xsl:variable name="before"
                  select="normalize-space(substring-before($tocpg, '.'))"/>
    <xsl:variable name="after"
                  select="normalize-space(substring-after($tocpg, '.'))"/>
    <xsl:variable name="z" select="@num"/>

    <xsl:variable name="tocpgtag" select="translate($tocpg,'.', '-')"/>
    <xsl:variable name="numa" select="number(translate(@num, '.', ''))" />

    <xsl:variable name="itemlevel"
                  select="$ThisDocument//ntw:nums[@num=$abc]/@word"/>

    <xsl:variable name="tocitemlevel"
                  select="concat('toc-item-', $itemlevel,'-level')"/>
    <xsl:variable name="conc"
                  select="concat('er:#BVI_CH_0',$before, '/P', 
                                 normalize-space($tocpgtag))"/>


    <table class="{$tocitemlevel}">
      <tbody>
        <tr>
          <td class="toc-subitem-num">
            <xsl:value-of select="$z" />
          </td>
          <td class="toc-title">
            <xsl:value-of select="toc-title" />
          </td>
          <td class="toc-pg">
            <a href="{$conc}">
              <xsl:value-of select="toc-pg" />
            </a>
          </td>
        </tr>
      </tbody>
    </table>
  </xsl:template>

但是,我认为将每一行放在自己的表中并不是一个很好的设计。你为什么选择这样做?

于 2013-03-20T08:21:56.980 回答
0

笔记:

  1. 您的标题表无效 - 它没有<tr>.
  2. 您的 XSLT 给您重复的原因toc-title是您为每次toc-subitem处理打印它。在下面的解决方案中,它只打印一次。

对您的 XSLT 进行以下更改:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

<xsl:template match="//toc-item">
  <table>
    <td>
      <!-- Print the title *once* -->
      <xsl:value-of select="./toc-title"/>
    </td>
  </table>

  <!-- Process each subitem -->
  <xsl:apply-templates select="toc-subitem"/>
</xsl:template>

<xsl:template match="toc-subitem">

  <!-- Removed the <table> with the preceding sibling -->

  <xsl:variable name="tocpg">
    <xsl:value-of select="current()/toc-pg"/></xsl:variable>

  <!-- Put the rest of your XSLT here, no changes there -->

 </xsl:template>
</xsl:stylesheet>
于 2013-03-20T08:22:45.420 回答
0

我真的不明白你为什么想要这样的输出(<td>不是 的有效子级<table>,例如,在 HTML 中),但是这个样式表应该让你更接近(我重写了样式表以将你的代码拆分为单独的<xsl:template>元素):

样式表

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" encoding="utf-8" indent="yes"/>

  <xsl:template match="/">
    <html>
      <body>
        <table>
          <xsl:apply-templates/>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="toc-item/toc-title">
      <td><xsl:value-of select="."/></td>
  </xsl:template>

  <xsl:template match="toc-subitem">
    <!--
    I don't know how your code that fetches the TOC item level is supposed
    to work so you'll have to figure that out yourself.
    -->
    <table class="toc-item-$level-level">
      <tbody>
        <tr>
          <xsl:apply-templates select="@* | node()"/>
        </tr>
      </tbody>
    </table>
  </xsl:template>

  <xsl:template match="@num">
    <td class="toc-subitem-num">
      <xsl:value-of select="."/>
    </td>
  </xsl:template>

  <xsl:template match="toc-subitem/toc-title">
    <td class="toc-title">
      <xsl:value-of select="."/>
    </td>
  </xsl:template>

  <xsl:template match="toc-pg">
    <td class="toc-pg">
      <a href="{concat('er:#BVI_CH_0', substring-before(., '.'), '/P', translate(., '.', '-'))}">
        <xsl:value-of select="."/>
      </a>
    </td>
  </xsl:template>

</xsl:stylesheet>

输入

<?xml version="1.0"?>
<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>

输出

<html>
  <body>
    <table>
      <td>CHAPTER 1 INTRODUCTION</td>
      <table class="toc-item-$level-level">
        <tbody>
          <tr>
            <td class="toc-subitem-num">1.</td>
            <td class="toc-title">The British Virgin Islands</td>
            <td class="toc-pg">
              <a href="er:#BVI_CH_01/P1-001">1.001</a>
            </td>
          </tr>
        </tbody>
      </table>
      <table class="toc-item-$level-level">
        <tbody>
          <tr>
            <td class="toc-subitem-num">2.</td>
            <td class="toc-title">History and early constitutional developments</td>
            <td class="toc-pg">
              <a href="er:#BVI_CH_01/P1-003">1.003</a>
            </td>
          </tr>
        </tbody>
      </table>
      <table class="toc-item-$level-level">
        <tbody>
          <tr>
            <td class="toc-subitem-num">3.</td>
            <td class="toc-title">Development as a financial centre</td>
            <td class="toc-pg">
              <a href="er:#BVI_CH_01/P1-008">1.008</a>
            </td>
          </tr>
        </tbody>
      </table>
      <table class="toc-item-$level-level">
        <tbody>
          <tr>
            <td class="toc-subitem-num">4.</td>
            <td class="toc-title">Common Law and Equity</td>
            <td class="toc-pg">
              <a href="er:#BVI_CH_01/P1-015">1.015</a>
            </td>
          </tr>
        </tbody>
      </table>
      <table class="toc-item-$level-level">
        <tbody>
          <tr>
            <td class="toc-subitem-num">5.</td>
            <td class="toc-title">Statutes</td>
            <td class="toc-pg">
              <a href="er:#BVI_CH_01/P1-017">1.017</a>
            </td>
          </tr>
        </tbody>
      </table>
      <table class="toc-item-$level-level">
        <tbody>
          <tr>
            <td class="toc-subitem-num">6.</td>
            <td class="toc-title">Taxation</td>
            <td class="toc-pg">
              <a href="er:#BVI_CH_01/P1-022">1.022</a>
            </td>
          </tr>
        </tbody>
      </table>
    </table>
  </body>
</html>

如果您真的不需要它们,只需从第一条规则中删除<html>and元素。<body><xsl:template>

于 2013-03-20T08:29:42.240 回答