Struggling with xsl:sort

D

Derek Tinney

Hi,

I'm having difficulty building an XLST file that allows me to sort a list of
log records. I put together an XSL file that allows me to output a copy of
the input file and then I attempted to sort it. Eventually I want to filter
it based on the "when" element (and/or others) but I cannot proceed until I
get the sort to work. I have tried several approaches (specific XPATHs,
data-type on the sort) none of which have worked (or have produced unsorted
output, and this is the one I feel is simplest and most logical. What am I
doing wrong?

Any assistance appreciated.

..js driver
var xmlRetDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlRetDoc.async = false
xmlRetDoc.load("newinput.xml")

// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM");
xsl.async = false;
xsl.load("XSL Log Report.xsl");

// Transform
var newxml = new ActiveXObject("Microsoft.XMLDOM");
newxml.loadXML(xmlRetDoc.transformNode(xsl));
newxml.save("newxml.xml");

Input file:
<?xml version="1.0" encoding="UTF-16"?>
<ATfES>
<log type="I" level="4000"
id="000000000001-5b96fa11-efd2-4cf0-86cb-dab84186fac6">
<when>2004-10-29T15:21:18</when>
<user>VIC\ATSystem</user>
</log>
<log type="I" level="4000"
id="000000000002-46be7d71-800f-49e2-ab0f-ed14a35e197c">
<when>2004-10-29T15:21:17</when>
<user>VIC\ATSystem</user>
</log>
<log type="I" level="4000"
id="000000000003-86a927bf-b06f-42cc-8f3b-7f76231271d5">
<when>2004-10-29T15:21:15</when>
<user>VIC\ATSystem</user>
</log>
<log type="I" level="3000"
id="000000000004-bbec8881-f09f-442a-8d98-d2f04f02c695">
<when>2004-10-29T15:21:20</when>
<user>VIC\ATSystem</user>
</log>
<log type="I" level="3000"
id="000000000005-f83ba729-29a2-4dd3-b97c-25919d926d59">
<when>2004-10-29T15:21:19</when>
<user>VIC\ATSystem</user>
</log>
</ATfES>

XSL File 1 - this one successfully copies the input file to a new output
file
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="xml" indent="yes"/>

<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

XSL File 2 - creates no output - added select on "log", sort on "when" and
template for "log"
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="xml" indent="yes"/>

<xsl:template match="/">
<xsl:apply-templates select="log">
<xsl:sort select="when"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="log">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>
 
J

Joris Gillis

Hi,

I'm having difficulty building an XLST file that allows me to sort a list of
log records. I put together an XSL file that allows me to output a copy of
the input file and then I attempted to sort it. Eventually I want to filter
it based on the "when" element (and/or others) but I cannot proceed until I
get the sort to work. I have tried several approaches (specific XPATHs,
data-type on the sort) none of which have worked (or have produced unsorted
output, and this is the one I feel is simplest and most logical. What am I
doing wrong?

XSL File 2 - creates no output - added select on "log", sort on "when" and
template for "log"
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:eek:utput method="xml" indent="yes"/>

<xsl:template match="/">
<xsl:apply-templates select="log">
<xsl:sort select="when"/>
</xsl:apply-templates>
</xsl:template>

Hi,

The problem is not the sort but the apply-templates.

Use this and you will get the output you want:

<xsl:apply-templates select="*/log">
<xsl:sort select="when"/>
</xsl:apply-templates>


regards,
 

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,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top