472,958 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

recursive xsl

Hi i'm new to xsl and i have been using smarty php templating but its
just so hard to read codes in smarty/php/flash than xml/xsl/flash, i
rather sacrifice speed then not being able to read code after 3 months.
So my problem goes like this.
I have an xml that like this
<avatar>
<avatarId>1</avatarId>
<avatarName>MyNewAvatar</avatarName>
<avatarFile>
<fileName>MyNewAvatar.swf</fileName>
</avatarFile>
<avatarColor>
<color>
<colorId>1</colorId>
<colorName>BLUE</colorName>
</color>
<color>
<colorId>2</colorId>
<colorName>BLACK</colorName>
</color>
<color>
<colorId>3</colorId>
<colorName>RED</colorName>
</color>
<color>
<colorId>4</colorId>
<colorName>GREEN</colorName>
</color>
</avatarColor>
</avatar>

i want something like this
<div>
<div>BLUE BLACK</div>
<div><object ...... src="MyNewAvatar.swf" ... /></div>
<div>RED GREEN</div>
</div>

Is there a way to suspend the recursive loop say after 2 or more
colors? then go back to it the N +1 place? Any help would be big so i
need to thank you in advance...

Dec 20 '06 #1
5 1791

monmonja wrote:
<avatar>
<avatarId>1</avatarId>
<avatarName>MyNewAvatar</avatarName>
<avatarFile>
<fileName>MyNewAvatar.swf</fileName>
</avatarFile>
<avatarColor>
<color>
<colorId>1</colorId>
<colorName>BLUE</colorName>
</color>
<color>
<colorId>2</colorId>
<colorName>BLACK</colorName>
</color>
<color>
<colorId>3</colorId>
<colorName>RED</colorName>
</color>
<color>
<colorId>4</colorId>
<colorName>GREEN</colorName>
</color>
</avatarColor>
</avatar>

i want something like this
<div>
<div>BLUE BLACK</div>
<div><object ...... src="MyNewAvatar.swf" ... /></div>
<div>RED GREEN</div>
</div>

Is there a way to suspend the recursive loop say after 2
or more colors? then go back to it the N +1 place?
I'm not sure what you mean and why do you need a recursive
loop in the first place. Something like the following
transformation should do the trick:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="avatar">
<xsl:variable name="colors-1"
select=
"
avatarColor/color[colorId&gt;=1 and colorId&lt;=2]
"/>
<xsl:variable name="colors-2"
select=
"
avatarColor/color[colorId&gt;=3 and colorId&lt;=4]
"/>
<div>
<div>
<xsl:apply-templates
select="$colors-1" mode="color-names"/>
</div>
<div><object src="{avatarFile/fileName}"/></div>
<div>
<xsl:apply-templates
select="$colors-2" mode="color-names"/>
</div>
</div>
</xsl:template>
<xsl:template match="color" mode="color-names">
<xsl:value-of select="colorName"/>
<xsl:text</xsl:text>
</xsl:template>
</xsl:stylesheet>

In case there's a good reason you need recursive loop, you
can always fiddle around with passing parameters on each
iteration: start-from, stop-at or somesuch.

--
Pavel Lepin

Dec 20 '06 #2
monmonja wrote:
Hi i'm new to xsl and i have been using smarty php templating but its
just so hard to read codes in smarty/php/flash than xml/xsl/flash, i
rather sacrifice speed then not being able to read code after 3 months.
If you are willing to look at languages other
than xsl, you might appreciate this solution in
XMLgawk:

@load xml
BEGIN { print "<div>" }
XMLCHARDATA { data = $0}
XMLENDELEM == "colorName" { color[++ci] = data }
XMLENDELEM == "fileName" { fileName = data }
ci == 2 {
print "<div>" color[1], color[2] "</div>" ;
print "<div><object ...... src=\"" fileName "\" ... /></div>"
ci = 0
}
END { print "</div>" }
Readability of such scripts is in the eye of the beholder.
The output produced by this script looks like this:

<div>
<div>BLUE BLACK</div>
<div><object ...... src="MyNewAvatar.swf" ... /></div>
<div>RED GREEN</div>
<div><object ...... src="MyNewAvatar.swf" ... /></div>
</div>

