【问题标题】:Grouping same xml elements using XSLT 2使用 XSLT 2 对相同的 xml 元素进行分组
【发布时间】:2019-05-05 18:38:48
【问题描述】:

我有一个重复相同元素的 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>

我需要将相邻的 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>

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


    【解决方案1】:

    您有一个匹配 body 的模板,但您的 XML 中没有 body 元素。您可能指的是section。

    对于分组,您实际上应该选择所有元素,而不仅仅是 div,然后对于 group-adjacent,您需要一个表达式,如果元素是 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多