Jump to content

Unkeyed Lookup & Sum In Xsl 1.0


Recommended Posts

I've got a list of values that will be inside an xsl stylesheet document.

<lu:temperatures>   <lu:reading>87</lu:reading>   <lu:reading>44</lu:reading>   <lu:reading>71</lu:reading>   <lu:reading>99</lu:reading>   <lu:reading>100</lu:reading>   <lu:reading>99</lu:reading>   <lu:reading>57</lu:reading>   <lu:reading>63</lu:reading>   <lu:reading>104</lu:reading>   <lu:reading>84</lu:reading>   <lu:reading>84</lu:reading>   <lu:reading>91</lu:reading>   <lu:reading>91</lu:reading>   <lu:reading>91</lu:reading>   <lu:reading>91</lu:reading>   <lu:reading>88</lu:reading>   <lu:reading>74</lu:reading>   <lu:reading>69</lu:reading>   <lu:reading>88</lu:reading>   <lu:reading>92</lu:reading></lu:temperatures>

Those values are at the root level in the xsl stylesheet, immediately before the template that would do a lookup on them. Following those values, I have a template that gets called from elsewhere in the xsl, named sum-temps. That template gets passed 1 parameter, the number of samples it is due to total (this number can vary). Within that template, I want to sum the values in the lookup table whose position is <= the number passed to the template.

<xsl:variable name="tempsum" select="sum(document('')/lu:temperatures/lu:reading[position() <= $tempsample])" />

However, I cannot make it sum the readings, no matter how I've changed the above statement. My stylesheet tag has the lu namespace in it & I exclude result prefixes.

<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:lu="http://example.com/lookup"exclude-result-prefixes="lu">

The expected behavior is if tempsum = 5, it returns the sum of the first 5 readings. However, the tempsum variable always returns zero. I've confirmed though, that the number (tempsample) being passed into the sum statement is what it's supposed to be. What am I doing wrong? SC

Link to post
Share on other sites
sum(*/lu:temperatures/lu:readings[position() <= $tempsample])

This is just a fast reply so don't know if your methods are accurate or if mine are in your case.But in my eyes you should write something like above. The star means anywhere, the point is to get results you can change it to a static path later.

Link to post
Share on other sites
I've tried that - it is still returning zero for the value.
Please post minimal but complete samples allowing us to reproduce the problem.I tried
<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:lu="http://example.com/lookup"exclude-result-prefixes="lu"> <xsl:param name="tempsample" select="5"/> <lu:temperatures>   <lu:reading>87</lu:reading>   <lu:reading>44</lu:reading>   <lu:reading>71</lu:reading>   <lu:reading>99</lu:reading>   <lu:reading>100</lu:reading>   <lu:reading>99</lu:reading>   <lu:reading>57</lu:reading>   <lu:reading>63</lu:reading>   <lu:reading>104</lu:reading>   <lu:reading>84</lu:reading>   <lu:reading>84</lu:reading>   <lu:reading>91</lu:reading>   <lu:reading>91</lu:reading>   <lu:reading>91</lu:reading>   <lu:reading>91</lu:reading>   <lu:reading>88</lu:reading>   <lu:reading>74</lu:reading>   <lu:reading>69</lu:reading>   <lu:reading>88</lu:reading>   <lu:reading>92</lu:reading></lu:temperatures> <xsl:template match="/">  <xsl:variable name="tempsum" select="sum(document('')/xsl:stylesheet/lu:temperatures/lu:reading[position() <= $tempsample])" />  <xsl:value-of select="$tempsum"/></xsl:template> </xsl:stylesheet>

and when I run that with Saxon 6.5.5 against any XML input the output is

<?xml version="1.0" encoding="utf-8"?>401

which seems to be different from zero. So I don't know what you are doing differently, the samples you have shown so far together with the xpath I posted should do.

