Loading ...
Sorry, an error occurred while loading the content.

Question about using xsl:sort and xsl:number

Expand Messages
  • Jonathan Weaver
    My goal is to get an xsl file that will take a multilevel tree that comes in with the nodes in an arbitrary order, sort the nodes at each level by some element
    Message 1 of 1 , Aug 28, 2001
    • 0 Attachment
      My goal is to get an xsl file that will take a multilevel tree that comes in
      with the nodes in an arbitrary order, sort the nodes at each level by some
      element of the node, then assign a number to that each node that reflects
      the depth and sorted order.

      so if I have
      <myNodes>
      <Atype>
      <ATID>102</ATID>
      <dSequence>2</dSequence>
      <Atype>
      <ATID>202</ATID>
      <dSequence>2</dSequence>
      </Atype>
      <Atype>
      <ATID>201</ATID>
      <dSequence>1</dSequence>
      </Atype>
      </Atype>
      <Atype>
      <ATID>101</ATID>
      <dSequence>1</dSequence>
      </Atype>
      </myNodes>

      I want to have
      <html>
      <head><title>Test numbering and sorting</title></head>
      <body>
      <p>Node 1.</p>
      <p>ATID = 101</p>
      <p>dSequence = 1</p>
      <p>Node 2.</p>
      <p>ATID = 102</p>
      <p>dSequence = 2</p>
      <p>Node 2.1.</p>
      <p>ATID = 201</p>
      <p>dSequence = 1</p>
      <p>Node 2.2.</p>
      <p>ATID = 202</p>
      <p>dSequence = 2</p>
      </body>
      </html>

      So I have xsl of
      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <xsl:template match="myNodes">
      <html>
      <head><title>Test numbering and sorting</title></head>
      <body>
      <xsl:for-each select="Atype">
      <xsl:sort order="ascending" select="Atype/dSequence"/>
      <p>Node <xsl:number format="1. " level="any"/></p>
      <xsl:call-template name="level1Node"/>
      </xsl:for-each>
      </body>
      </html>
      </xsl:template>
      <xsl:template name="level1Node">
      <p>ATID = <xsl:value-of select="ATID"/></p>
      <p>dSequence = <xsl:value-of select="dSequence"/></p>
      <xsl:for-each select="Atype">
      <xsl:sort order="ascending" select="Atype/dSequence"/>
      <p>Node <xsl:number format="1. " level="any"/></p>
      <xsl:call-template name="subNode"/>
      </xsl:for-each>
      </xsl:template>
      <xsl:template name="subNode">
      <p>ATID = <xsl:value-of select="ATID"/></p>
      <p>dSequence = <xsl:value-of select="dSequence"/></p>
      </xsl:template>
      </xsl:stylesheet>

      But what I get is
      <html xmlns:fo="http://www.w3.org/1999/XSL/Format">
      <head><title>Test numbering and sorting</title></head>
      <body>
      <p>Node 4. </p>
      <p>ATID = 101</p>
      <p>dSequence = 1</p>
      <p>Node 1. </p>
      <p>ATID = 102</p>
      <p>dSequence = 2</p>
      <p>Node 2. </p>
      <p>ATID = 202</p>
      <p>dSequence = 2</p>
      <p>Node 3. </p>
      <p>ATID = 201</p>
      <p>dSequence = 1</p>
      </body>
      </html>

      What do I have to do to get where I want to go?

      Thanks for your help. Sorry is this is a beginner's question. I'm a novice
      at XSLT.
    Your message has been successfully submitted and would be delivered to recipients shortly.