XSLT 1.0 中的分组和计数 [英] Grouping and counting in XSLT 1.0

查看:38
本文介绍了XSLT 1.0 中的分组和计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我输入了 XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <FT>Paket</FT>
   <FT>Parti</FT>
   <FT>Paket</FT>
   <FT>Styche</FT>
   <FT>Styche</FT>
</root>

而且我希望我的输出显示如 -

And I want my output to display such as -

Paket   2
Parti   1
Styche  2

它是对元素的值和编号进行分组.显示重复值的总数.就像 Paket 指示的值,它在 XML 中重复了 2 次.

Its is grouping the value of elements and the no. is showing the total count of the value being repeated. Like Paket is indicating the value and it is being repeated 2 times in the XML.

逻辑将如何工作?

推荐答案

在 XSLT 1.0 中,使用 Muenchian 分组:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>
  <xsl:key name="k" match="FT" use="."/>

  <xsl:template match="/*">
    <xsl:apply-templates select="FT[generate-id() = generate-id(key('k', .))]"/>
  </xsl:template>

  <xsl:template match="FT">
    <xsl:value-of select="concat(., ' ', count(key('k', .)))"/>
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>

</xsl:stylesheet>

输出:

Paket 2
Parti 1
Styche 2

这篇关于XSLT 1.0 中的分组和计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