Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 16th, 2004, 11:36 PM
Registered User
 
Join Date: Jun 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to pielati
Default Sorting a delimited attribute

Hi all

Newbie to the forum (and to XSL/T :)) and i have a problem that i'm hoping someone can help me with.

I have an XML attribute that is seperated by commas(,) and pipes(|). Here's a small sample...

CC_DESCRIPTION="D686,Work,1|E004,English,2|E005,En glish,2|E012,Media,1|E231,Business Technology,1|E280,Applied Technology,1|E282,Technical Graphics,1| etc etc "

What I need to do is create 2 HTML tables with each pipe(|) seperated set of values in a new row and each comma(,) seperated value in it's own cell.

Ie..

<table>
  <tr>
    <td>D686 (code)</td>
    <td>Work (title)</td>
    <td>1 (points)</td>
  </tr>
  <tr>
    <td>E004 (code)</td>
    <td>English (title)</td>
    <td>2 (points)</td>
  </tr>
</table> etc etc

I've done this, however, all the values are in 1 table - what I need to do is now split the data - all codes starting with 'E' should be in a seperate table to those starting with 'E'.

I'm completely baffled. Any help would be appreciated.

BTW, I have no control over the layout of the XML...only the XSL.

Thanks in advance,
Perry
 
Old July 1st, 2004, 08:45 AM
Authorized User
 
Join Date: Jul 2004
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Perry,

The trick is to use recursive templates to parse the string, not trivial but the following works.

xml:
<A B="D686,Work,1|E004,English,2|E012,Media|1" />

xslt:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:template match="/">
      <table border="1">


         <xsl:call-template name="parse-row">
            <xsl:with-param name="rows">
               <xsl:value-of select="A/@B" />
            </xsl:with-param>
         </xsl:call-template>
      </table>
   </xsl:template>

   <xsl:template name="parse-row">
      <xsl:param name="rows" />


      <xsl:variable name="this-row">
         <xsl:choose>
            <xsl:when test="contains($rows, '|')">
               <xsl:value-of select="substring-before($rows, '|')" />
            </xsl:when>
            <xsl:otherwise>
               <xsl:value-of select="$rows" />
            </xsl:otherwise>
         </xsl:choose>
      </xsl:variable>


      <xsl:variable name="rows-left" select="substring-after($rows, '|')" />

      <xsl:if test="string-length($this-row)!=0">
         <tr>

            <xsl:call-template name="parse-cells">
               <xsl:with-param name="cells">
                  <xsl:value-of select="$this-row" />
               </xsl:with-param>
            </xsl:call-template>
         </tr>
      </xsl:if>


      <xsl:if test="string-length($rows-left)!=0">
         <xsl:call-template name="parse-row">
            <xsl:with-param name="rows">
               <xsl:value-of select="$rows-left" />
            </xsl:with-param>
         </xsl:call-template>
      </xsl:if>
   </xsl:template>

   <xsl:template name="parse-cells">
      <xsl:param name="cells" />

      <xsl:variable name="this-cell">
         <xsl:choose>
            <xsl:when test="contains($cells, ',')">
               <xsl:value-of select="substring-before($cells, ',')" />
            </xsl:when>

            <xsl:otherwise>
               <xsl:value-of select="$cells" />
            </xsl:otherwise>
         </xsl:choose>
      </xsl:variable>

      <xsl:variable name="cells-left">
         <xsl:value-of select="substring-after($cells, ',')" />
      </xsl:variable>

      <td>
         <xsl:value-of select="$this-cell" />
      </td>

      <xsl:if test="string-length($cells-left)!=0">
         <xsl:call-template name="parse-cells">
            <xsl:with-param name="cells">
               <xsl:value-of select="$cells-left" />
            </xsl:with-param>
         </xsl:call-template>
      </xsl:if>
   </xsl:template>
</xsl:stylesheet>

Regards
Bryan






Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting some nodes by attribute value acw274 XSLT 8 July 2nd, 2008 01:22 AM
delimited files using PHP richardk PHP How-To 0 July 16th, 2007 11:40 PM
Delimited data MargateFan XSLT 2 May 22nd, 2006 05:10 AM
Access to attribute values from class of attribute jacob C# 1 October 28th, 2005 01:11 PM
Datagrid sorting by non alphabetical sorting? LLAndy VS.NET 2002/2003 1 July 15th, 2004 01:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.