Helpful Information
 
 
Category: Programming Languages More
XSLT/XPath Expression

I'm having a bit of trouble coming up with the XPath/XSLT expression to express an algorithm.

<!--This is an example XML Fragment that my algorithm needs to work on-->
<parent-node type="generated">
<child-node id="first-child" number-of-pages="10">
<![CDATA[Text of page would go here]]>
</child-node>
<child-node id="second-child" number-of-pages="5">
<![CDATA[Text of page would go here]]>
</child-node>
<child-node id="third-child" number-of-pages="20">
<![CDATA[Text of page goes here]]>
</child-node>
</parent-node>

<!--This is the output fragment that my algorithm should yield-->
Table of Contents
first child.................................1
second child................................11
third child.................................16

*The "...." are not really part of the output. They are simply for formatting and clarity

The purpose of the algorithm is to find out what page the 'CDATA' section of each 'child-node' would start on,
assuming that each CDATA section would begin on a new page. This can be expressed as the following algorithm (in psuedo code):
- When the current 'child-node' is being processed
- For every 'child-node' before the current 'child-node' get the value of the 'number-of-pages' attribute
- Sum the values then add 1
- The value of the previous step is the page number that the 'CDATA' section of the current 'child-node' will start on ...

... Therefore if there were a forth-child element it would print,
fourth child............................. 36 (because 20 + 5 + 10 + 1 = 36)


The XSLT/XPath construct that I currently have looks like this (pretend the spacing, carriage returns, and tabs are being magically handled):

<xsl:template match="parent-node[@type='generated']">
Table of Contents

<xsl:for-each select="child-node">
<xsl:value-of select="@id"/>..............<xsl:number value="sum(previous-siblings[attribute: http://www.devshed.com/Talk/Forums/tongue.gifageCount)"/><!-- point of interest (POC)-->
</xsl:for-each>
</xsl:template>


I've tried all sorts of different expressions at POC. The one you see now is just the latest. Any help would be greatly appreciated.

Not sure if this hits the spot... but tried it with more child nodes & it works!.
I have included the complete stylesheet for clarity, so pick what you want from it if it helps!

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:template match="parent-node[@type='generated']">
<p>Table of Contents<p/>
<xsl:apply-templates select="child-node" />
</xsl:template>

<xsl:template match="child-node">
<xsl:choose>
<xsl:when test="position() = 1">
<p><xsl:value-of select="@id" /> .............................. 1.</p>
</xsl:when>
<xsl:otherwise>
<p><xsl:value-of select="@id" /> ............................... <xsl:value-of select="sum(preceding-sibling::child-node/@number-of-pages) + 1" />.</p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>