如何在XSL中对元素进行分组?

时间:2015-02-26 09:24:50

标签: xslt-2.0

我有以下过程的HTML。

<p class="Reference_Unnum" id="RefId_2"><span class="first_name" dir="Author" value="Klingmann">Klingmann, </span><span class="last_name" dir="Author" value="Anna.">A</span>, <span class="first_name" dir="Author" value="John">John, </span><span class="last_name" dir="Author" value="Kumar">K</span>, <span class="first_name" dir="Author" value="William">William, </span><span class="last_name" dir="Author" value="Jaya">J</span> &amp; <span class="first_name" dir="Author" value="Hannah">Hannah, </span><span class="last_name" dir="Author" value="Leslie.">L</span> <span class="chapter_title" value="Sample Chapter title">Sample Chapter title. </span>In: <span class="first_name" dir="Editor" value="Mihail">Mihail, </span><span class="last_name" dir="Editor" value="Popescu">P</span>, <span class="first_name" dir="Editor" value="Dong">Dong, </span><span class="last_name" dir="Editor" value="Xu">X</span> &amp; <span class="first_name" dir="Editor" value="Hannah">Hannah, </span><span class="last_name" dir="Editor" value="Leslie.">L</span> (eds.) <span class="book_title" value="Sample Book title">Sample Book title.</span></p>

作者和编辑名称应该是组。如何分组名字和姓氏。

输出应如下所示。

 <reference>
  <authors>
   <author>
     <given-name>Klingmann</given-name>
     <surname>A</surname>
   </author>
   <author>
    <given-name>John</given-name>
    <surname>K</surname>
   </author>
   <author>
    <given-name>William</given-name>
    <surname>J</surname>
   </author>
   <author>
    <given-name>Hannah</given-name>
    <surname>L</surname>
   </author>
  </authors>
  <chapter-title>Sample Chapter title</chapter-title>
  <editors>
   <editor>
    <given-name>Mihail</given-name>
    <surname>P</surname>
   </editor>
   <editor>
    <given-name>Dong</given-name>
    <surname>X</surname>
   </editor>
   <editor>
    <given-name>Hannah</given-name>
    <surname>L</surname>
   </editor>
  </editors>
  <book-title>Sample Book title</book-title>
 </reference>

提前致谢。

1 个答案:

答案 0 :(得分:1)

以下是样式表示例:

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

<xsl:output indent="yes"/>

<xsl:template match="p[@class = 'Reference_Unnum']">
  <reference>
    <xsl:for-each-group select="span" group-by="(@dir, @class)[1]">
      <xsl:choose>
        <xsl:when test="current-group()[2]">
          <xsl:element name="{lower-case(@dir)}s">
            <xsl:for-each-group select="current-group()" group-starting-with="span[@class = 'first_name']">
              <xsl:element name="{lower-case(@dir)}">
                <xsl:apply-templates select="current-group()"/>
              </xsl:element>
            </xsl:for-each-group>
          </xsl:element>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="."/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
  </reference>
</xsl:template>

<xsl:template match="span[@class = 'first_name']">
  <given-name>
    <xsl:value-of select="@value"/>
  </given-name>
</xsl:template>

<xsl:template match="span[@class = 'last_name']">
  <surname>
    <xsl:value-of select="."/>
  </surname>
</xsl:template>

<xsl:template match="span[@class = 'chapter_title']">
  <chapter-title>
    <xsl:value-of select="@value"/>
  </chapter-title>
</xsl:template>

<xsl:template match="span[@class = 'book_title']">
  <book-title>
    <xsl:value-of select="@value"/>
  </book-title>
</xsl:template>

</xsl:stylesheet>
相关问题