Edited by Martin Honnen
Link to post
Share on other sites
and when I run that with Saxon 6.5.5 against any XML input the output is
<?xml version="1.0" encoding="utf-8"?>401

which seems to be different from zero. So I don't know what you are doing differently, the samples you have shown so far together with the xpath I posted should do.

Using your example, I get the same result as you. My xsl code has more going on inside it, but the basic differences between the two is that you are doing a template match, and I am calling this named template from elsewhere in the code (from inside another named template). I even went so far as to treat $tempsample as a number:
<xsl:variable name="tempsample" select="number($sample)"/>

before using it in the assignment of a value to that variable.

<xsl:variable name="tempsum" select="sum(document('')/xsl:stylesheet/lu:temperatures/lu:reading[position() < $tempsample])"/>

My xsl is:

<xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:lu="http://example.com/lookup"exclude-result-prefixes="lu"><xsl:output method="xml" /><xsl:template match="@*|*">    <xsl:apply-templates select= "*"/></xsl:template> <xsl:template match="abcde">

then a bunch of xsl doing all sorts of things, including setting the number for our sample size. then:

      <xsl:variable name="tempsample" select="number($sample)"/>	  <xsl:call-template name="tempreadings">		    <xsl:with-param name="tempsample" select="$tempsample"/>	  </xsl:call-template></xsl:template><xsl:template name="tempreadings">	  <xsl:param name="tempsample"/>

then do some more things inside tempreadings........

	   <xsl:call-template name="tempaccumulator">			 <xsl:with-param name="tempsample" select="$tempsample"/>	    <xsl:call-template></xsl:template>

then I have the lookup table:

<xsl:param name="tempsample" select="5"/><lu:temperatures>   <lu:reading>87</lu:reading>   <lu:reading>44</lu:reading>   <lu:reading>71</lu:reading>   <lu:reading>99</lu:reading>   <lu:reading>100</lu:reading>   <lu:reading>99</lu:reading>   <lu:reading>57</lu:reading>   <lu:reading>63</lu:reading>   <lu:reading>104</lu:reading>   <lu:reading>84</lu:reading>   <lu:reading>84</lu:reading>   <lu:reading>91</lu:reading>   <lu:reading>91</lu:reading>   <lu:reading>91</lu:reading>   <lu:reading>91</lu:reading>   <lu:reading>88</lu:reading>   <lu:reading>74</lu:reading>   <lu:reading>69</lu:reading>   <lu:reading>88</lu:reading>   <lu:reading>92</lu:reading></lu:temperatures>

followed immediately by the named template that does the lookup.

<xsl:template name="tempaccumulator">	  <xsl:param name="tempsample"/>	  <xsl:variable name="tempsample1" select="number($tempsample)"/>       <xsl:variable name="tempsum" select="sum(document('')/xsl:stylesheet/lu:temperatures/lu:reading[position() < $tempsample1])"/>       <xsl:value-of select="$tempsum"/></xsl:template> </xsl:stylesheet>

In my code, it's like the lookup isn't even being done. But when I run this, it returns zero for that $tempsum variable. I'm somewhat stumped as to why it wouldn't even appear to do the lookup. Is there a way to debug that?

Link to post
Share on other sites

I can't find the problem by reading the various code snippets you posted, I don't think it should make any difference whether the lookup with document('') is used in a named template or a matching template. If possible try to post a minimal but complete stylesheet that we can run to see its output to reproduce the issue.As for debugging, there are various XML editors like XML Spy or Stylus Studio or Oxygen that have debuggers.And of course you can always add some debugging output yourself with inserting stuff like e.g.

<debug>  <xsl:copy-of select="document('')/xsl:stylesheet/lu:temperatures/lu:reading"/></debug>

in your stylesheet to find out whether your path finds the right elements.

