473,408 Members | 2,477 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,408 software developers and data experts.

Recursive Looping with Document Function

Hello,

I've found syntax documentation on the document XSLT function, but I haven't
found a good resource yet on how to properly use it.

As stripped down example, I have XML files that are logically nested, where
the ParentID element of one is the file name of another...chaining all the
way to the top:

<!-- BasePage.xml -->
<?xml version="1.0" encoding="utf-8" ?>
<Page>
<ParentID>Parent1.xml</ParentID>
<Objects>
<ObjectID>1</ObjectID>
<ObjectID>3</ObjectID>
<ObjectID>5</ObjectID>
</Objects>
</Page>

<!-- Parent1.xml -->
<?xml version="1.0" encoding="utf-8" ?>
<Page>
<ParentID>Parent2.xml</ParentID>
<Objects>
<ObjectID>7</ObjectID>
<ObjectID>9</ObjectID>
<ObjectID>11</ObjectID>
</Objects>
</Page>

<!-- Parent2.xml -->
<?xml version="1.0" encoding="utf-8" ?>
<Page>
<ParentID>Parent3.xml</ParentID>
<Objects>
<ObjectID>13</ObjectID>
<ObjectID>15</ObjectID>
<ObjectID>17</ObjectID>
</Objects>
</Page>

<!-- Parent3.xml -->
<?xml version="1.0" encoding="utf-8" ?>
<Page>
<ParentID></ParentID> <!-- Top-level file -->
<Objects>
<ObjectID>19</ObjectID>
<ObjectID>21</ObjectID>
<ObjectID>23</ObjectID>
</Objects>
</Page>

How can I write my XSLT file so that it will:
- Start with BasePage.xml
- Render the ObjectIDs to the page, delimited by <br>
- Get the ParentID (parent XML file name)
- Print the parent XML file's ObjectIDs
- Repeat last two steps until ParentID is empty

Thank you,

Eric

Nov 12 '05 #1
4 1260
Hello!

Not an answer to your question, but it may help:

<xslt:variable name="filename" select="'1.xml'" />
<xslt:variable name="firstFile" select="document($filename)" />
(opens 1.xml and loads it into variable firstFile)

<xslt:copy-of select="$firstFile/node()" />
(copies all contents of the variable->the file to the output)

<xslt:for-each select="$firstFile/node()">
....</xslt:for-each>
(loops through nodes)
--
Pascal Schmitt
Nov 12 '05 #2
Hi,
Tempore 16:00:01, die Thursday 15 September 2005 AD, hinc in foro {microsoft.public.xml,microsoft.public.xsl,microso ft.public.dotnet.xml} scripsit Eric <Er**@discussions.microsoft.com>:
I've found syntax documentation on the document XSLT function, but I haven't
found a good resource yet on how to properly use it.

As stripped down example, I have XML files that are logically nested, where
the ParentID element of one is the file name of another...chaining all the
way to the top:

<!-- BasePage.xml -->
<?xml version="1.0" encoding="utf-8" ?>
<Page>
<ParentID>Parent1.xml</ParentID>
<Objects>
<ObjectID>1</ObjectID>
<ObjectID>3</ObjectID>
<ObjectID>5</ObjectID>
</Objects>
</Page>


apply this stylesheet to the XML:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="Page">
<xsl:apply-templates select="Objects"/>
<xsl:apply-templates select="ParentID[not(.='')]"/>
</xsl:template>

<xsl:template match="ObjectID">
<xsl:apply-templates/><br/>
</xsl:template>

<xsl:template match="ParentID">
<xsl:apply-templates select="document(.)"/>
</xsl:template>

</xsl:stylesheet>

generates:

1<br/>3<br/>5<br/>7<br/>9<br/>11<br/>13<br/>15<br/>17<br/>19<br/>21<br/>23<br/>

regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Fiat W3C in tenebris
Nov 12 '05 #3
Wow...I had no idea it was that simple (and could be written so concisely).
Thank you so much to the both of you, Joris and Pascal.

Just out of curiosity, do you know how I could do the same thing, but only
return a single result at random? (I've seen a lot of examples with
JavaScript on single XSL transforms, but I don't know how to apply those to
an aggregate result like we have here.)

Thanks again!

