【问题标题】:XSLT Grouping's : Add parent to set of elements in child elementsXSLT Grouping's:将父级添加到子元素中的元素集
【发布时间】:2022-01-12 06:59:33
【问题描述】:

使用 for-each-group 我试图通过每个 para 元素的类值添加父节点。我尝试应用分组,但结果不好,我没有得到想要的输出。我对在这种情况下使用分组感到困惑。在这种情况下是否有更好的方法来添加父节点?

当前 XML:

<?xml version="1.0" encoding="utf-8" ?>
<section>
  <h1>Some heading</h1>
  <section>
   <p>normal paragaraph</p>
   <p class="list">list 1</p>
   <p class="list">list 1</p>
  
    <p>normal paragaraph</p>
   <p class="list">list 2</p>
   <p class="list">list 2</p>
  </section>
    <section> ...  </section>
</section>

使用的 XSLT:

   <xsl:template match="section">
      <xsl:for-each-group select="node()" group-by="if (@class='list') then 'list' else 'nolist'">
        <xsl:for-each select="current-grouping-key()">
            <xsl:choose>
              <xsl:when test="current-grouping-key() = 'list'">
              <list>
                  <xsl:apply-templates select="current-group()" />
              </list>                
              </xsl:when>
              <xsl:otherwise>
                  <xsl:apply-templates select="current-group()" />
              </xsl:otherwise>
            </xsl:choose>

        </xsl:for-each>
      </xsl:for-each-group>
  </xsl:template>

电流输出:


<h1>Some heading</h1>
<p>normal paragaraph</p>
<p>normal paragaraph</p>
<list>
   <p class="list">list 1</p>
   <p class="list">list 1</p>
   <p class="list">list 2</p>
   <p class="list">list 2</p>
</list>

<p>normal paragaraph</p>
....

预期输出:

<section>
  <h1>Some heading</h1>
  <section>
   <p>normal paragaraph</p>
   <list>
      <p class="list">list 1</p>
      <p class="list">list 1</p>
   </list>
    <p>normal paragaraph</p>
   <list>
      <p class="list">list 2</p>
      <p class="list">list 2</p>
   </list>
</section>
<section>...</section>
</section>

【问题讨论】:

  • 查看现有的group-adjacent 示例xslt-grouping
  • :) 是的,这有帮助,谢谢你展示这个。

标签: xslt xslt-2.0 xslt-grouping


【解决方案1】:
<xsl:template match="section">
  <section>
    <h1>Some Heading</h1>
    <section>
        <xsl:for-each-group select="section/p" group-by=".">
            <xsl:choose>
              <xsl:when test="current-grouping-key() != 'normal paragaraph'">
                <p>normal paragaraph</p>
                <list>
                  <xsl:for-each select="current-group()">
                      <p class="list"><xsl:value-of select="current-grouping-key()"/></p>
                  </xsl:for-each>
                </list>
              </xsl:when>
            </xsl:choose>
        </xsl:for-each-group>
      </section>
  </section>
</xsl:template>

【讨论】:

  • 我希望我的贡献对某人有所帮助
【解决方案2】:

这是我希望这对某人有所帮助的答案,正如@marthin honnen 所评论的那样,我尝试使用 group-adjacent,得到了所需的输出,这很有帮助。

 <xsl:template match="section">
     <xsl:copy>
      <xsl:for-each-group select="*" group-adjacent="if (@class='list') then 'list' else 'nolist'">
        <xsl:for-each select="current-grouping-key()">
            <xsl:choose>
              <xsl:when test="current-grouping-key() = 'list'">
              <list>
                  <xsl:apply-templates select="current-group()" />
              </list>                
              </xsl:when>
              <xsl:otherwise>
                  <xsl:apply-templates select="current-group()" />
              </xsl:otherwise>
            </xsl:choose>

        </xsl:for-each>
      </xsl:for-each-group>
      </xsl:copy>
  </xsl:template>

【讨论】:

  • 考虑删除&lt;xsl:for-each select="current-grouping-key()"&gt;,因为current-grouping-key(在XSLT 2 或XSLT 3 中具有默认的composite="no" 键行为)是一个单一的值。所以你只需要xsl:choose/xsl:when
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
  • 2014-05-13
  • 1970-01-01
相关资源
最近更新 更多