current-group() 除了 xslt 中的一个元素

current-group() except for one element in xslt

以下是我的输入xml。我正在尝试使用 current-group() 函数进行分组,但它不符合我的要求,下面我提供了详细信息。

        <UsrTimeCardEntry>
            <Code>1<Code>
            <Name>TC1</Name>
            <Person>
                <Code>074</Code>
            </Person>
        </UsrTimeCardEntry>
        <UsrTimeCardEntry>
            <Code>2<Code>
            <Name>TC2</Name>
            <Person>
                <Code>074</Code>
            </Person>
        </UsrTimeCardEntry>

我想按Person/Code分组,这样看起来像这样

   <Person Code="074">
       <UsrTimeCardEntry>
              <Code>1</Code>
              <Name>TC1</Name>
       </UsrTimeCardEntry>
       <UsrTimeCardEntry>
              <Code>2</Code>
              <Name>TC2</Name>
       </UsrTimeCardEntry>
</Person>

为此我正在使用下面的 xslt,但它再次复制了我不想要的人,我在这里缺少的是什么,我尝试使用 current-group() except 和 not[ child::Person] 但这也行不通。

<xsl:template match="businessobjects">
    <xsl:for-each-group select="UsrTimeCardEntry" group-by="Person/Code">
        <Person Code="{current-grouping-key()}">
            <xsl:copy-of select="current-group()"></xsl:copy-of>
        </Person>
    </xsl:for-each-group>
</xsl:template>

这里不使用xsl:copy-of,而是使用xsl:apply-templates,然后可以添加模板忽略Person节点

<xsl:template match="Person" />

这假定您还使用身份模板正常复制所有其他节点。

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

试试这个 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:strip-space elements="*" />

    <xsl:template match="businessobjects">
        <xsl:for-each-group select="UsrTimeCardEntry" group-by="Person/Code">
            <Person Code="{current-grouping-key()}">
                <xsl:apply-templates select="current-group()" />
            </Person>
        </xsl:for-each-group>
    </xsl:template>

    <xsl:template match="Person" />

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