在XSLT中包装没有属性的节点组

问题描述 投票:0回答:1

我想用新的父元素包装元素组。这些组出现在文档的许多地方。元素没有属性。使用for-each-group,我设法将所有需要的文档元素包含在一个包含新父元素的大型组中,但我想根据它们的出现来区分这些组。发现了许多类似的问题和答案,但无法解决问题。

我的文件

<modul>
    <body>
        <Standard>some text</Standard>
        <Herausforderung>some text</Herausforderung>
        <Standard>some text
           <d2tb>some text</d2tb>
        </Standard>
        <Aufzhlungbullets>some text</Aufzhlungbullets>
        <Aufzhlungbullets>some text</Aufzhlungbullets>
        <Aufzhlungbullets>some text</Aufzhlungbullets>
        <Standard>some text
           <d2ti>some text</d2ti>
        </Standard0>
        <Aufzhlungbullets>some text</Aufzhlungbullets>
        <Aufzhlungbullets>some text</Aufzhlungbullets>
        <Aufzhlungbullets>some text</Aufzhlungbullets>
        <Standard>some text</Standard>
   </body>
</modul>

期望的输出

<modul>
        <body>
            <Standard>some text</Standard>
            <Herausforderung>some text</Herausforderung>
            <Standard>some text
               <d2tb>some text</d2tb>
            </Standard>
            <list>
               <Aufzhlungbullets>some text</Aufzhlungbullets>
               <Aufzhlungbullets>some text</Aufzhlungbullets>
               <Aufzhlungbullets>some text</Aufzhlungbullets>
            </list>
            <Standard>some text
               <d2ti>some text</d2ti>
            </Standard0>
            <list>
               <Aufzhlungbullets>some text</Aufzhlungbullets>
               <Aufzhlungbullets>some text</Aufzhlungbullets>
               <Aufzhlungbullets>some text</Aufzhlungbullets>
            </list>
            <Standard>some text</Standard>
       </body>
    </modul>

我的XSLT样式表

<xsl:template match="body">
        <list>
        <xsl:for-each-group select="Aufzhlungbullets" group-by=".">
                <xsl:copy-of select="current-group()"/>
        </xsl:for-each-group>
        </list>
    </xsl:template>

注意:我知道此模板会抑制所有其他内容。起初我集中精力让分组正确。文件其余部分的内容当然应该是可见的,但我不认为这是一个主要问题。

使用此模板,我只获得一个大组中的所有元素。

<modul>
    <list>
      <Aufzhlungbullets>some text</Aufzhlungbullets>
      <Aufzhlungbullets>some text</Aufzhlungbullets>
      <Aufzhlungbullets>some text</Aufzhlungbullets>
      <Aufzhlungbullets>some text</Aufzhlungbullets>
      <Aufzhlungbullets>some text</Aufzhlungbullets>
      <Aufzhlungbullets>some text</Aufzhlungbullets>
      <Aufzhlungbullets>some text</Aufzhlungbullets>
   </list>
</modul>

我尝试了很多模板的变化 - 没有成功。如何区分所需元素出现在文档中的不同组中?

xslt grouping nodes
1个回答
2
投票

尝试在group-adjacent上使用xsl:for-each-group,按照名称对相邻节点进行分组,然后将组包装在<list>标记中(如果它是Aufzhlungbullets节点):

<xsl:template match="body">
    <list>
        <xsl:for-each-group select="*" group-adjacent="local-name()">
            <xsl:choose>
                <xsl:when test="self::Aufzhlungbullets">
                    <list>
                        <xsl:copy-of select="current-group()" />    
                    </list>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="current-group()" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each-group>
    </list>
</xsl:template>
© www.soinside.com 2019 - 2024. All rights reserved.