473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

XSLT: recursion (with key?) vs nesting call-template

I have a document about 4 levels deep and in my XSLT I want to generate
a unique string ID for each basic element based on its path through the
hierarchy.

If I use recursion, I am continually accessing the root element ID, here
is a typical call:

<xsl:variable name="fullPath"
select="concat('p',../../../@id,'_c',../../@id,'_r',../@id,'_s',$slaveID)
"/>

I *could* do this by nesting call-templates and passing the current id
as a parameter. Is this likely to be more or less efficient?

e.g. starting xml:

<castle id="0>
<level id="2">
<room id="2"/>
<room id="3"/>
<room id="4"/>
</level>
<level id="3">
<room id="2"/>
<room id="3"/>
<room id="4"/>
</level>
</castle>
so heres an xslt:

<xsl:template match="castle">
<xsl:fullPath select="concat('c',@id)"/>
<thisCastle uid="{$fullPath}"/>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="level">
<xsl:fullPath select="concat('c',../@id,'_l',@id)"/>
<thisLevel uid="{$fullPath}"/>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="room">
<xsl:fullPath select="concat('c',../../@id,'_l',../@id,'_r',@id)"/>
<thisRoom uid="{$fullPath}"/>
<xsl:apply-templates/>
</xsl:template>

I'm guessing this is very inefficient, accessing the castle ID for each
room. is there a better way? I could generate an ID, but would the ID
reflect the data path it took to get to a particular room?

cheers

shaun
Sep 28 '05 #1
3 2673


shaun roe wrote:

I'm guessing this is very inefficient, accessing the castle ID for each
room. is there a better way?


Well if you know that is the root element then a global variable
<xsl:variable name="rootId" select="/castle/@id" />
is all you need, then in your expressions you can use
$rootId

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 28 '05 #2
In article <43***********************@newsread4.arcor-online.net>,
Martin Honnen <ma*******@yahoo.de> wrote:
shaun roe wrote:

I'm guessing this is very inefficient, accessing the castle ID for each
room. is there a better way?


Well if you know that is the root element then a global variable
<xsl:variable name="rootId" select="/castle/@id" />
is all you need, then in your expressions you can use
$rootId


True, but accessing the root element multiple times is only the worst
symptom of my philosophy... each 'parent' Id gets recalled by each of
its children, each grandparent by both the children and grandchildren
etc. Maybe this is equally efficient as the nested template-call,
(passing the id as a parameter to children) if this information is
somehow cached... I dont know.
Sep 28 '05 #3
The following transformation implements all your requirements:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:strip-space elements="*"/>

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:attribute name="globalId">id<xsl:text/>
<xsl:number count="*" level="multiple"/>
</xsl:attribute>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

When applied on the source xml provided in your post, the result is:

<castle globalId="id1" id="0">
<level globalId="id1.1" id="2">
<room globalId="id1.1.1" id="2"/>
<room globalId="id1.1.2" id="3"/>
<room globalId="id1.1.3" id="4"/>
</level>
<level globalId="id1.2" id="3">
<room globalId="id1.2.1" id="2"/>
<room globalId="id1.2.2" id="3"/>
<room globalId="id1.2.3" id="4"/>
</level>
</castle>
Cheers,
Dimitre Novatchev
"shaun roe" <sh*******@wanadoo.fr> wrote in message
news:sh*****************************@news-reader.wanadooportails.com...
I have a document about 4 levels deep and in my XSLT I want to generate
a unique string ID for each basic element based on its path through the
hierarchy.

If I use recursion, I am continually accessing the root element ID, here
is a typical call:

<xsl:variable name="fullPath"
select="concat('p',../../../@id,'_c',../../@id,'_r',../@id,'_s',$slaveID)
"/>

I *could* do this by nesting call-templates and passing the current id
as a parameter. Is this likely to be more or less efficient?

e.g. starting xml:

<castle id="0>
<level id="2">
<room id="2"/>
<room id="3"/>
<room id="4"/>
</level>
<level id="3">
<room id="2"/>
<room id="3"/>
<room id="4"/>
</level>
</castle>
so heres an xslt:

<xsl:template match="castle">
<xsl:fullPath select="concat('c',@id)"/>
<thisCastle uid="{$fullPath}"/>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="level">
<xsl:fullPath select="concat('c',../@id,'_l',@id)"/>
<thisLevel uid="{$fullPath}"/>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="room">
<xsl:fullPath select="concat('c',../../@id,'_l',../@id,'_r',@id)"/>
<thisRoom uid="{$fullPath}"/>
<xsl:apply-templates/>
</xsl:template>

I'm guessing this is very inefficient, accessing the castle ID for each
room. is there a better way? I could generate an ID, but would the ID
reflect the data path it took to get to a particular room?

cheers

shaun

Sep 28 '05 #4

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

Similar topics

0
by: Sergio del Amo | last post by:
Hi, I use the xslt functions provided by php. I am running in my computer the package xampp(www.apachefriends.org) which includes php/apache/mysql .. In this package the php includes the sablotron...
1
by: Leo Kirch | last post by:
Hello XSLT gurus, i've got a rather difficult problem. Some explanations first. theres a signal oriented xml-file - the graphical represantation looks like: | startsignal (signal00) \...
1
by: Jens Mueller | last post by:
Hi there, this is a Java-XML Question, so I am not sure whether this is the right place, haven't found anything better .... I try to convert a Java object to XML via SAX and let the FOP...
0
by: Vincent Lefevre | last post by:
Under Linux, I currently use xsltproc (libxslt), but it is awfully slow and takes a lot of memory (e.g. more than 100 MB) when using many XML files with a large DTD (e.g. DocBook): With my DTD...
0
by: Chris Young | last post by:
Operating Ubunutu Linux 5.04 on iMac 333mhz Python 2.4.1 in IDLE 1.1.1 In trying to create a interactive drawing framework in Python I came across the idea of binding attributes of one object to...
6
by: Mike Grass | last post by:
Hi, I have an XML file similar to the following: <!-- snippet --> <selector key='USER/id' value='type1'/> <selector key='USER/id' value='type2'/> <selector key='USER/id' value='type3'/>...
4
by: dwergkees | last post by:
Hi, Got a litte problem here. I'm trying to create a XSLT file that will do a transformation from WordML format (MS Word XML format, see...
1
by: notnorwegian | last post by:
when using recursion should one use a return statement or not? there is a difference obv since with a return statement it will ultimately return a value if not recursing forever. but is there...
3
by: z1 | last post by:
hi- i am fooling around with soap and weather templates. for some reason either this if or select is failing. i am very new to xml and found this code at another site. i can show you the xml...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.