Re: [xsl] Merge 2 files and sort output

Subject: Re: [xsl] Merge 2 files and sort output
From: "J.Pietschmann" <j3322ptm@xxxxxxxx>
Date: Thu, 24 Apr 2003 00:14:38 +0200
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"/>

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
instead of

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.


XSL-List info and archive:

Current Thread