XSLT / XPath来计算最常用的单词

时间:2013-11-04 23:37:29

标签: xml xslt xpath

假设我有一个这样的XML文档:

<library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="library.xsd">
<book>
    <title>Harry Potter Sorcerer's Stone</title>
    <artist>J.K. Rowling</artist>
</book>
<book>
    <title>Harry Potter Chamber of Secrets</title>
    <artist>J.K. Rowling</artist>
</book>
<book>
    <title>Harry Potter Prisoner of Azkaban</title>
    <artist>J.K. Rowling</artist>
</book>     
</library>

我希望我的XSLT文档能够找到标题中最常用的3个单词及其计数。 (所以我想输出:&#34; Harry&#34;:3,&#34; Potter&#34;:3,&#34;&#34;: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()的一些组合?有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:2)

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