Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 2nd, 2008, 09:07 AM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
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


Reply With Quote
  #2 (permalink)  
Old January 2nd, 2008, 09:20 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
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 -/
Reply With Quote
  #3 (permalink)  
Old January 2nd, 2008, 09:40 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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
Reply With Quote
  #4 (permalink)  
Old January 2nd, 2008, 10:00 AM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
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

Reply With Quote
  #5 (permalink)  
Old January 2nd, 2008, 10:10 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
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 -/
Reply With Quote
  #6 (permalink)  
Old January 2nd, 2008, 10:26 AM
Friend of Wrox
Points: 1,676, Level: 16
Points: 1,676, Level: 16 Points: 1,676, Level: 16 Points: 1,676, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2006
Location: , , .
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:45 PM.


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