使用XSLT2对相同的xml元素进行分组

使用XSLT2对相同的xml元素进行分组,xml,xslt,Xml,Xslt,我有一个重复了相同元素的xml文档。我需要将相邻的div元素放入另一个元素中。下面我的xml文档包含p和div元素。所有div元素都应分组在分区内。如果是单个或相邻的 <doc> <section> <p class="h1">the fisr A</p> <p class="txt">one</p> <p>tow</p>

我有一个重复了相同元素的xml文档。我需要将相邻的div元素放入另一个元素中。下面我的xml文档包含p和div元素。所有div元素都应分组在分区内。如果是单个或相邻的

   <doc>
    <section>
        <p class="h1">the fisr A</p>
        <p class="txt">one</p>
        <p>tow</p>
        <div>the sec B</div>
        <div>theree</div>
        <div>theree</div>
        <p class="h2">the sec sec B</p>
        <p class="txt">the next text</p>
        <p class="h3">the fisr C</p>
        <p class="txt">four</p>
        <div>five</div>
        <div>the seccond A</div>
        <p class="txt">the seccond txt</p>
        <p class="h2">the second B</p>
    </section>
    <section>
        <p class="txt">six</p>
        <div>seven</div>
        <p class="txt">eight</p>
        <p class="txt">nine</p>
    </section>
</doc>

fisr A

一个

证券交易委员会 特雷 特雷

秒B

下一个文本

fisr C

四个

五 第二个A

第二个文本

第二个B

六个

七 八个

我需要将相邻的div元素放在一个父元素分区中。下面是我的输出

   <doc>
    <section>
        <p class="h1">the fisr A</p>
        <p class="txt">one</p>
        <p>tow</p>
        <division>
            <div>the sec B</div>
            <div>theree</div>
            <div>theree</div>
        </division>
        <p class="h2">the sec sec B</p>
        <p class="txt">the next text</p>
        <p class="h3">the fisr C</p>
        <p class="txt">four</p>
        <division>
            <div>five</div>
            <div>the seccond A</div>
        </division>
        <p class="txt">the seccond txt</p>
        <p class="h2">the second B</p>
    </section>
    <section>
        <p class="txt">six</p>
        <division>
            <div>seven</div>
        </division>
        <p class="txt">eight</p>
        <p class="txt">nine</p>
    </section>
</doc>

fisr A

一个

证券交易委员会 特雷 特雷

秒B

下一个文本

fisr C

四个

五 第二个A

第二个文本

第二个B

六个

七 八个

我已经尝试了下面的xslt

<?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" method="xml"/>
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="body">
    <xsl:copy>
      <xsl:for-each-group select="div" group-adjacent="div">
        <xsl:choose>
          <xsl:when test="current-grouping-key()">
            <division>
              <xsl:apply-templates select="current-group()"/>
            </division>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="current-group()"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>


请有人来帮我。

您有一个与
正文
匹配的模板,但XML中没有
正文
元素。你可能是指这里的
部分

对于分组,您实际上应该选择所有元素,而不仅仅是div,然后对于相邻的组,您需要一个表达式,该表达式在元素为div时返回true,否则返回false

试试这个XSLT

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

  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="section">
    <xsl:copy>
      <xsl:for-each-group select="*" group-adjacent="boolean(self::div)">
        <xsl:choose>
          <xsl:when test="current-grouping-key()">
            <division>
              <xsl:apply-templates select="current-group()"/>
            </division>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="current-group()"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>