This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Attribute-based XML Sorting using XSL


All right Matt, here you go, I'm handing it to you on a silver platter...
:-)  Just kidding.

The attached XSL file will do the sorting for you using the Muenchian method
like I described to you in the last e-mail.  However, it appears that you
might have another problem.  You say you want to do dynamic sorting?  As
long as your XML follows a specific schema/DTD, you should be able to match
the nodes no matter what the value.  However, if you were to do dynamic
sorting, that would imply use of a variable.  XSL variables, can only be set
once... they're more like constants in functional programming.  Now if you
have the luxury of using Saxon 7, with XSLT 2.0 implemented, then I've read
that variables are a little more forgiving.

http://www.fawcette.com/xmlmag/2002_06/magazine/columns/practice/kcagle/

Otherwise, you might need to re-think your approach?  Or do I not completely
understand the problem?  Drop me a line if you want more clarification, or
better yet, send the group an e-mail... there are many experts, especially
Jeni.

Later,

-Jeremy Saldate

----------------------------------------------------------------------------
-
XML
----------------------------------------------------------------------------
-
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="test.xsl" ?>
<ROWSET>
    <ROW>
        <COLUMN name="Col1" value="4-1" />
        <COLUMN name="Col2" value="4-2" />
        <COLUMN name="Col3" value="4-3" />
    </ROW>
    <ROW>
        <COLUMN name="Col1" value="1-1" />
        <COLUMN name="Col2" value="1-2" />
        <COLUMN name="Col3" value="1-3" />
    </ROW>
    <ROW>
        <COLUMN name="Col1" value="3-1" />
        <COLUMN name="Col2" value="3-2" />
        <COLUMN name="Col3" value="3-3" />
    </ROW>
    <ROW>
        <COLUMN name="Col1" value="2-1" />
        <COLUMN name="Col2" value="2-2" />
        <COLUMN name="Col3" value="2-3" />
    </ROW>
</ROWSET>

----------------------------------------------------------------------------
-
XSL
----------------------------------------------------------------------------
-
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
	<xsl:key match="/ROWSET/ROW/COLUMN" use="@name" name="columns"/>

	<xsl:template match="/">
		<html>
			<body>
			      <TABLE border="1" cellspacing="0"
cellpadding="0">
				<xsl:apply-templates select="ROWSET" />
			      </TABLE>
			</body>
		</html>
	</xsl:template>
	
<xsl:template match="ROWSET/ROW">
  <xsl:apply-templates
    select="COLUMN[generate-id(.) = generate-id(key('columns', @name)[1])]"
/>
 </xsl:template>
	
<xsl:template match="COLUMN">
	<TR STYLE="font-size:x-small;">
	<xsl:for-each select="key('columns', ./@name)">
			<TD valign="top"><xsl:value-of select="./@value"
/>&#160;</TD>
	</xsl:for-each>
	</TR>
</xsl:template>	
</xsl:stylesheet>

-----Original Message-----
From: Matt Jones [mailto:xsl-list@mattjones.org]
Sent: Monday, June 17, 2002 12:41 PM
To: XSL-List@lists.mulberrytech.com
Subject: [xsl] Attribute-based XML Sorting using XSL


Hello,
I have an XML file which rather generically describes the information to 
display in an HTML table, like so:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="attribute-sort.xsl" ?>
<ROWSET>
    <ROW>
        <COLUMN name="Col1" value="4-1" />
        <COLUMN name="Col2" value="4-2" />
        <COLUMN name="Col3" value="4-3" />
    </ROW>
    <ROW>
        <COLUMN name="Col1" value="1-1" />
        <COLUMN name="Col2" value="1-2" />
        <COLUMN name="Col3" value="1-3" />
    </ROW>
    <ROW>
        <COLUMN name="Col1" value="3-1" />
        <COLUMN name="Col2" value="3-2" />
        <COLUMN name="Col3" value="3-3" />
    </ROW>
    <ROW>
        <COLUMN name="Col1" value="2-1" />
        <COLUMN name="Col2" value="2-2" />
        <COLUMN name="Col3" value="2-3" />
    </ROW>
</ROWSET>

And an XSL file which creates an HTML table, like so:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

    <xsl:template match="/">
        <HTML><BODY>
            <xsl:apply-templates select="ROWSET"/>
        </BODY></HTML>
    </xsl:template>

    <xsl:template match="ROWSET">
        <TABLE border="1" cellspacing="0" cellpadding="0">
          <TR STYLE="font-size:x-small;">
            <xsl:for-each select="ROW[position()=1]/COLUMN">
              <xsl:value-of select="@name"/>
            </xsl:for-each>
          </TR>           
          <xsl:apply-templates select="ROW">
                <xsl:sort select="COLUMN[@value]"/>
          </xsl:apply-templates>
        </TABLE>
    </xsl:template>

    <xsl:template match="ROW">
          <!-- Output rows of the table -->
        <TR STYLE="font-size:x-small;">
        <xsl:for-each select="COLUMN">
                <TD valign="top"><xsl:apply-templates 
select="@value"/>&#160;</TD>
        </xsl:for-each>
        </TR>
    </xsl:template>
</xsl:stylesheet>

  My problem is that I'd like to be able to sort on a column name.  That 
is to say, I'd like to be able to sort the ROWs in the order of the 
@value attribute for the COLUMN which has a matching @name attribute. 
 This is rather difficult to explain--think about a list control where 
you can click any of the headers and sort the entire list based upon 
that column.

To keep this as generic as possible, I don't really want to change the 
XML, only the XSL.  

Any help would be greatly appreciated.

Peace,

Matt


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]