473,387 Members | 1,619 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,387 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 2098
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.