Re: [xsl] sort a node across a dynamic group of files

Subject: Re: [xsl] sort a node across a dynamic group of files
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 22 Nov 2009 14:57:41 -0500
At 2009-11-22 14:28 -0500, J. Argyl Plath wrote:
I searched the archives of this forum (and the internet at large) looking for an answer to this query before posting, but I apologize in advance if I missed something. I found lots of results that seem to work if you have a static (and known) list of files before starting. If you have any suggestions for better search terms, please let me know.

That is one of the two problems stated, the other is sorting across the files.


I am using XSLT 1.0.

I tell my students that to sort across different documents to use variables.


I have multiple XML files in a directory. The number of files is frequently growing but they are all named ####.xml (example: 0001.xml, 0002.xml, 0003.xml, etc.) It is important to know that there will *never* be any skipped numbers. If 0100.xml exists it is guaranteed that 0001-0099 exist as well.
...
An earlier plan was to try and use the recursive method above. That looked like this:


<!-- Just the relevant entry template -->
<xsl:template match="entries">

        <!-- Set up our iterators -->
        <xsl:param name="i" select="document( 'toc.xml' )//entries" />

So, $i I assume contains the number of files. The recursive trick works when you initialize a variable as the empty set and keep on unioning in the nodes of the document being opened.


Once you have all of the documents in a *single* variable, you can then sort the information.

Thank you for any help you can give on this matter. I appreciate any and all leads. I hope I haven't left any relevant information out.

A working example is below.


I hope this helps.

. . . . . . . . . . Ken

t:\ftemp>type 0001.xml
<doc>
 <name>x</name>
</doc>

t:\ftemp>type 0002.xml
<doc>
  <name>q</name>
</doc>

t:\ftemp>type 0003.xml
<doc>
  <name>z</name>
</doc>

t:\ftemp>xslt j.xsl j.xsl con last=3
<?xml version="1.0" encoding="utf-8"?>
q
x
z
t:\ftemp>type j.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:param name="last"/>

<xsl:template match="/">
  <xsl:call-template name="do-all">
    <xsl:with-param name="index" select="$last"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="do-all">
  <xsl:param name="index"/>
  <xsl:param name="docs" select="/.."/>
  <xsl:choose>
    <xsl:when test="$index>0">
      <xsl:call-template name="do-all">
        <xsl:with-param name="index" select="$index - 1"/>
        <xsl:with-param name="docs"
                        select="$docs |
                       document(concat(format-number($index,'0000'),'.xml'))"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <!--$docs is ready with all documents-->
      <!--sort by name-->
      <xsl:variable name="names" select="$docs/doc/name"/>
      <xsl:for-each select="$names">
        <xsl:sort select="."/>
        <xsl:text>
</xsl:text>
        <xsl:value-of select="."/>
      </xsl:for-each>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>

--
Vote for your XML training:   http://www.CraneSoftwrights.com/s/i/
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg&fmt=18
Video overview:  http://www.youtube.com/watch?v=VTiodiij6gE&fmt=18
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread