XSL to select and sort problem
-
Hi, This is the first time I've used XSL and I have got some of what I want, but I cannot get it to do everything I need, and I am not sure how good the XSL I have created is performance wise. I need the XSL to first sort records in the reverse order to what they are stored in the XML file. Then I need to select from a particular record to another. Here is the XSL I have created, but as soon as I added the pagination part it stopped giving output. :sigh:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="fromRecord" select="0" />
<xsl:variable name="toRecord" select="2" /><xsl:template match="node() |@\*"> <xsl:copy> <xsl:apply-templates select="node() |@\*"/> </xsl:copy> </xsl:template> <xsl:template match="log"> <xsl:for-each select="row\[position() >= number($fromRecord) and position() <= number($toRecord)\]" > <xsl:copy> <xsl:copy-of select="@\*"/> <xsl:apply-templates> <xsl:sort select="position()" order="descending" data-type="number"/> </xsl:apply-templates> </xsl:copy> </xsl:for-each> </xsl:template>
</xsl:stylesheet>
Any help would be fantastic! Lea Hayes
-
Hi, This is the first time I've used XSL and I have got some of what I want, but I cannot get it to do everything I need, and I am not sure how good the XSL I have created is performance wise. I need the XSL to first sort records in the reverse order to what they are stored in the XML file. Then I need to select from a particular record to another. Here is the XSL I have created, but as soon as I added the pagination part it stopped giving output. :sigh:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="fromRecord" select="0" />
<xsl:variable name="toRecord" select="2" /><xsl:template match="node() |@\*"> <xsl:copy> <xsl:apply-templates select="node() |@\*"/> </xsl:copy> </xsl:template> <xsl:template match="log"> <xsl:for-each select="row\[position() >= number($fromRecord) and position() <= number($toRecord)\]" > <xsl:copy> <xsl:copy-of select="@\*"/> <xsl:apply-templates> <xsl:sort select="position()" order="descending" data-type="number"/> </xsl:apply-templates> </xsl:copy> </xsl:for-each> </xsl:template>
</xsl:stylesheet>
Any help would be fantastic! Lea Hayes
Hi again, I have finally come up with the following which appears to do what I need. Is this the most efficient way of doing this? Or is there a simpler or more generic way?
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:param name="fromRecord" />
<xsl:param name="toRecord" />
<xsl:template match="/">
<log>
<xsl:for-each select="log/entry">
<xsl:sort select="position()" order="descending" data-type="number"/>
<xsl:if test="position() >= number($fromRecord) and position() <= number($toRecord)">
<entry>
<xsl:attribute name="type">
<xsl:value-of select="@type"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="@title"/>
</xsl:attribute>
<xsl:attribute name="message">
<xsl:value-of select="@message"/>
</xsl:attribute>
<xsl:attribute name="logged">
<xsl:value-of select="@logged"/>
</xsl:attribute>
</entry>
</xsl:if>
</xsl:for-each>
</log>
</xsl:template>
</xsl:stylesheet>Many thanks, Lea Hayes
-
Hi again, I have finally come up with the following which appears to do what I need. Is this the most efficient way of doing this? Or is there a simpler or more generic way?
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:param name="fromRecord" />
<xsl:param name="toRecord" />
<xsl:template match="/">
<log>
<xsl:for-each select="log/entry">
<xsl:sort select="position()" order="descending" data-type="number"/>
<xsl:if test="position() >= number($fromRecord) and position() <= number($toRecord)">
<entry>
<xsl:attribute name="type">
<xsl:value-of select="@type"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="@title"/>
</xsl:attribute>
<xsl:attribute name="message">
<xsl:value-of select="@message"/>
</xsl:attribute>
<xsl:attribute name="logged">
<xsl:value-of select="@logged"/>
</xsl:attribute>
</entry>
</xsl:if>
</xsl:for-each>
</log>
</xsl:template>
</xsl:stylesheet>Many thanks, Lea Hayes
lhayes00 wrote:
Is this the most efficient way of doing this?
Might be given the nature of the XML you appear to be working with. A more standard approach might be to have specific information in the XML rather than using Position() that you can query and possible sort against. That solution might be more standard and could perform better as well.
led mike