Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. XSL to select and sort problem

XSL to select and sort problem

Scheduled Pinned Locked Moved XML / XSL
xmlhelpwpfregexperformance
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lea Hayes
    wrote on last edited by
    #1

    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() &gt;= number($fromRecord) and position() &lt;= 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

    L 1 Reply Last reply
    0
    • L 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() &gt;= number($fromRecord) and position() &lt;= 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

      L Offline
      L Offline
      Lea Hayes
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • L 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

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups