【问题标题】:Nested grouping elements using XSLT使用 XSLT 嵌套分组元素
【发布时间】:2014-12-27 17:41:08
【问题描述】:

我需要对元素进行分组并为这些组应用 div 元素。另请注意,元素级别是可选的,应相应地进行分组。应将级别“prelims”、“part”、“chapter”、“levela”、“levelb”和“endmatter”分组。另请注意,如果“levelc”存在,那么它也应该被分组

输入:

<toc>
<toc.entry level="prelims">Half Titlepage</toc.entry>
<toc.entry level="prelims">Titlepage</toc.entry>
<toc.entry level="prelims">Imprint</toc.entry>
<toc.entry level="part">Part 1. This is part title</toc.entry>
<toc.entry level="chapter">Chapter 1. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<toc.entry level="levela">This is level a head</toc.entry>
<toc.entry level="levelb">This is level b head</toc.entry>
<toc.entry level="levelb">This is level b head</toc.entry>
<toc.entry level="levelb">This is level b head</toc.entry>
<toc.entry level="chapter">Chapter 2. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<toc.entry level="levela">This is level a head</toc.entry>
<toc.entry level="chapter">Chapter 3. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<toc.entry level="levela">This is level a head</toc.entry>
<toc.entry level="part">Part 2. This is part title</toc.entry>
<toc.entry level="chapter">Chapter 4. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<toc.entry level="chapter">Chapter 5. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<toc.entry level="chapter">Chapter 6. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<toc.entry level="endmatter">References</toc.entry>
<toc.entry level="endmatter">Index</toc.entry>
</toc>

需要输出

<toc>
<div class="prelims">
<toc.entry level="prelims">Half Titlepage</toc.entry>
<toc.entry level="prelims">Titlepage</toc.entry>
<toc.entry level="prelims">Imprint</toc.entry>
</div>
<div class="part">
<toc.entry level="part">Part 1. This is part title</toc.entry>
<div class="chapter">
<toc.entry level="chapter">Chapter 1. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<div class="levela">
<toc.entry level="levela">This is level a head</toc.entry>
<div class="levelb">
<toc.entry level="levelb">This is level b head</toc.entry>
<toc.entry level="levelb">This is level b head</toc.entry>
<toc.entry level="levelb">This is level b head</toc.entry>
</div>
</div>
</div>
<div class="chapter">
<toc.entry level="chapter">Chapter 2. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<div class="levela">
<toc.entry level="levela">This is level a head</toc.entry>
</div>
</div>
<div class="chapter">
<toc.entry level="chapter">Chapter 3. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
<div class="levela">
<toc.entry level="levela">This is level a head</toc.entry>
</div>
</div>
</div>
<div class="part">
<toc.entry level="part">Part 2. This is part title</toc.entry>
<div class="chapter">
<toc.entry level="chapter">Chapter 4. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
</div>
<div class="chapter">
<toc.entry level="chapter">Chapter 5. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
</div>
<div class="chapter">
<toc.entry level="chapter">Chapter 6. This is chapter title</toc.entry>
<toc.entry level="author">This is author</toc.entry>
</div>
</div>
<div class="endmatter">
<toc.entry level="endmatter">References</toc.entry>
<toc.entry level="endmatter">Index</toc.entry>
</div>
</toc>

XSLT 已尝试

<?xml version='1.0'?>
<xsl:stylesheet version="2.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:RSUITE="http://www.reallysi.com" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:epub="http://www.idpf.org/2007/ops" epub:prefix="index: http://www.index.com/" exclude-result-prefixes="RSUITE">
<xsl:import href="Entity.xsl"/>
<xsl:output method="xml" indent="no"/>


<xsl:template match="toc">
<toc><xsl:apply-templates select="toc.entry"/></toc>
</xsl:template>

<xsl:template match="toc.entry">
<xsl:choose>
<xsl:when test="@level='prelims'"><div class="prelims"><xsl:for-each select="@level='prelims'"><xsl:apply-templates/></xsl:for-each></div></xsl:when>
<xsl:when test="@level='levela'"><xsl:for-each select="@level='levela'"><div class="levela"><xsl:apply-templates/></div></xsl:for-each></xsl:when>
</xsl:choose>
 </xsl:template>

</xsl:stylesheet>

【问题讨论】:

  • 您使用的是哪个版本的 XSLT?你的例子也不清楚,为什么作者在章节下分组?
  • @Rnet:我已经编辑了我的问题。我正在使用 XSLT 2.0。 author 归类在章节下,因为它类似于那些章节的作者。另请注意,某些 XML 可能没有作者
  • @siva2012,如果答案符合您的要求,请接受。

标签: xml xslt xslt-2.0


【解决方案1】:

如果你想分组,为什么不使用the proper grouping tool

XSLT 2.0

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

