473,228 Members | 1,820 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,228 software developers and data experts.

[XSLT] Question about the include order when "document()" is called in a "for-each" instruction.

Hi everybody,

I am a XSLT beginner and the following problem really makes me crazy !

I have a main "contacts.xml" document which contains references to
several contact data XML files.
My aim is to process the contacts in a single-pass XSLT process.
That is why the "document()" function is what I need.

I call the "document()" XPath function from a "for-each" instruction.
Inside the "for-each" loop, I process the included elements : you will
notice that the referenced filenames are ordered from "contact1" to
"5".
I would like to preserve that order.

My "for-each" or my "document()" call seems to change the reference
order I specified as you can see on my program output :

Ouput:
No. 2: Johny
No. 1: Jimmy
No. 5: Bill
No. 3: Paul
No. 4: Keith
I ran my XSLT through many processors (Xalan, MSXML ...) and the
result is (hopefully !) always the same.
Before this, I understood that if no "sort" instruction was specified,
the order was always the document order.

=> Can somebody explain to me why the XSLT processor change the
include order and how element are ordered ?

Thanks in advance.


Please find my XSLT/XML file below
----------------------------------
simple-document.xslt
--------------------

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

Ouput:

<xsl:for-each select="document(/contacts/entry/@filename)">
No. <xsl:value-of select="/contact/no"/>: <xsl:value-of
select="/contact/name"/>
</xsl:for-each>

</xsl:template>
</xsl:stylesheet>

contacts.xml
------------

<?xml version="1.0"?>

<contacts>
<entry filename="contact1.xml"/>
<entry filename="contact2.xml"/>
<entry filename="contact3.xml"/>
<entry filename="contact4.xml"/>
<entry filename="contact5.xml"/>
</contacts>


contact1.xml
------------

<?xml version="1.0"?>

<contact>
<name>Jimmy</name>
<company>IBM</company>
<email>ji***@ibm.com</email>
<no>1</no>
</contact>


contact2.xml
------------

<?xml version="1.0"?>

<contact>
<name>Johny</name>
<company>Compaq</company>
<email>jo***@compaq.com</email>
<no>2</no>
</contact>


contact3.xml
------------

<?xml version="1.0"?>

<contact>
<name>Paul</name>
<company>Hewlet-Packard</company>
<email>pa**@hp.com</email>
<no>3</no>
</contact>


contact4.xml
------------

<?xml version="1.0"?>

<contact>
<name>Keith</name>
<company>Nortel</company>
<email>ke***@nortel.com</email>
<no>4</no>
</contact>


contact5.xml
------------

<?xml version="1.0"?>

<contact>
<name>Bill</name>
<company>Microsoft</company>
<email>bi**@microsoft.com</email>
<no>5</no>
</contact>
Jul 20 '05 #1
3 2088
Phil wrote:
I call the "document()" XPath function from a "for-each" instruction.
Inside the "for-each" loop, I process the included elements : you will
notice that the referenced filenames are ordered from "contact1" to
"5".
I would like to preserve that order.

My "for-each" or my "document()" call seems to change the reference
order I specified as you can see on my program output :


A way to preserve the order is to use apply-templates:

<xsl:template match="/">
<xsl:apply-templates select="contacts/entry" />
</xsl:template>

<xsl:template match="contacts/entry">
No. <xsl:value-of select="document(@filename)/contact/no"/>:
<xsl:value-of select="document(@filename)/contact/name"/>
</xsl:template>
JW

Jul 20 '05 #2
Thanks for the trick.

Do you know why the document order is changed ?
Does the "document()" function change the order ?
Thanks in advance.
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message news:<76******************************@news.terane ws.com>...
Phil wrote:
I call the "document()" XPath function from a "for-each" instruction.
Inside the "for-each" loop, I process the included elements : you will
notice that the referenced filenames are ordered from "contact1" to
"5".
I would like to preserve that order.

My "for-each" or my "document()" call seems to change the reference
order I specified as you can see on my program output :


A way to preserve the order is to use apply-templates:

<xsl:template match="/">
<xsl:apply-templates select="contacts/entry" />
</xsl:template>

<xsl:template match="contacts/entry">
No. <xsl:value-of select="document(@filename)/contact/no"/>:
<xsl:value-of select="document(@filename)/contact/name"/>
</xsl:template>
JW

Jul 20 '05 #3
Phil wrote:
Do you know why the document order is changed ?
Does the "document()" function change the order ?


Yes and no. I'm not sure what the reason is, but it probably has to do with
filesystem/os related delays which occure when the xml files are retrieved
by the document() function through the rapid moving for-each calls.

Nice topic to Google on, though ;-)
JW

Jul 20 '05 #4

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

Similar topics

3
by: Phil Powell | last post by:
Has anyone here ever done a case where you have a select multiple form element and you have to do both server-side and client-side validation? I am honestly not sure how to do it in Javascript (I...
6
by: Zhang Weiwu | last post by:
Hello. I am working with a php software project, in it (www.egroupware.org) Chinese simplified locate is "zh" while Traditional Chinese "tw". I wish to send correct language attribute in http...
1
by: putty | last post by:
I found a few posts of people asking about insertCell()/insertRow() not working in IE6 SP2, and a few others about getting "null is null or not an object" errors, but no one posted a solution...
7
by: GfxGuy | last post by:
I've seen this problem posted a million times, but I've read through all of them and can't figure out what I'm doing wrong. Simple example (this is the whole file, no editing): ---------- ...
6
by: adamrfrench | last post by:
Let it be mentioned that Javascript is not my forte, so the solution to this could very well be a simple one. I am working on an AJAX function where I can pass a URL and the target ID in, and...
2
by: Claus - Arcolutions | last post by:
I got a word document as a stream, and I want to get the text from the word document. But I cant seem to find anything to use for that purpose. The "Microsoft office ?.object" com reference, only...
8
by: Just Me | last post by:
1)How can I find the folders like "My Recent Document", "Desktop", "My Pictures",... if I want to store a file there? 2) How can I find the icon for those files if I want to display it? ...
9
by: Ant | last post by:
Hi, I'm wondering if anybody can tell me what the Validate document button is used for & how to use it. When I deliberately put some tag in that shouldn't be there, (such as an extra body tag) &...
1
by: gslim | last post by:
I am trying to implement the busybox sample from // From Mark Wagner // http://blogs.crsw.com/mark/articles/642.aspx When I get to this line I get an access denied error. Could someone give me...
43
by: Christoph Schneegans | last post by:
Hi! Okay, so positions on "text/html" XHTML are totally contradicting. Anyway! I hope there's more consensus about "application/xml" XHTML. I've recently learned that Opera 9.0b2 does not only...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.