如何在XSLT 2.0中按xsl:for-each-group元素对TEXT / ELEMENT进行分组

时间:2019-03-28 10:13:20

标签: xslt-2.0

输入XML:

<overline-start id="tie1" specific-use="tie-bar"/>PtCl<sub>2</sub>(P((CH<sub>2</sub>)<sub><italic toggle="yes">n</italic></sub>)<sub>3</sub><overline-end rid="tie1"/>

期望的XML:

<overline id="tie1" specific-use="tie-bar">PtCl<sub>2</sub>(P((CH<sub>2</sub>)<sub><italic toggle="yes">n</italic></sub>)<sub>3</sub></overline>

MY XSLT 2.0代码:

<xsl:template match="overline-start">
<xsl:for-each-group select="self::overline-start" group-adjacent="self::overline-start[following-sibling::overline-end]">
<xsl:for-each select="current-group()">
<overline>
<xsl:apply-templates select="@*"/>
<xsl:copy-of select="current-group()"/>
</overline>
</xsl:for-each>

1 个答案:

答案 0 :(得分:0)

正如您在评论中提到的要求一样,我尝试了 @Martin Honnen 的方式:

假设输入为:

<?xml version="1.0" encoding="UTF-8"?>
<p>
      <overline-start id="tie1" specific-use="tie-bar"/>PtCl<sub>2</sub>(P((CH<sub>2</sub>)<sub><italic toggle="yes">n</italic></sub>)<sub>3</sub><overline-end rid="tie1"/>
</p>

2.0 解决方案可以是:

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output omit-xml-declaration="yes" indent="no" />

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
</xsl:template>

<xsl:template match="p">
    <overline>
        <xsl:for-each-group select="* | text()" group-starting-with="overline-start">
            <xsl:for-each-group select="current-group()" group-ending-with="overline-end">
                <xsl:apply-templates select="@*" />
                <xsl:sequence select="(current-group() except .) [position() != last()]" />
            </xsl:for-each-group>
        </xsl:for-each-group>
    </overline>
</xsl:template>
</xsl:stylesheet>

http://xsltfiddle.liberty-development.net/bnnZW8/1