当前-group(),xslt中的一个元素除外
下面是我的输入xml。我正在使用current-group()函数尝试分组,但它不符合我的要求,下面我提供了详细信息当前-group(),xslt中的一个元素除外,xslt,grouping,except,Xslt,Grouping,Except,下面是我的输入xml。我正在使用current-group()函数尝试分组,但它不符合我的要求,下面我提供了详细信息 <UsrTimeCardEntry> <Code>1<Code> <Name>TC1</Name> <Person> <Code>074</Code>
<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="074">
<UsrTimeCardEntry>
<Code>1</Code>
<Name>TC1</Name>
</UsrTimeCardEntry>
<UsrTimeCardEntry>
<Code>2</Code>
<Name>TC2</Name>
</UsrTimeCardEntry>
</Person>
为此,我使用了下面的xslt,但它再次复制了我不想要的人,我在这里遗漏了什么,我尝试使用current-group()而不是[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>