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

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.