[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: recursive addition
One question -- what does the attribute @files mean on the folder element whose @id="dir1"? It has @files="4", but that doesn't seem to enter into the calculation you're doing. So the semantics of the XML are a little unclear to me. > -----Original Message----- > From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx > [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx]On Behalf Of > annirack@xxxxxxx > Sent: Wednesday, January 14, 2004 2:41 PM > To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx > Subject: recursive addition > > > I have a structure (much like a file system) where elements > of the same type are linked together via IDs rather than the > XML structure--i.e. all directory elements are at the same level. > > For an arbitrary directory level element, I want to sum a > numeric attribute for all linked elements and so on down the tree. To rephrase the above question, are you just summing the attributes of leaf folders (as you appear to be in your example where you expect the answer to be 9)? > Is there any way to do this? I know that recursion is the > first step, but what I don't know how to sum an arbitrary > number of parallel level attributes in different elements. > > Here's an example: > <root> > <folder files="2" id="dir0"> > <file id="dir1" /> > > <file id="dir4" /> > </folder> > > <folder files="4" id="dir1"> > <file id="dir2" /> > > <file id="dir3" /> > </folder> > > <folder files="3" id="dir2" /> > > <folder files="6" id="dir3" /> > > <folder files="7" id="dir4" /> > > <folder files="5" id="dir5" /> > </root> > > If my arbitrary element id is "dir1", then the result should > be 9. (folder[@id='dir2']/@files + folder[@id='dir3']/@files) > > How do I do that with xsl? Assuming you're only summing the @files attributes of folders that have no subfolders, here's some pseudo code: with parameter folderID main template variable folder = /root/folder[@id = $folderID] call template sumFiles(folder, 1) template name=sumFiles params folder, startFrom if not(child::file) return @files else if not(child::file[startFrom]) return "0" else { variable firstChild = child::file[startFrom] firstSum = sumFiles(/root/folder[@id = $firstChild/@id], 1) restSum = sumFiles(., startFrom+1) return xsl:value-of select="$firstSum + $restSum"; } Does that help? Of course you could make it more efficient by using keys/id's. You could probably also make the code simpler by using an extension function to build a nodeset and then use sum() on it. Lars XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|