use*_*778 7 xml xslt aggregate muenchian-grouping
Muenchian分组如何在细节上工作?
我有一个从数据库生成的简单XML文档:
<CLIENTS>
<CLIENT>
<NAME>John</NAME>
<ACCOUNT_NUMBER>1424763562761</ACCOUNT_NUMBER>
<LAST_USED>2012-10-03</LAST_USED>
<AMOUNT>5000</AMOUNT>
</CLIENT>
<CLIENT>
<NAME>John</NAME>
<ACCOUNT_NUMBER>543667543732</ACCOUNT_NUMBER>
<LAST_USED>2012-10-02</LAST_USED>
<AMOUNT>10000</AMOUNT>
</CLIENT>
...
Run Code Online (Sandbox Code Playgroud)
我想按名称节点分组.我怎样才能获得以下所需的输出?
<ClIENTS>
<CLIENT>
<NAME>John</NAME>
<ACCOUNT>
<ACCOUNT_NUMBER>1424763562761</ACCOUNT_NUMBER>
<LAST_USED>2012-10-03</LAST_USED>
<AMOUNT>5000</AMOUNT>
</ACCOUNT>
<ACCOUNT>
<ACCOUNT_NUMBER>543667543732</ACCOUNT_NUMBER>
<LAST_USED>2012-10-03</LAST_USED>
<AMOUNT>10000</AMOUNT>
</ACCOUNT>
....
</CLIENTS>
Run Code Online (Sandbox Code Playgroud)
Mar*_*nen 10
阅读www.jenitennison.com/xslt/grouping/muenchian.xml,获取代码定义密钥的帮助
<xsl:key name="client-by-name" match="CLIENT" use="NAME"/>
Run Code Online (Sandbox Code Playgroud)
然后使用模板作为
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CLIENTS">
<xsl:copy>
<xsl:apply-templates select="CLIENT[generate-id() = generate-id(key('client-by-name', NAME)[1])]" mode="group"/>
<xsl:copy>
</xsl:template>
<xsl:template match="CLIENT" mode="group">
<xsl:copy>
<xsl:copy-of select="NAME"/>
<xsl:apply-templates select="key('client-by-name', NAME)"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CLIENT">
<ACCOUNT>
<xsl:apply-templates select="node()[not(self::NAME)]"/>
</ACCOUNT>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
[编辑]如果你想使用XSLT 2.0,那么当然你不需要Muenchian分组,而是你使用
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* , node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="CLIENTS">
<xsl:copy>
<xsl:for-each-group select="CLIENT" group-by="NAME">
<CLIENT>
<xsl:apply-templates select="NAME, current-group()"/>
</CLIENT>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="CLIENT">
<ACCOUNT>
<xsl:apply-templates select="node() except NAME"/>
</ACCOUNT>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)