XSLT:变量中的 filter/group 个节点

XSLT: filter/group nodes in variables

我正在尝试 filter/group 变量中的节点。

XML:

<?xml version="1.0" encoding="UTF-8"?>
<row>
    <cell n="1"/>
    <cell n="2"/>
    <cell n="3"/>
    <cell n="4"/>
    <cell n="2"/>
    <cell n="5"/>
</row>

XSLT 2:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="row">
        <xsl:element name="row">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>   

    <xsl:template match="cell">
        <xsl:variable name="attribute" select="@n"/>
        <xsl:variable name="sequence">
            <xsl:call-template name="seq">
                <xsl:with-param name="attribute" select="$attribute"/>
            </xsl:call-template>
        </xsl:variable>
        <xsl:for-each-group select="$sequence" group-by="cell/@val">
            <xsl:sequence select="."></xsl:sequence>
        </xsl:for-each-group>
    </xsl:template>

    <xsl:template name="seq">
        <xsl:param name="attribute"/>
        <xsl:element name="cell">
            <xsl:attribute name="val">
                <xsl:value-of select="$attribute"/>
            </xsl:attribute>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

预期输出:

<?xml version="1.0" encoding="UTF-8"?>
<row>
    <cell val="1"/>
    <cell val="2"/>
    <cell val="3"/>
    <cell val="4"/>
    <cell val="5"/>
</row>

XML-代码只是一个更复杂文件的示例。我想要做的是首先处理所有元素 cell 并将结果保存在一个变量中。在第二个 运行 中,我想过滤或重新组合序列的节点。

我还尝试使用谓词过滤 $sequence

<xsl:sequence select="$sequence/*[@val != preceding-sibling::cell/@val]"/> 

在这种情况下,输出文件为空。

编辑 2(现在有效):

<?xml version="1.0" encoding="UTF-8"?>

<xsl:template match="row">
    <xsl:variable name="sequence">
        <xsl:for-each select="cell">
            <xsl:variable name="attribute" select="@n"/>
            <xsl:call-template name="seq">
                <xsl:with-param name="attribute" select="$attribute"/>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:variable>
    <row>
        <xsl:for-each-group group-by="@val" select="$sequence/*">
            <xsl:sequence select="."/>
        </xsl:for-each-group>
    </row>
</xsl:template>

<xsl:template name="seq">
    <xsl:param name="attribute"/>
    <xsl:element name="cell">
        <xsl:attribute name="val">
            <xsl:value-of select="$attribute"/>
        </xsl:attribute>
    </xsl:element>
</xsl:template>

这是一个 XSLT 2.0 样式表,它首先将 <cell n="x">...</cell> 转换为 <cell val="n">...</cell>,然后通过 val 属性对这些单元格元素进行分组以消除重复项:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:output indent="yes"/>

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

    <xsl:template match="row">
        <xsl:copy>
            <xsl:variable name="transformed-cells">
                <xsl:apply-templates select="cell"/>
            </xsl:variable>
            <xsl:for-each-group select="$transformed-cells/cell" group-by="@val">
                <xsl:copy-of select="."/>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="cell/@n">
        <xsl:attribute name="val" select="."/>
    </xsl:template>

</xsl:transform>

输出是

<row>
   <cell val="1"/>
   <cell val="2"/>
   <cell val="3"/>
   <cell val="4"/>
   <cell val="5"/>
</row>

http://xsltransform.net/a9Giwz