如何按XSL中的元素进行分组

时间:2016-11-28 10:29:14

标签: xslt-2.0

我有以下过程的HTML。

<p class="Ahead">FIRST SECTION</p>
  <p class="Text">with a moisture content of 16.34</p>
  <p class="Ahead">SECOND SECTION</p>
    <p class="Bhead">Second First Section</p>
      <p class="Text">with a moisture content of 20.56</p>
      <p class="Chead">Second first first section</p>
        <p class="Text">with a moisture content of 48.15</p>

Ahead,Bhead和Chead应该是个人团体。如何对其进行分组。

输出应如下所示。

<sec>
  <title>FIRST SECTION</title>
  <p class="Text">with a moisture content of 16.34</p>
</sec>
<sec>
  <title>SECOND SECTION</title>
  <sec
    <title>Second First Section</title>
    <p class="Text">with a moisture content of 20.56</p>
    <sec>
      <title>Second first first section</title>
      <p class="Text">with a moisture content of 48.15</p>
    </sec>
  </sec>
</sec>

提前致谢。

1 个答案:

答案 0 :(得分:0)

这样的事情:

<xsl:function name="f:group">
  <xsl:param name="level" as="xs:integer"/>
  <xsl:param name="population" as="element()*"/>
  <xsl:variable name="name" select="('Ahead', 'Bhead', 'Chead')[$level]"/>
  <xsl:for-each-group select="$population"
    group-starting-with="p[@class=$name]">
    <xsl:choose>
      <xsl:when test="@class='Text'">
       <xsl:copy-of select="current-group()"/>
      </xsl:when>
      <xsl:otherwise>
       <xsl:sequence select="f:group($level+1, current-group())"/>
      </xsl:otherwise>
    </xsl:choose> 
  </xsl:for-each-group>
</xsl:function>

然后:

<xsl:template match="/">
  <xsl:sequence select="f:group(1, //p)"/>
</xsl:template>

未经测试,只是为您开始。

相关问题