xsl-list
[Top] [All Lists]

Re: Merge 2 files and sort output

2003-04-23 15:14:38
Sascha Slabschi wrote:
I have 2 input files - files with articles in different xml structure -
and i want to merge them to one file and sort the output file by the
date values.

Yoiu could use a control document and the document() function to pull
your two XML files in, then sort it. Lets say the control document is
<files>
  <file>file1</file>
  <file>file2</file>
</files>

and the releant XSLT snippet:
  <xsl:template match="files">
    <xsl:apply-templates select="document(file)/document/article">
      <xsl:sort select="date"/>
    </xsl:apply-templates>
  </xsl:template>

The problem is that this wouldn't work because you two files
use different date representations, and neither is amenable
to simple comparision.
If you have control over the input format, use ISO 8601 date
format consistently, for example
     <date>20020521</date>
instead of
     <date>21-05/2002</date>

So far i have made a xsl file where i have set a variable to my file2
and then i run through a for-each for file1 and a for-each for file2. So
i get an output file where all articles of file1 are first and then
those of file2 are following.

If you can settle for a two stage approach, use a slightly modified
identity transformation for merging. First the template for making
a copy and recursive descend:
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates match="node()|@*"/>
    </xsl:copy>
  </xsl:template>
Then a set of templates which convert your odd date formats into
something more usable as sorting key:
 <xsl:template match="date">
   <xsl:choose>
     <xsl:when test="day and month and year">
       <xsl:value-of select="year"/>
       <xsl:value-of select="month"/>
       <xsl:value-of select="day"/>
     </xsl:when>
     <xsl:otherwise>
       <!-- process dd/mm/yy -->
       <xsl:value-of select="substring(.,2,2)"/>
       <xsl:value-of select="substring(.,1,2)"/>
       <xsl:value-of select="substring(.,2,2)"/>
     </xsl:otherwise>
   </xsl:choose>
 </xsl:template>
Note that the processing above needs enhancements if leading zeros
for days or month can be ommitted.

J.Pietschmann


XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>