Page 1 of 1

Using Javascript for counter...Stuck...need help!!!

Posted: Wed Oct 03, 2012 2:29 am
by AshKutt
Here's my requirement. To Have a running counter in a for-loop and then when the for-loop completes display the total rows processed. Thought this would be the easiest to do, but I have spent more than 5 hours trying to figure this simple counter value displayed.

So result should say

1|xxx|1234
2|jhgh|4343
3|nhkj|4442
Total Rowcount: 3

I tried using position() function, but since i put it in for loop it display row number after each row. I tried to use it as a template variable calling another template, but that didnt work either. So finally thought of using Javascript and then call the counter function withn the javascript. But the script is erroringout. Here's the code snippet

xmlns:ex="http://exslt.org/dates-and-times" extension-element-prefixes="ex"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:myExt="urn:com.schemas-microsoft-com:xslt">

<xsl:output method="html" version='4.0' encoding='UTF-8' indent='yes'/>
<msxsl:script language="javascript" implements-prefix="myExt">
<![CDATA[

var segmentCount = 0;
function AddSegments(addition)
{
segmentCount = segmentCount + addition;
return segmentCount;
}
function GetSegmentsCount()
{
return segmentCount;
}
]]>
</msxsl:script>

<xsl:template match="/">
<xsl:for-each
<xsl:with-param name="counter" select="position()"/>
<xsl:variable name="headerSegmentCount" select="myExt:AddSegments(@counter)" />
</xsl:for-each
<xsl>Total Row Count:</xsl>|<xsl:variable name="totalSegmentCount" select="myExt:GetSegmentsCount()" />

When I run this I get the following message.

Engine name: Saxon6.5.5
Severity: error
Description: The URI urn:com.schemas-microsoft-com:xslt does not identify an external Java class
Start location: 83:0

Help please!

Re: Using Javascript for counter...Stuck...need help!!!

Posted: Thu Oct 04, 2012 4:13 pm
by adrian
I wouldn't use JavaScript in a stylesheet just for a counter, it's overkill. Try to learn a bit of XSLT and XPath and it will make things much easier.

As to why it's not working, you're trying to use the 'msxsl' XSLT extension with a non-Microsoft XML transformer. Saxon doesn't support 'msxsl', you'll have to use either the MSXML or the .NET transformers.

Search the forums when you encounter problems like these, they are rather common and usually have been discussed and resolved.

Regards,
Adrian