By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,714 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,714 IT Pros & Developers. It's quick & easy.

How often is my recursive template called ??

P: n/a
Dear All,

somehow I remember that such or similar question was discussed already
somewhere. But I can't find it anymore.

I have a template calling itself. As long it goes deeper into the
hierarchy (by the key) I can set the CurrentY parameter by itself +
some constant correctly. Hence which each call the CurrentY gets
bigger.
But when the template reaches a leave and the caller is poped from
stack the old parameter is valid. Hence the absolute sum is lost.

Finally, as a minimun information, I would like to know how often the
recursive template has been called at all (at any time during
recursive execution).

Please find below a xslt which outputs svg rectangles with some text
(the OrgaID) Actually it should draw many rectangles, but as the
currentY is not computed correctly the rectangles overlap and we see
only 5 (which is determined by the maximum hierarchy depth) .

Any hint and help is highly welcome

Rolf

################## xslt ####################################
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:a3="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"

<xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>
<xsl:key name="kOrga" match="/Orga/OrgaBox" use="@BelongsTo"/>
<xsl:template match="/">
<svg contentScriptType="text/ecmascript" width="500" height="500"
viewBox="0 0 10000 30000" a3:scriptImplementation="Adobe"
zoomAndPan="magnify" contentStyleType="text/css" id="PinFunction"
preserveAspectRatio="xMidYMid meet">
<g id="all" transform="translate(2000,2000)">
<xsl:apply-templates select="/Orga/OrgaBox[@OrgaID=169]"/><!--
this is the top one -->
</g>
</svg>
</xsl:template>

<xsl:template name="BuildBoxes" match="*">
<xsl:param name="CurrentX" select="0"/>
<xsl:param name="CurrentY" select="0"/>
<g>
<xsl:attribute name="transform"><xsl:value-of
select="concat('translate(',$CurrentX,',',$Current Y,')')"/></xsl:attribute>
<rect style="fill:yellow;stroke:red;stroke-width:10" width="5000"
height="2000"/>
<text x="200" y="1000" pointer-events="none"
style="fill:black;font-size:800">
<xsl:value-of select="@OrgaID"/>
</text>
</g>
<xsl:apply-templates select="key('kOrga',@OrgaID)">
<xsl:with-param name="CurrentY" select="$CurrentY+3000"/>
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>

######## xml test data ###########################################
<?xml version="1.0" encoding="UTF-8"?>
<Orga>
<OrgaBox OrgaID="131" BelongsTo="169" />
<OrgaBox OrgaID="132" BelongsTo="135" />
<OrgaBox OrgaID="133" BelongsTo="169" />
<OrgaBox OrgaID="135" BelongsTo="169" />
<OrgaBox OrgaID="136" BelongsTo="169" />
<OrgaBox OrgaID="137" BelongsTo="171" />
<OrgaBox OrgaID="138" BelongsTo="171" />
<OrgaBox OrgaID="139" BelongsTo="171" />
<OrgaBox OrgaID="140" BelongsTo="171" />
<OrgaBox OrgaID="141" BelongsTo="136" />
<OrgaBox OrgaID="142" BelongsTo="132" />
<OrgaBox OrgaID="143" BelongsTo="135" />
<OrgaBox OrgaID="144" BelongsTo="135" />
<OrgaBox OrgaID="145" BelongsTo="143" />
<OrgaBox OrgaID="146" BelongsTo="132" />
<OrgaBox OrgaID="147" BelongsTo="133" />
<OrgaBox OrgaID="148" BelongsTo="162" />
<OrgaBox OrgaID="149" BelongsTo="139" />
<OrgaBox OrgaID="150" BelongsTo="139" />
<OrgaBox OrgaID="151" BelongsTo="139" />
<OrgaBox OrgaID="155" BelongsTo="182" />
<OrgaBox OrgaID="156" BelongsTo="136" />
<OrgaBox OrgaID="157" BelongsTo="139" />
<OrgaBox OrgaID="158" BelongsTo="136" />
<OrgaBox OrgaID="159" BelongsTo="133" />
<OrgaBox OrgaID="160" BelongsTo="137" />
<OrgaBox OrgaID="161" BelongsTo="138" />
<OrgaBox OrgaID="162" BelongsTo="138" />
<OrgaBox OrgaID="163" BelongsTo="138" />
<OrgaBox OrgaID="164" BelongsTo="139" />
<OrgaBox OrgaID="165" BelongsTo="139" />
<OrgaBox OrgaID="166" BelongsTo="140" />
<OrgaBox OrgaID="167" BelongsTo="140" />
<OrgaBox OrgaID="168" BelongsTo="140" />
<OrgaBox OrgaID="169" BelongsTo="0" />
<OrgaBox OrgaID="170" BelongsTo="169" />
<OrgaBox OrgaID="171" BelongsTo="169" />
<OrgaBox OrgaID="172" BelongsTo="169" />
<OrgaBox OrgaID="174" BelongsTo="182" />
<OrgaBox OrgaID="175" BelongsTo="138" />
<OrgaBox OrgaID="182" BelongsTo="136" />
<OrgaBox OrgaID="192" BelongsTo="132" />
</Orga>

