代码之家  ›  专栏  ›  技术社区  ›  Reegan

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

  •  1
  • Reegan  · 技术社区  · 7 年前

    我有一个 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>
    

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

    我已经尝试了下面的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>
    

    请有人来帮我。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Tim C    7 年前

    body 但是没有 身体 XML中的元素。你可能是说 section 在这里

    试试这个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>