Grouping and counting in XSLT 1.0

故事扮演 提交于 2019-12-11 02:46:38

问题


I have input 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

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.

How the logic will work?


回答1:


In XSLT 1.0, using Muenchian grouping:

<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>

Output:

Paket 2
Parti 1
Styche 2


来源:https://stackoverflow.com/questions/19828294/grouping-and-counting-in-xslt-1-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!