Eric
"Joris Gillis" wrote:
Hi,
Tempore 16:00:01, die Thursday 15 September 2005 AD, hinc in foro {microsoft.public.xml,microsoft.public.xsl,microso ft.public.dotnet.xml} scripsit Eric <Er**@discussions.microsoft.com>:
I've found syntax documentation on the document XSLT function, but I haven't
found a good resource yet on how to properly use it.

As stripped down example, I have XML files that are logically nested, where
the ParentID element of one is the file name of another...chaining all the
way to the top:

<!-- BasePage.xml -->
<?xml version="1.0" encoding="utf-8" ?>
<Page>
<ParentID>Parent1.xml</ParentID>
<Objects>
<ObjectID>1</ObjectID>
<ObjectID>3</ObjectID>
<ObjectID>5</ObjectID>
</Objects>
</Page>


apply this stylesheet to the XML:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="Page">
<xsl:apply-templates select="Objects"/>
<xsl:apply-templates select="ParentID[not(.='')]"/>
</xsl:template>

<xsl:template match="ObjectID">
<xsl:apply-templates/><br/>
</xsl:template>

<xsl:template match="ParentID">
<xsl:apply-templates select="document(.)"/>
</xsl:template>

</xsl:stylesheet>

generates:

1<br/>3<br/>5<br/>7<br/>9<br/>11<br/>13<br/>15<br/>17<br/>19<br/>21<br/>23<br/>

regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Fiat W3C in tenebris

Nov 12 '05 #4
Tempore 16:39:07, die Thursday 15 September 2005 AD, hinc in foro {microsoft.public.xml,microsoft.public.xsl,microso ft.public.dotnet.xml} scripsit Eric <Er**@discussions.microsoft.com>:
Wow...I had no idea it was that simple (and could be written so concisely).
Thank you so much to the both of you, Joris and Pascal.

Just out of curiosity, do you know how I could do the same thing, but only
return a single result at random?

You mean, returning one single 'ObjectID' from any document in the hierarchy?
XSL has no function that returns a random value. If you can supply it externally via the parameter 'RandInteger', you could perhaps use this code:

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

<xsl:param name="RandInteger" select="915572"/>

<xsl:template match="/">
<xsl:variable name="result">
<xsl:apply-templates />
</xsl:variable>
<xsl:value-of select="substring($result,1 + 4 *
($RandInteger mod (string-length($result) div 4)),4)"/>
</xsl:template>

<xsl:template match="Page">
<xsl:apply-templates select="Objects"/>
<xsl:apply-templates select="document(ParentID[not(.='')])/*"/>
</xsl:template>

<xsl:template match="ObjectID">
<xsl:number value="." format="0001"/>
</xsl:template>

</xsl:stylesheet>
regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Gaudiam omnibus traderat W3C, nec vana fides
Nov 12 '05 #5

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

Similar topics

2
by: replace-this-with-my-name | last post by:
Hi. How do I return a string containing an entire menu-tree from a recursive function? Here is my current recursive function: function page_tree( $_i ){ //Call global mysql connection...
6
by: Phil | last post by:
Hello I'm trying to display all DIV tags of a document : Example : + <DIV id="1"> - <DIV id="1-1"></DIV> </DIV> + <DIV id="2"> - <DIV id="2-1"></DIV>
8
by: Ryan Stewart | last post by:
Putting the following code in a page seems to make it go into an infinite loop unless you give it a very simple node to work with. Either that or it's very very slow. I'm somewhat new to this,...
2
by: Ivo | last post by:
Hi, I have an audio file (.mid or .wav or .mp3) in an object element: <object id="snd" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"...
2
by: samuel.adam | last post by:
Hi all, I am coding an AJAX DHTML whatever application and I was fed up with always typing a lot of appendChild() functions. I created a custom one called append_children() and wanted to share...
2
by: Steven Burn | last post by:
..:: The Specs: MS Access 2000 (host charges extra for SQL/MySQL) MS Windows Server 2003 (prod) / MS XP SP1 (dev) ..:: The setup: The database has been setup with two tables; tblDownloads
5
by: Petr Bravenec | last post by:
I have found that when I use the RETURN NEXT command in recursive function, not all records are returned. The only records I can obtain from function are records from the highest level of...
7
by: Michael | last post by:
Hi Everyone, I got the following code from Deborah Kurata's book. I need to return the control that matches the name that I pass into the function. It does iterate through the form but it will not...
10
by: AsheeG87 | last post by:
Hello Everyone! I have a linked list and am trying to include a recursive search. However, I am having trouble understanding how I would go about that. I don't quite understand a recursive...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.