################ END #####################################
Jul 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
> I have a template calling itself. As long it goes deeper into the
hierarchy (by the key) I can set the CurrentY parameter by itself +
some constant correctly. Hence which each call the CurrentY gets
bigger.
But when the template reaches a leave and the caller is poped from
stack the old parameter is valid. Hence the absolute sum is lost.

Finally, as a minimun information, I would like to know how often the
recursive template has been called at all (at any time during
recursive execution).

Please find below a xslt which outputs svg rectangles with some text
(the OrgaID) Actually it should draw many rectangles, but as the
currentY is not computed correctly the rectangles overlap and we see
only 5 (which is determined by the maximum hierarchy depth) .

Any hint and help is highly welcome


Hi,

As far as I know, there's no way to find out how many times a template is called in XSLT. To solve your specific problem, I think you have to do 2 transformations (2 consecutive transformations or the use of a document fragment node).

Using the following template, a node tree is generated that will be easier to transform to the desired SVG:

<xsl:template name="BuildBoxes" match="*">
<box caption="{@OrgaID}">
<xsl:apply-templates select="key('kOrga',@OrgaID)"/>
</box>
</xsl:template>
## output snippet ###

<box caption="131"/>
<box caption="133">
<box caption="147"/>
<box caption="159"/>
</box>
<box caption="135">
<box caption="132">
<box caption="142"/>
<box caption="146"/>
<box caption="192"/>
</box>
<box caption="143">
<box caption="145"/>
</box>
<box caption="144"/>
</box>
regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #2

P: n/a
>
<box caption="131"/>
<box caption="133">
<box caption="147"/>
<box caption="159"/>
</box>
<box caption="135">
<box caption="132">
<box caption="142"/>
<box caption="146"/>
<box caption="192"/>
</box>
<box caption="143">
<box caption="145"/>
</box>
<box caption="144"/>
</box>
Applying the following stylesheet on the output above, would display structured SVG.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:a3="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"

<xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>
<xsl:key name="kOrga" match="/Orga/OrgaBox" use="@BelongsTo"/>
<xsl:template match="/">
<svg contentScriptType="text/ecmascript" width="500" height="500"
viewBox="0 0 10000 30000" a3:scriptImplementation="Adobe"
zoomAndPan="magnify" contentStyleType="text/css" id="PinFunction"
preserveAspectRatio="xMidYMid meet">
<defs>
<style type="text/css">
rect {fill:yellow;stroke:red;stroke-width:10}
text {fill:black;font-size:800}
</style>
</defs>
<g id="all" transform="translate(2000,2000)">
<xsl:apply-templates select="box"/>
</g>
</svg>
</xsl:template>

<xsl:template match="box">
<g>
<xsl:attribute name="transform"><xsl:value-of
select="concat('translate(',count(ancestor::*) * 8000 ,',',count(preceding::*)*3000,')')"/></xsl:attribute>
<rect width="5000"
height="2000"/>
<text x="200" y="1000" pointer-events="none">
<xsl:value-of select="@caption"/>
</text>
</g>
<xsl:apply-templates select="box"/>
</xsl:template>
</xsl:stylesheet>

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #3

P: n/a
Dear Joris,

thanks a lot for your input.
I think you idea is the only practical solution. But as I undestood it
right I have to store the 'converted' result set first to a file. I
would like to aviod that and tried to create a variable which should
fianlly hold the pre-processed data as an xml tree. Aftrer that I
would like to process this variable.

