【问题标题】:duplication in xslt preceeding-siblingxslt 前同级中的重复
【发布时间】:2013-03-20 08:05:08
【问题描述】:

我有以下 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


    【解决方案1】:

    注意:

    1. 您的标题表无效 - 它没有 &lt;tr&gt;
    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>
    

    【讨论】:

    • 您好@kamituelthanks 的回复,当我应用您提供的 xslt 时得到的输出很好,但这里我想要 table> 结束
      第 1 章介绍
      标记应在
    【解决方案2】:

    您应该为此使用单独的模板(我还清理了一堆&lt;xsl:variable&gt;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>
    

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

    【讨论】:

    • 嗨 Rishe 这是我得到的实际要求,无论如何感谢更新,我会测试。
    • 这行不通,我得到的结构是
      CHAPTER 1 INTRODUCTION
      ,但我想要就像
      第一章介绍
      。谢谢
    • 嘿,感谢您的帮助。正如您所提到的,问题出在表格格式中。现在它是固定的
    【解决方案3】:

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

    样式表

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

    如果您真的不需要它们,只需从第一个 &lt;xsl:template&gt; 规则中删除 &lt;html&gt;&lt;body&gt; 元素。

    【讨论】:

    • 嗨@Eero 感谢您的回复,您显示的输出是我第一次尝试类似事情时得到的结果,但这里我想要
      第 1 章介绍 td>
      结束 标记应在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 2012-04-30
    • 1970-01-01
    • 2021-06-27
    • 2011-08-19
    相关资源
    最近更新 更多