简体   繁体   中英

Displaying the sum of counted keywords from xml files using xsl and javascript

I have an html file that uses javascript to read an xml file and apply an xsl file to it to output a count of the number of times a certain keyword appears in the xml file and display that count in a on the html page. I would like to get a count of these keywords from 2 separate xml files. How would I go about doing that?

Here is my 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 = "";
}

and here is my xsl to count the number of keywords:

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

Thank you for your help!

Try this:

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

Here I used XSLT and XML method, try for calling inputs thru 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 (input 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 (Input 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM