使用xsl和javascript显示xml文件中计算的关键字总和

时间:2014-11-03 19:29:30

标签: javascript xml xslt

我有一个html文件,它使用javascript读取xml文件并对其应用xsl文件,以输出某个关键字在xml文件中出现的次数,并在html页面上显示该计数。我想从2个单独的xml文件中获取这些关键字的计数。我该怎么做呢?

这是我的javascript:

function transform1() 
{
    var xdoc1 = emilygfinalreadxml("xml/cleveland-browns-news.xml") ;
    var xslt1 = emilygfinalreadxml("xsl/browns-keywords.xsl");
    var result = emilygfinalrunTransform(xdoc1,xslt1);
    var resultDiv = document.getElementById("browns-keywords");
    resultDiv.innerHTML = result;
}

function clearDivs1()
{
    var resultDiv = document.getElementById("browns-keywords");
    resultDiv.innerHTML = "";
}

这是我的xsl来计算关键字的数量:

<div>
        Number of times "quarterback" is used: <xsl:value-of select="count(//*[contains(text(), 'quarterback')])"/>
</div>
<div>
        Number of times "Cleveland" is used: <xsl:value-of select="count(//*[contains(text(), 'Cleveland')])"/>
</div>
</xsl:template>

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

试试这个:

<div>
   <xsl:text>Number of times "quarterback" is used: </xsl:text><xsl:value-of select="count(//text()[contains(., 'quarterback')])"/>
</div>
<div>
    <xsl:text>Number of times "Cleveland" is used: </xsl:text><xsl:value-of select="count(//text()[contains(., 'Cleveland')])"/>
</div>

这里我使用了XSLT和XML方法,尝试通过javascript调用输入。

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:variable name="varDoc1"  select="document('Text1.xml')"/>
<xsl:variable name="varDoc2"  select="document('Text2.xml')"/>

<xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>

<xsl:template match="/">
    <div>
       <xsl:text>Number of times "quarterback" is used: </xsl:text>
        <xsl:value-of select="count($varDoc1//text()[contains(., 'quarterback')]) + count($varDoc2//text()[contains(., 'quarterback')])"/>
    </div>

    <div>
        <xsl:text>Number of times "Cleveland" is used: </xsl:text>
        <xsl:value-of select="count($varDoc1//text()[contains(., 'Cleveland')]) + count($varDoc2//text()[contains(., 'Cleveland')])"/>
    </div>
</xsl:template>

</xsl:stylesheet>

Text1.xml(输入1):

<article>Cleveland
 <para-first>The Cleveland-system and quarterback</para-first>
 <para>The <i>Cleveland-system</i></para>
 <para>The <i><b>Cleveland-system</b></i> and quarterback.</para>
</article>

Text1.xml(输入2):

<article>quarterback
<para-first>The Cleveland-system and quarterback</para-first>
<para>The <i>Cleveland-system</i></para>
<para>The <i><b>Cleveland-system</b></i> and quarterback.</para>
</article>