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

P

Phil

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>[email protected]</email>
<no>1</no>
</contact>




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

<?xml version="1.0"?>

<contact>
<name>Johny</name>
<company>Compaq</company>
<email>[email protected]</email>
<no>2</no>
</contact>




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

<?xml version="1.0"?>

<contact>
<name>Paul</name>
<company>Hewlet-Packard</company>
<email>[email protected]</email>
<no>3</no>
</contact>




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

<?xml version="1.0"?>

<contact>
<name>Keith</name>
<company>Nortel</company>
<email>[email protected]</email>
<no>4</no>
</contact>




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

<?xml version="1.0"?>

<contact>
<name>Bill</name>
<company>Microsoft</company>
<email>[email protected]</email>
<no>5</no>
</contact>
 
J

Janwillem Borleffs

Phil said:
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
 
P

Phil

Thanks for the trick.

Do you know why the document order is changed ?
Does the "document()" function change the order ?


Thanks in advance.
 
J

Janwillem Borleffs

Phil said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top