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

Discussion in 'XML' started by Phil, May 25, 2004.

  1. Phil

    Phil Guest

    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>
    <no>1</no>
    </contact>




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

    <?xml version="1.0"?>

    <contact>
    <name>Johny</name>
    <company>Compaq</company>
    <email></email>
    <no>2</no>
    </contact>




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

    <?xml version="1.0"?>

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




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

    <?xml version="1.0"?>

    <contact>
    <name>Keith</name>
    <company>Nortel</company>
    <email></email>
    <no>4</no>
    </contact>




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

    <?xml version="1.0"?>

    <contact>
    <name>Bill</name>
    <company>Microsoft</company>
    <email></email>
    <no>5</no>
    </contact>
     
    Phil, May 25, 2004
    #1
    1. Advertising

  2. 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
     
    Janwillem Borleffs, May 25, 2004
    #2
    1. Advertising

  3. Phil

    Phil Guest

    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" <> wrote in message news:<>...
    > 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
     
    Phil, May 27, 2004
    #3
  4. 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
     
    Janwillem Borleffs, May 27, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rosa
    Replies:
    1
    Views:
    438
    Peter Flynn
    Aug 6, 2004
  2. Replies:
    4
    Views:
    683
  3. gouranga
    Replies:
    4
    Views:
    490
    Joe Kesselman
    Aug 3, 2006
  4. Martin Welch
    Replies:
    4
    Views:
    541
    Martin Welch
    Sep 7, 2007
  5. Andreas Bogenberger
    Replies:
    3
    Views:
    930
    Andreas Bogenberger
    Feb 22, 2008
Loading...

Share This Page