Link to post
Share on other sites
I can't find the problem by reading the various code snippets you posted, I don't think it should make any difference whether the lookup with document('') is used in a named template or a matching template.
Outside of some housekeeping & calling other named templates, the code was pretty much the same as I was running. I think I uncovered the issue though. I am using jSimpleX, a visual transformation tool, which tells me a few things about the environment:
java.runtime.name=Java(TM) SE Runtime Environmentsun.boot.library.path=C:\Program Files\Java\jre1.6.0_07\binjava.vm.version=10.0-b23java.vm.vendor=Sun Microsystems Inc.java.vendor.url=http://java.sun.com/path.separator=;java.vm.name=Java HotSpot(TM) Client VMfile.encoding.pkg=sun.iosun.java.launcher=SUN_STANDARDuser.country=USsun.os.patch.level=Service Pack 3java.vm.specification.name=Java Virtual Machine Specificationuser.dir=C:\Documents and Settings\UserX\My Documents\Downloadsjava.runtime.version=1.6.0_07-b06java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironmentjava.endorsed.dirs=C:\Program Files\Java\jre1.6.0_07\lib\endorsedos.arch=x86java.io.tmpdir=C:\DOCUME~1\UserX\LOCALS~1\Temp\line.separator=java.vm.specification.vendor=Sun Microsystems Inc.user.variant=os.name=Windows XPsun.jnu.encoding=Cp1252java.library.path=C:\WINDOWS\system32;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;c:\Program Files\Common Files\Lenovo;C:\Program Files\Common Files\Roxio Shared\10.0\DLLShared\;C:\Program Files\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Common Files\Roxio Shared\10.0\DLLShared\;C:\WINDOWS\system32\WindowsPowerShell\v1.0;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\TortoiseSVN\bin;c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\;C:\Program Files\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\QuickTime\QTSystem\java.specification.name=Java Platform API Specificationjava.class.version=50.0sun.management.compiler=HotSpot Client Compileros.version=5.1user.home=C:\Documents and Settings\UserXuser.timezone=java.awt.printerjob=sun.awt.windows.WPrinterJobfile.encoding=zzzzzzjava.specification.version=1.6java.class.path=jsimplex_3.1.jaruser.name=UserXjava.vm.specification.version=1.0java.home=C:\Program Files\Java\jre1.6.0_07sun.arch.data.model=32user.language=enjava.specification.vendor=Sun Microsystems Inc.awt.toolkit=sun.awt.windows.WToolkitjava.vm.info=mixed mode, sharingjava.version=1.6.0_07java.ext.dirs=C:\Program Files\Java\jre1.6.0_07\lib\ext;C:\WINDOWS\Sun\Java\lib\extsun.boot.class.path=C:\Program Files\Java\jre1.6.0_07\lib\resources.jar;C:\Program Files\Java\jre1.6.0_07\lib\rt.jar;C:\Program Files\Java\jre1.6.0_07\lib\sunrsasign.jar;C:\Program Files\Java\jre1.6.0_07\lib\jsse.jar;C:\Program Files\Java\jre1.6.0_07\lib\jce.jar;C:\Program Files\Java\jre1.6.0_07\lib\charsets.jar;C:\Program Files\Java\jre1.6.0_07\classesjava.vendor=Sun Microsystems Inc.

If I paste your test code into it, it will not work.If I paste your test code into Cooktop 2.5, it will work (this is what I did yesterday). My code won't run in Cooktop, which is why I was using jSimpleX, where it will run, but will not perform the lookup. So- it's really a problem with the tool I'm using to develop & test it. I have not stumbled upon a good, free, supported & recent tool for doing xsl development & testing. jSimpleX is 2003, as is Cooktop (I think it's beta from January 2003).

Link to post
Share on other sites

In the Java world you could move to Saxon 9 and XSLT 2.0, for ease of use there is http://kernowforsaxon.sourceforge.net/. jEdit is a general purpose programming editor that has an XML and XSLT plugin that also makes it easier to use Saxon than running it directly from the command line. For debugging I think you would need to invest on some of the earlier mentioned commercial XML editors.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...