Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 2nd, 2008, 09:07 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default Word search - Count Hits

Hello All,

I've been working on a XSLT driven search engine for my website. I am getting stuck on a hit count. My search engine uses VB.NET and XSLT to display the results so I am forced to use XSLT 1.0.

I've cut out most of the XSLT to keep it simple:
Code:
        <xsl:for-each select="document($sFile)">
            <xsl:choose>
                <xsl:when test="contains(//*,$search)">
                    <xsl:for-each select="//*">
                        <xsl:number/>
                    </xsl:for-each>
........
How do I count the number of times the word is found in the document?

Thanks


 
Old January 2nd, 2008, 09:20 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

A quick aside - Saxon and AltovaXML can both be used with .Net, so you are not restricted to XSLT 1.0 along just because you are using VB.Net.

The contains function takes two strings as arguments, therefore the above will create the the string representation of the node set containing every node in the document - which seeing as in XSLT 1.0 this will simply be the text value of the first node, which will probably be the root node, and hence the text value of the entire tree the following will probably do the same test="contains(., $search)"

Also a 'feature' is that the contains function would return true if the XML input contained "<root><element>1</element><element>2</element></root>" and you searched for "12".

Lastly your inner for-each will just loop through every node in the tree and output the <xsl:number> for each.

Perhaps <xsl:for-each select="//*[contains(text(), $search)"> might be a better way of doing this.

/- Sam Judson : Wrox Technical Editor -/
 
Old January 2nd, 2008, 09:40 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Counting the number of occurrences requires a recursive template with the logic (in pesudo-code)

countOccurrences(x, str) {
  if (contains(x, str))
  then 1 + countOccurrences(substring-after(x, str), str)
  else 0
}

But you might find that you can get by with counting the number of text nodes that contain the string, which would be

count(//text()[contains(., $str)])

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 2nd, 2008, 10:00 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Thank you very much.

I should have been more clear. I am using VB.NET and XSLT for a web application and doing the transformation with Internet Explorer.

Michael, the following worked:

count(//text()[contains(., $str)])

Thank you,

Bones

 
Old January 2nd, 2008, 10:10 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

So the reason you can't use XSLT 2.0 is because you are using IE, not because you are using .Net.

/- Sam Judson : Wrox Technical Editor -/
 
Old January 2nd, 2008, 10:26 AM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Hi Sam,

Yes, I am doing the transformation with IE.

I do use Saxon 9.0 with my .NET apps. I only wish IE had the same functionality as SAXON.





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to count lines in word file arshad mahmood VB How-To 2 June 7th, 2006 09:40 PM
character count of ms-word in asp.net sh.rajkumar ASP.NET 1.0 and 1.1 Professional 0 May 19th, 2006 07:24 AM
Coding for Word Count zadiac8318 JSP Basics 1 February 26th, 2005 07:34 PM
how to count the database search results of record gilgalbiblewheel Classic ASP Databases 2 July 31st, 2004 10:37 AM
Word Count 12th_Man SQL Language 7 July 25th, 2003 09:07 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.