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 October 10th, 2007, 05:23 AM
Registered User
 
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Understanding wordcount.xsl

Hi there,

I'm new to the forum, so please be gentle...

Maybe someone could help me understanding the wordcount.xsl example given in the XSLT 2.0 Programmers Reference. I'm not sure where to post it or nor, but since it is a short stylesheet, here we go:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet
   version="2.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
  <wordcount>
    <xsl:for-each-group group-by="." select="
          for $w in tokenize(string(.), '\W+') return lower-case($w)">
      <xsl:sort select="count(current-group())" order="descending"/>
      <word word="{current-grouping-key()}" frequency="{count(current-group())}"/>
    </xsl:for-each-group>
  </wordcount>
</xsl:template>
</xsl:stylesheet>

I tested the stylesheet with a hamlet.xml encoded by Jon Bosak (just google for it, I will definitely not post this one here...) on a Linux machine (Kubuntu 7.04, Java 1.6.0, Saxon-B 8.9.0.4J) and it worked perfect.

But then I realized I shouldn't. 'string(.)' returns the concatenation of all descending text nodes, in this case the whole text of the input document, ignoring the markup.
So something like

<SPEECH>
 <SPEAKER>HAMLET</SPEAKER>
 <LINE>Not so, my lord; I am too much i' the sun.</LINE>
</SPEECH>

would become

HAMLETNot so, my lord; I am too much i' the sun.

tokenizing (and lower-casing) this line would result in a 'hamletnot', amongst others, and not a 'hamlet' as the out put given by 'java -jar saxon8.jar hamlet.xml wordcount.xsl' would suggest. Saxon counts 475 occurrences of 'hamlet', which is perfectly correct, but theoretically it should only report 107, along with the occurrence with some esoteric words like 'hamletnot', 'hamletthy' and so on. Am I missing something?

In my case, using this stylesheet with the eXist open source XML database, I resolved the matter using a temporary tree in which the markup was removed in a more hamlet-friendly way, but I was still wondering about Saxon's behaviour in this case. Can anyone point me in the right direction?

Thanks,
Sven

 
Old October 10th, 2007, 05:45 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The above xml snippet contains more text nodes than you are giving it credit for.
I've hightlighted them below by putting each text node in brackets.

Code:
<SPEECH>(
 )<SPEAKER>(HAMLET)</SPEAKER>(
 )<LINE>(Not so, my lord; I am too much i' the sun.)</LINE>(
)</SPEECH>
/- Sam Judson : Wrox Technical Editor -/
 
Old October 10th, 2007, 05:56 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

(It should be noted that some XSLT 2.0 processors strip out whitespace before it gets to the XSLT processor, e.g. Altova, so the example above does work exactly as you suspected).

/- Sam Judson : Wrox Technical Editor -/
 
Old October 10th, 2007, 07:48 AM
Registered User
 
Join Date: Oct 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I understand (at last)... thank you for your quick help!




 
Old October 10th, 2007, 08:05 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

This is a good catch. The code as written is dependent on whitespace text nodes being preserved on their way in to the XSLT processor, and (especially as the example appears so early in the book) this should at least have been made clear. In an ideal world it would be possible to write XSLT code knowing that the whitespace will be there (unless you use xsl:strip-space to remove it). However, the world is not ideal, and neither you as the stylesheet author nor W3C as the author of the XSLT specification can legislate what happens to the document on its journey from a file on disk to a tree in memory before the XSLT processor gets to see it. Some XML parsers, notably those used by Microsoft and Altova, remove the whitespace by default, and in some environments (notably Altova) you don't even get any option to stop this happening. With Saxon there are command line options to request stripping of the whitespace, or conditional stripping based on the content model in any DTD or schema.

So, in the ideal situation where whitespace is preserved the code will work correctly, but in some environments it won't. To work correctly in all environments, the simplest fix is to replace the use of string(.) by normalize-space(string-join(descendant::text(), ' ')), which adds spaces between any two text nodes. Unfortunately this is a fairly expensive operation.

You've left me in a bit of a quandary about what to do with this example when I revise the chapter. The above is far too complicated to introduce at the time I present the example, but perhaps I can fix it by noting the problem briefly and coming back to it in Chapter 3 where whitespace issues are analyzed in sordid detail.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help understanding DataRow asp_convert ADO.NET 3 March 13th, 2007 02:39 PM
Need help understanding databinding asp_convert ASP.NET 2.0 Basics 0 February 11th, 2007 07:17 PM
Understanding Axes Chamkaur XSLT 1 August 6th, 2006 03:09 PM
understanding Variable use imroostercogburn C++ Programming 7 July 28th, 2006 06:15 PM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





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