I tried the snipet below with XMLSpy (ALTOVA). In debug mode the
result of the variable is shown as Caption="169" Box="" Box="" Box=""
Box="" Box="" Box="" Box="" Which indicates that the tree is not build
correctly.
What I'm doing wrong ?
I expected a hirarchial tree of Box Elements with one Attribute
Caption for each element.

May be my idea to create a tree in a variable and then process it is
bad at all.
But if there is a solution, it has probably a lot of applications.

Looking forward to your comments/inputs

Rolf
################### snipet ####################################
<xsl:template match="/">
<xsl:variable name="vProtoBoxes">
<xsl:apply-templates mode="PROTO"
select="/Orga/OrgaBox[@OrgaID=169]"/>
</xsl:variable>
<svg contentScriptType="text/ecmascript" width="500" height="500"
viewBox="0 0 10000 30000" a3:scriptImplementation="Adobe"
zoomAndPan="magnify" contentStyleType="text/css" id="PinFunction"
preserveAspectRatio="xMidYMid meet">
<g id="all" transform="translate(2000,2000)">
<!-- ... processing the varaiable follows here -->
</g>
</svg>
</xsl:template>

<xsl:template mode="PROTO" name="BuildProtoBoxes" match="*">
<xsl:element name="Box">
<xsl:attribute name="Caption">
<xsl:value-of select="@OrgaID"/>
</xsl:attribute>
<xsl:apply-templates mode="PROTO" select="key('kOrga',@OrgaID)"/>
</xsl:element>
</xsl:template>
##################### end of snipet #################################
"Joris Gillis" <ro**@pandora.be> wrote in message news:<op**************@news.pandora.be>...

<box caption="131"/>
<box caption="133">
<box caption="147"/>
<box caption="159"/>
</box>
<box caption="135">
<box caption="132">
<box caption="142"/>
<box caption="146"/>
<box caption="192"/>
</box>
<box caption="143">
<box caption="145"/>
</box>
<box caption="144"/>
</box>


Applying the following stylesheet on the output above, would display structured SVG.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:a3="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"

<xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>
<xsl:key name="kOrga" match="/Orga/OrgaBox" use="@BelongsTo"/>
<xsl:template match="/">
<svg contentScriptType="text/ecmascript" width="500" height="500"
viewBox="0 0 10000 30000" a3:scriptImplementation="Adobe"
zoomAndPan="magnify" contentStyleType="text/css" id="PinFunction"
preserveAspectRatio="xMidYMid meet">
<defs>
<style type="text/css">
rect {fill:yellow;stroke:red;stroke-width:10}
text {fill:black;font-size:800}
</style>
</defs>
<g id="all" transform="translate(2000,2000)">
<xsl:apply-templates select="box"/>
</g>
</svg>
</xsl:template>

<xsl:template match="box">
<g>
<xsl:attribute name="transform"><xsl:value-of
select="concat('translate(',count(ancestor::*) * 8000 ,',',count(preceding::*)*3000,')')"/></xsl:attribute>
<rect width="5000"
height="2000"/>
<text x="200" y="1000" pointer-events="none">
<xsl:value-of select="@caption"/>
</text>
</g>
<xsl:apply-templates select="box"/>
</xsl:template>
</xsl:stylesheet>

Jul 20 '05 #4

P: n/a
Hi again,
I tried the snipet below with XMLSpy (ALTOVA). In debug mode the
result of the variable is shown as Caption="169" Box="" Box="" Box=""
Box="" Box="" Box="" Box="" Which indicates that the tree is not build
correctly.
What I'm doing wrong ?
Your code generates a variable with exactly the tree you want. (using altovaxslt.exe). The only explaination I can see, is that the debug mode of XMLSpy doesn't show the exact XML-tree but rather a more textual representation. The 'Box' elements contain no text-nodes, so the debugger might indicate that with 'Box=""' . Perhaps you can change some configurations of the program to see the XML-tree.

You could try the 'box' template in my 2nd mail to test if the variable contains the right xml-tree. (the case of the element names should be updated).
May be my idea to create a tree in a variable and then process it is
bad at all.


Off course it's not bad. With 'document fragment node' (in my first mail) I actually meant using a variable to store the 'pre-processed xml tree'.
Don't hesitate to post further questions or remarks.

regards,

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #5

P: n/a
Hi Joris,

thank you very much for your confirmation.
I have tried to combine our code, but I get still stuck at using the
variable which should hold the tree/tree fragment or whatever this is
called correctly. Using the variable in the select part of an
apply-templates element leads to the following error (in debug mode,
menas xmlspy processor):
"Cannot use result tree fragmentError in Xpath expression, Cannot use
result tree fragment"

