Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xslt前同级中的重复_Xslt - Fatal编程技术网

xslt前同级中的重复

xslt前同级中的重复,xslt,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>

我有下面的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>
请告诉我哪里出了问题


谢谢

您应该为此使用单独的模板,我还清理了一堆:

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

注意:

您的标题表无效-它没有。 XSLT为您提供重复的toc标题的原因是您为处理的每个toc子项打印了它。在下面的解决方案中,它只打印一次。 对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>

我真的不明白为什么您希望这样的输出不是HTML中的有效子级,但是这个样式表应该让您更接近我重写了样式表,将代码拆分为单独的元素:

样式表 输入 输出
如果您真的不需要,只需从第一条规则中删除和元素。

嗨,Rishe,这是我得到的实际要求,无论如何,感谢更新,我将进行测试。Rishe,这不起作用,我得到的结构是第1章介绍,但我希望它与第1章介绍一样。谢谢你的帮助。正如您所提到的,问题出在athat表格式中。现在是fixedHi@Eero感谢您的回复,您显示的输出是我第一次尝试类似操作时得到的,但这里我想要第1章介绍结束标记应该在标记之前结束。谢谢在这种情况下,您原始问题中的预期输出是错误的。但是好的,我编辑了我的代码。嗨@kamituelthanks对于响应,我在应用您提供的xslt时得到的输出是好的,但是这里我想要第1章介绍结束标记应该在标记之前结束。谢谢
<?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>
<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>