<xsl:template match="/toc">
    <toc>
        <!-- PRELIMS -->
        <div class="prelims">
            <xsl:copy-of select="toc.entry[@level='prelims']"/>
        </div>
        <!-- PARTS -->
        <xsl:for-each-group select="toc.entry[not(@level='prelims' or @level='endmatter')]" group-starting-with="toc.entry[@level='part']">
            <div class="part">
                <xsl:copy-of select="current-group()[@level='part']"/>
                <!-- CHAPTERS -->
                <xsl:for-each-group select="current-group()[not(@level='part')]" group-starting-with="toc.entry[@level='chapter']">
                    <div class="chapter">
                        <!-- CHAPTER & AUTHOR -->
                        <xsl:copy-of select="current-group()[@level='chapter' or @level='author']"/>
                        <!-- LEVEL A (OPTIONAL) -->
                        <xsl:if test="current-group()[@level='levela']">
                            <div class="'levela">
                                <xsl:copy-of select="current-group()[@level='levela']"/>
                                <!-- LEVEL B (OPTIONAL) -->
                                <xsl:if test="current-group()[@level='levelb']">
                                    <div class="'levelb">
                                        <xsl:copy-of select="current-group()[@level='levelb']"/>
                                        <!-- LEVEL C (OPTIONAL) -->
                                        <xsl:if test="current-group()[@level='levelc']">
                                            <div class="'levelc">
                                                <xsl:copy-of select="current-group()[@level='levelc']"/>
                                            </div>  
                                        </xsl:if>
                                    </div>
                                </xsl:if>
                            </div>
                        </xsl:if>
                    </div>
                </xsl:for-each-group>
            </div>
        </xsl:for-each-group>
        <!-- END MATTER -->
        <div class="endmatter">
            <xsl:copy-of select="toc.entry[@level='endmatter']"/>
        </div>
    </toc>
</xsl:template>

</xsl:stylesheet>

【讨论】:

  • 漂亮的代码使用分组,+1,'levelb'嵌套在'levela'下。
  • @michael.hor257k 非常感谢您的编码。它对我理解分组的概念很有帮助
  • @RudramuniTP 我现在才明白你的意思。谢谢,我已经修好了。
  • @michael.hor257k,先生,感谢您的建议和更新。
【解决方案2】:

遵循 XSLT

<?xml version='1.0'?>
<xsl:stylesheet version="2.0"  xmlns="http://www.w3.org/1999/xhtml"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:RSUITE="http://www.reallysi.com" 
     xmlns:m="http://www.w3.org/1998/Math/MathML" 
     xmlns:epub="http://www.idpf.org/2007/ops" 
     epub:prefix="index: http://www.index.com/" 
     exclude-result-prefixes="RSUITE epub m">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:template match="/">
    <toc>
      <div class="prelims">
        <xsl:apply-templates select="//toc.entry[@level='prelims']"/>
      </div>
      <xsl:for-each select="//toc.entry[@level='part']">
        <div class="part">
          <xsl:apply-templates select="." mode="part">
            <xsl:with-param name="positionPart" select="position()"/>
          </xsl:apply-templates>
        </div>
      </xsl:for-each>
      <div class="endmatter">
        <xsl:apply-templates select="//toc.entry[@level='endmatter']"/>
      </div>
    </toc>
  </xsl:template>
  <xsl:template match="toc.entry" mode="part">
  <xsl:param name="positionPart"/>
   <xsl:apply-templates select="."/>
   <xsl:for-each select="following-sibling::*[@level='chapter'
        and not(count(preceding-sibling::*[@level='part']) > $positionPart)]">
      <xsl:variable name="positionChapter" select="position()"/>
        <div class="chapter">
          <xsl:apply-templates select="." mode="chapter">
            <xsl:with-param name="positionPart" select="$positionPart"/>
            <xsl:with-param name="positionChapter" select="$positionChapter"/>
          </xsl:apply-templates>
        </div>
   </xsl:for-each>
  </xsl:template>
  <xsl:template match="toc.entry" mode="chapter" >
  <xsl:param name="positionPart"/>
  <xsl:param name="positionChapter"/>
  <xsl:apply-templates select="."/>
  <xsl:apply-templates select="following-sibling::*[1][@level='author']"/>
    <xsl:for-each select="following-sibling::*[@level='levela'
      and not(count(preceding-sibling::*[@level='chapter']) > $positionChapter)]">
      <div class="levela">
        <xsl:apply-templates select="."/>
        <xsl:if test="following-sibling::*[@level='levelb'
             and not(count(preceding-sibling::*[@level='chapter']) > $positionChapter)]">
          <div class="levelb">
            <xsl:for-each select="following-sibling::*[@level='levelb'
                 and not(count(preceding-sibling::*[@level='chapter']) > $positionChapter)]">
              <xsl:apply-templates select="."/>
            </xsl:for-each>
          </div>
        </xsl:if>
      </div>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="toc.entry">
    <toc.entry>
      <xsl:attribute name="level" select="@level"/>
      <xsl:value-of select="."/>
    </toc.entry>
  </xsl:template>