I know this isnt exactly what you asked for, but it
is easy to change the script if you need different output.
Dec 20 '06 #3
The reason i need recursive because its more complex then that. Thanks
for the reply, can you show me a simple example of the passing
parameters on each
iteration: start-from, stop-at or somesuch.

BTW, php has more than 50 template systems, i cant afford to study one
again unless its a standard which XSL is. The reason ive used Smarty is
because it was here in my work when i came here.

Dec 21 '06 #4
Ive found the answer but i have another problem can someone explain to
this:
<xsl:template match="/">
<xsl:call-template name="tmpColors" >
<xsl:with-param name="counter" select="5" />
</xsl:call-template>
flash OBJECT
<xsl:apply-templates select="avatars/avatar/colors" />
</xsl:template>

<xsl:template match="avatars/avatar/colors" name="tmpColors">
<xsl:param name="counter" />
<xsl:value-of select="count(color)" />
<xsl:for-each select="avatars/avatar/colors/color[position()
&lt; $counter]">
Try
<xsl:value-of select="colornameame" />
<xsl:text</xsl:text>
</xsl:for-each>
</xsl:template>

On call-template count(color) = 0 while on apply-template count(color)
= n. but if i do XPath from the root down
count(avatars/avatar/colors/color) the opposite happens. Do
call-template use the match patterns? Any advice from the experts out
there when its more appropriate to use call-template over
apply-template. Again thanks in advance.

Dec 21 '06 #5

Please quote what you're replying to. (And if you start
quoting--don't top-post.) Read something about proper
etiquette when posting on the usenet.

monmonja wrote:
<xsl:template match="/">
<xsl:call-template name="tmpColors" >
<xsl:with-param name="counter" select="5" />
</xsl:call-template>
flash OBJECT
<xsl:apply-templates select="avatars/avatar/colors" />
</xsl:template>

<xsl:template match="avatars/avatar/colors"
name="tmpColors">
<xsl:param name="counter" />
<xsl:value-of select="count(color)" />
<xsl:for-each
select="avatars/avatar/colors/color[position()
&lt; $counter]">
Try
<xsl:value-of select="colornameame" />
<xsl:text</xsl:text>
</xsl:for-each>
</xsl:template>

On call-template count(color) = 0 while on apply-template
count(color) = n. but if i do XPath from the root down
count(avatars/avatar/colors/color) the opposite happens.
Do call-template use the match patterns?
No, they don't. (I remember vividly putting my foot in my
mouth regarding this a few months ago--and Joe Kesselman
gently biting my head off shortly thereafter. Ah, sweet
memories.) call-template simply invokes another template,
without changing the context node.
Any advice from the experts out there when its more
appropriate to use call-template over apply-template.
Again thanks in advance.
First of all, I'd say using the same template as both
matchable and callable is a bad idea. Naturally, there
might be certain situations where this would be
appropriate, but unless you have a very good reason to do
something like that--don't.

In terms of imperative programming it might help thinking
about named templates as something like functions, and
about templates invoked using apply-templates as
polymorphic methods. A named template doesn't really care
about the context it's invoked from: it just does some
largerly context-independent stuff and that's all. Applying
templates to a nodeset is similar to doing something with a
collection of objects that you know implement a certain
interface without really caring about implementations. You
just say: 'do-something with all these nodes', and the
templates matching the nodes in question will determine
precisely how it will be done.

--
Pavel Lepin

Dec 21 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Carlos Ribeiro | last post by:
Hello all, Here I am using some deeply nested, tree-like data structures. In some situations I need to traverse the tree; the old-style way to do it is to write a recursive method on the node...
10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
1
by: Jon Slaughter | last post by:
I've managed to put together a template class that basicaly creates a recursive tree that lets you easily specify the "base" class of that tree and and ending notes and lets you stop the recursive...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
9
by: seberino | last post by:
I'm a compiler newbie and curious if Python grammar is able to be parsed by a recursive descent parser or if it requires a more powerful algorithm. Chris
0
by: champ1979 | last post by:
I wrote an algorithm to get all the relatives of a person in a family tree. I'm basically getting all the users from the DB and am doing the recursive logic in code, so that there is only 1 call...
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.