Using MSXML4 I get:

"Expression must evaluate to a node-set
->$vOrgaTree<- "
I tried to get some answers by the debugger (xpath evaluation after
break) gambling with $vOrgaTree/* , $vOrgaTree/self::* ...

I have also tried to add a top element(to make the tree well formed in
sense of xml)
But all this never lead to a good result !!!

I think my basic understanding of node-set and result tree fragment is
still zero.

Please find below my xslt trial (combined code from both of us)
Thanks a lot for your help
(I feel still like a very beginner in xslt as you see)

######### xslt ########################################
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:a3="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/">
<xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>
<xsl:key name="kOrga" match="/Orga/OrgaBox" use="@BelongsTo"/>

<xsl:template mode="PROTO" name="BuildProtoBoxes" match="*">
<xsl:element name="box">
<xsl:attribute name="caption">
<xsl:value-of select="@OrgaID"/>
</xsl:attribute>
<xsl:apply-templates mode="PROTO" select="key('kOrga',@OrgaID)"/>
</xsl:element>
</xsl:template>

<xsl:template match="/">
<xsl:variable name="vOrgaTree" >
<xsl:apply-templates mode="PROTO" select="key('kOrga','169')"/>
</xsl:variable>
<xsl:value-of select="$vOrgaTree"></xsl:value-of><!-- just for test
and break point setting -->
<xsl:apply-templates mode="SVG" select="$vOrgaTree"/><!-- the error
happens here !!! -->
</xsl:template>

<xsl:template mode="SVG" match="/">
<svg contentScriptType="text/ecmascript" width="500" height="500"
viewBox="0 0 10000 30000" a3:scriptImplementation="Adobe"
zoomAndPan="magnify" contentStyleType="text/css" id="PinFunction"
preserveAspectRatio="xMidYMid meet">
<defs>
<style type="text/css">
rect {fill:yellow;stroke:red;stroke-width:10}
text {fill:black;font-size:800}
</style>
</defs>
<g id="all" transform="translate(2000,2000)">
<xsl:apply-templates select="box"/>
</g>
</svg>
</xsl:template>
<xsl:template match="box">
<g>
<xsl:attribute name="transform"><xsl:value-of
select="concat('translate(',count(ancestor::*) * 8000
,',',count(preceding::*)*3000,')')"/></xsl:attribute>
<rect width="5000" height="2000"/>
<text x="200" y="1000" pointer-events="none">
<xsl:value-of select="@caption"/>
</text>
</g>
<xsl:apply-templates select="box"/>
</xsl:template>

</xsl:stylesheet>
######################### xml just for reference as you may have it
already ###
<?xml version="1.0" encoding="UTF-8"?>
<Orga>
<OrgaBox OrgaID="131" BelongsTo="169" />
<OrgaBox OrgaID="132" BelongsTo="135" />
<OrgaBox OrgaID="133" BelongsTo="169" />
<OrgaBox OrgaID="135" BelongsTo="169" />
<OrgaBox OrgaID="136" BelongsTo="169" />
<OrgaBox OrgaID="137" BelongsTo="171" />
<OrgaBox OrgaID="138" BelongsTo="171" />
<OrgaBox OrgaID="139" BelongsTo="171" />
<OrgaBox OrgaID="140" BelongsTo="171" />
<OrgaBox OrgaID="141" BelongsTo="136" />
<OrgaBox OrgaID="142" BelongsTo="132" />
<OrgaBox OrgaID="143" BelongsTo="135" />
<OrgaBox OrgaID="144" BelongsTo="135" />
<OrgaBox OrgaID="145" BelongsTo="143" />
<OrgaBox OrgaID="146" BelongsTo="132" />
<OrgaBox OrgaID="147" BelongsTo="133" />
<OrgaBox OrgaID="148" BelongsTo="162" />
<OrgaBox OrgaID="149" BelongsTo="139" />
<OrgaBox OrgaID="150" BelongsTo="139" />
<OrgaBox OrgaID="151" BelongsTo="139" />
<OrgaBox OrgaID="155" BelongsTo="182" />
<OrgaBox OrgaID="156" BelongsTo="136" />
<OrgaBox OrgaID="157" BelongsTo="139" />
<OrgaBox OrgaID="158" BelongsTo="136" />
<OrgaBox OrgaID="159" BelongsTo="133" />
<OrgaBox OrgaID="160" BelongsTo="137" />
<OrgaBox OrgaID="161" BelongsTo="138" />
<OrgaBox OrgaID="162" BelongsTo="138" />
<OrgaBox OrgaID="163" BelongsTo="138" />
<OrgaBox OrgaID="164" BelongsTo="139" />
<OrgaBox OrgaID="165" BelongsTo="139" />
<OrgaBox OrgaID="166" BelongsTo="140" />
<OrgaBox OrgaID="167" BelongsTo="140" />
<OrgaBox OrgaID="168" BelongsTo="140" />
<OrgaBox OrgaID="169" BelongsTo="0" />
<OrgaBox OrgaID="170" BelongsTo="169" />
<OrgaBox OrgaID="171" BelongsTo="169" />
<OrgaBox OrgaID="172" BelongsTo="169" />
<OrgaBox OrgaID="174" BelongsTo="182" />
<OrgaBox OrgaID="175" BelongsTo="138" />
<OrgaBox OrgaID="182" BelongsTo="136" />
<OrgaBox OrgaID="192" BelongsTo="132" />
</Orga>

############## END ####################################


"Joris Gillis" <ro**@pandora.be> wrote in message news:<op**************@news.pandora.be>...
Hi again,
I tried the snipet below with XMLSpy (ALTOVA). In debug mode the
result of the variable is shown as Caption="169" Box="" Box="" Box=""
Box="" Box="" Box="" Box="" Which indicates that the tree is not build
correctly.
What I'm doing wrong ?
Your code generates a variable with exactly the tree you want.

(using altovaxslt.exe). The only explaination I can see, is that the
debug mode of XMLSpy doesn't show the exact XML-tree but rather a more
textual representation. The 'Box' elements contain no text-nodes, so
the debugger might indicate that with 'Box=""' . Perhaps you can
change some configurations of the program to see the XML-tree.
You could try the 'box' template in my 2nd mail to test if the variable contains the right xml-tree. (the case of the element names should be updated).
May be my idea to create a tree in a variable and then process it is
bad at all.


Off course it's not bad. With 'document fragment node' (in my first mail) I actually meant using a variable to store the 'pre-processed xml tree'.
Don't hesitate to post further questions or remarks.

regards,

Jul 20 '05 #6

P: n/a
> Using the variable in the select part of an
apply-templates element leads to the following error (in debug mode,
menas xmlspy processor):
"Cannot use result tree fragmentError in Xpath expression, Cannot use
result tree fragment"

Using MSXML4 I get:

"Expression must evaluate to a node-set
->$vOrgaTree<- "


Hi,

I forgot to tell: XSLT 1.0 doesn't allow the use of result tree fragments in XPATH expressions. It's only supported by version 1.1 . You must define this in the header:

<xsl:stylesheet version="1.1"/>

[
If the processor cannot handle XSLT 1.1, you must use an extension function like 'exsl:node-set()' , look for another processor or do the transformation with 2 seperate documents.
]

I hope you've not been spilling to much time or temper, trying to solve this issue.
regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #7

P: n/a
"Joris Gillis" <ro**@pandora.be> writes:
Using the variable in the select part of an
apply-templates element leads to the following error (in debug mode,
menas xmlspy processor):
"Cannot use result tree fragmentError in Xpath expression, Cannot use
result tree fragment"

Using MSXML4 I get:

"Expression must evaluate to a node-set
->$vOrgaTree<- "

Hi,

I forgot to tell: XSLT 1.0 doesn't allow the use of result tree
fragments in XPATH expressions. It's only supported by version 1.1
. You must define this in the header:

<xsl:stylesheet version="1.1"/>


There is no XSLT 1.1 (and never will be) there was a working draft but
the working group explictly killed off work on 1.1 in order to work on
2.0. So it will only ever be a working draft and explictly should not be
cited, except as work (not now) in progress. Unfortunately a couple of
processors do still have experimental support for this draft but it
should not be relied on.
[
If the processor cannot handle XSLT 1.1, you must use an extension function like 'exsl:node-set()' , look for another processor or do the transformation with 2 seperate documents.
]

I hope you've not been spilling to much time or temper, trying to solve this issue.
regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum

msxml has a node-set extension function in its own extension namespace.

David
Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.