</xsl:stylesheet>

当应用于您的输入时,XML 会产生输出

<toc xmlns="http://www.w3.org/1999/xhtml">
<div class="prelims">
  <toc.entry level="prelims">Half Titlepage</toc.entry>
  <toc.entry level="prelims">Titlepage</toc.entry>
  <toc.entry level="prelims">Imprint</toc.entry>
</div>
<div class="part">
  <toc.entry level="part">Part 1. This is part title</toc.entry>
  <div class="chapter">
     <toc.entry level="chapter">Chapter 1. This is chapter title</toc.entry>
     <toc.entry level="author">This is author</toc.entry>
     <div class="levela">
        <toc.entry level="levela">This is level a head</toc.entry>
        <div class="levelb">
           <toc.entry level="levelb">This is level b head</toc.entry>
           <toc.entry level="levelb">This is level b head</toc.entry>
           <toc.entry level="levelb">This is level b head</toc.entry>
        </div>
     </div>
  </div>
  <div class="chapter">
     <toc.entry level="chapter">Chapter 2. This is chapter title</toc.entry>
     <toc.entry level="author">This is author</toc.entry>
     <div class="levela">
        <toc.entry level="levela">This is level a head</toc.entry>
     </div>
  </div>
  <div class="chapter">
     <toc.entry level="chapter">Chapter 3. This is chapter title</toc.entry>
     <toc.entry level="author">This is author</toc.entry>
     <div class="levela">
        <toc.entry level="levela">This is level a head</toc.entry>
     </div>
  </div>
</div>
<div class="part">
  <toc.entry level="part">Part 2. This is part title</toc.entry>
  <div class="chapter">
     <toc.entry level="chapter">Chapter 4. This is chapter title</toc.entry>
     <toc.entry level="author">This is author</toc.entry>
  </div>
  <div class="chapter">
     <toc.entry level="chapter">Chapter 5. This is chapter title</toc.entry>
     <toc.entry level="author">This is author</toc.entry>
  </div>
  <div class="chapter">
     <toc.entry level="chapter">Chapter 6. This is chapter title</toc.entry>
     <toc.entry level="author">This is author</toc.entry>
  </div>
  </div>
  <div class="endmatter">
   <toc.entry level="endmatter">References</toc.entry>
   <toc.entry level="endmatter">Index</toc.entry>
  </div>
</toc>

我保留了示例 XSLT 中的名称空间,尽管我使用的在线 XSLT 处理器 (Saxon 9.5.1.6) 不需要它们。如果需要从输出中的 toc 根元素中删除 xmlns-namespace,则可以将其从样式表声明中删除。

解释:由于prelimsendmatter级别的元素应该显示在XML的开头/结尾,它们只是应用在匹配根的主模板中在具有适当类的div 容器中。
所有part 级别都以mode="part" 和当前部件的位置作为参数应用于模板。要生成属于当前部分的所有章节,模板仅适用于所有级别为 chapter 的后续兄弟,这些兄弟之前没有更多级别为 part 作为当前 part 元素的兄弟:

<xsl:for-each select="following-sibling::*[@level='chapter'
    and not(count(preceding-sibling::*[@level='part']) > $positionPart)]">
    ....

以当前章节的位置作为附加参数。
模板mode="chapter" 只会在章节的下一个兄弟级别为author 的情况下生成作者:

<xsl:apply-templates select="following-sibling::*[1][@level='author']"/>  

要只生成属于当前章节的levela级别元素,只选择没有更多前面章节的levela元素作为当前章节:

<xsl:for-each select="following-sibling::*[@level='levela'
     and not(count(preceding-sibling::*[@level='chapter']) > $positionChapter)]">
     ...

levelb 级别元素采用了类似的方法。
由于尚不清楚章节是否可能没有levela 元素,因此我在编写div 容器之前只添加了对levelb 的检查:

<xsl:if test="following-sibling::*[@level='levelb'
    and not(count(preceding-sibling::*[@level='chapter']) > $positionChapter)]">
    <div class="levelb">
    ...

我已将可能包含 levelc 元素的部分保留了出去 - 如果这种方法对您有用,添加它应该不会有太大问题。
此外,我想注意结果取决于您使用的 XSLT 处理器 - 例如当切换到 Saxon 6.5.5 时,以上 XSLT 不会生成 level 属性的值。
为了方便或进一步测试,我在此处保存了上面的示例:http://xsltransform.net/eiZQaF7

【讨论】:

  • 向你的逻辑致敬,+1。
  • @matthias_h 感谢您的详细方法
猜你喜欢
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-26
  • 2014-01-01
相关资源
最近更新 更多