假设我有这样一个XML文档:
哈利·波特魔法石J·K·罗琳·哈利·波特密室J·K·罗琳·哈利·波特阿兹卡班·J·K的囚徒罗琳
我希望我的XSLT文档能够找到标题中最常用的三个单词,以及它们的数量。(所以我想输出:“哈利”:3,“波特”:3,"of":2)。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<h2>3 Most Commonly Used Words</h2>
<ul>
<li>Word - Count</li>
</ul>
</xsl:template>
</xsl:stylesheet>我是一个XML初学者,不清楚如何使用XSLT和XPath进行聚合。我想把tokenize()和sum()组合起来?有人能给我指明正确的方向吗?
发布于 2013-11-05 01:07:58
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<h2>3 Most Commonly Used Words</h2>
<ul>
<xsl:for-each-group group-by="." select="
for $w in //title/tokenize(., '\W+') return $w">
<xsl:sort select="count(current-group())" order="descending" />
<xsl:if test="position() lt 4">
<li>
<xsl:value-of select="current-grouping-key()"/>
<xsl:text> - </xsl:text>
<xsl:value-of select="count(current-group())"/>
</li>
</xsl:if>
</xsl:for-each-group>
</ul>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/19779386
复制相似问题