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



我想用新的父元素包装元素组。此类组出现在文档中的许多位置。元素没有属性。 有了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>

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

尝试在xsl:for-each-group上使用group-adjacent,按名称对相邻节点进行分组,并将组包装在<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>

最新更新