使用XSLT嵌套分组元素

时间:2014-12-26 06:40:20

标签: xml xslt xslt-2.0

我需要分组元素并为这些组应用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 TRIED

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

2 个答案:

答案 0 :(得分:1)

遵循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个元素,我在编写{levelb之前只添加了div的格式<xsl:if test="following-sibling::*[@level='levelb' and not(count(preceding-sibling::*[@level='chapter']) > $positionChapter)]"> <div class="levelb"> ... 。 1}}容器:

levelc

我已经将可能出现level元素的部分遗漏了 - 如果这种方法对您有用,那么添加它不会有太大问题。
另外我想注意结果取决于你使用的XSLT处理器 - 例如切换到Saxon 6.5.5时,上面的XSLT不会生成{{1}}属性的值。
为方便起见或进一步测试,我在此处保存了上述示例:http://xsltransform.net/eiZQaF7

答案 1 :(得分: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>
相关问题