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 August 10th, 2012, 03:35 AM
Authorized User
 
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Default Advice on how to replace call-template with apply-templaes (if possible)

Hi,

I currently have a xml file like this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<data>
  <config>
    <field1 sort="group">field1</field1>
    <field2 sort="group">field2</field2>
    <field3>field3</field3>
    <field4>field4</field4>
  </config>
  <rows>
    <row>
      <field1>c</field1>
      <field2>g</field2>
      <field3>c</field3>
      <field4>test2</field4>
    </row>
    <row>
      <field1>a</field1>
      <field2>b</field2>
      <field3>z</field3>
      <field4>test</field4>
    </row>
    <row>
      <field1>a</field1>
      <field2>b</field2>
      <field3>a</field3>
      <field4>test2</field4>
    </row>
  </rows>
</data>
Now what I want to to is to generate a HTML table which is grouped according to the config-element, eg grouped by field1,field2 and then field3 and field4 ungrouped (as specified in the config).

My current (working) approach is this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xsl:output method="html" indent="yes" version="5.0"/>

  <xsl:template match="data">
    <html>
    <body>
      <table border="1">
        <thead>
          <tr>
            <xsl:for-each select="config/*">
              <th><xsl:value-of select="."/></th>
            </xsl:for-each>
          </tr>
        </thead>
        <tbody>
          <xsl:call-template name="table-helper">
            <xsl:with-param name="config" select="config"/>
          </xsl:call-template>
        </tbody>
      </table>
    </body>
    </html>
  </xsl:template>

  <xsl:template name="table-helper">
    <xsl:param name="config" required="yes"/>
    <xsl:param name="column" select="1" as="xs:integer"/>
    <xsl:param name="first-round" select="xs:boolean(1)" as="xs:boolean"/>

    <xsl:variable name="current-column" select="$config/*[$column]"/>
    <xsl:variable name="current-data" select="if($first-round) then rows/row else current-group()"/>

    <xsl:choose>

      <xsl:when test="$current-column/@sort = 'group'">
        <xsl:for-each-group select="$current-data"
                            group-by="*[local-name() = $current-column/local-name()]">
          <xsl:call-template name="table-helper">
            <xsl:with-param name="first-round" select="xs:boolean(0)"/>
            <xsl:with-param name="column" select="$column + 1"/>
            <xsl:with-param name="config" select="$config"/>
          </xsl:call-template>
        </xsl:for-each-group>
      </xsl:when>

      <xsl:otherwise>
        <xsl:for-each select="$current-data">
          <tr>
            <xsl:for-each select="for $i in $config/* return *[local-name() = $i/local-name()]">
              <td><xsl:value-of select="."/></td>
            </xsl:for-each>
          </tr>
        </xsl:for-each>
      </xsl:otherwise>

    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
Now while this works I am wondering if I can rewrite that to apply-templates and if rewriting makes sense at all. (Also: If there are any non-xsl-isms in the template please tell me)

Thx,
Florian
 
Old August 10th, 2012, 05:36 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I'll be honest I can't see what your call-template is actually meant to do.

Running it against the sample XML it simply prints out the rows, one after the other. Any attempt to 'group' (which from the wording it appears to be trying to do) seems to fail.

As for re-writing - generally in XSLT I tend to favour trying to use template matching, as this is kind of the way XSLT is 'meant' to be used (and I use 'meant' in the very loosest sense). So you might certainly benefit your knowledge of XSLT by rewriting. It might also be easier to maintain perhaps.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old August 10th, 2012, 05:41 AM
Authorized User
 
Join Date: Mar 2010
Posts: 18
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
Running it against the sample XML it simply prints out the rows, one after the other. Any attempt to 'group' (which from the wording it appears to be trying to do) seems to fail.
Oh it does group, but yes currently every row displays the full data (for now). Granted the example xml isn't the best, but take this one:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<data>
  <config>
    <field1 sort="group">field1</field1>
    <field2 sort="group">field2</field2>
    <field3>field3</field3>
    <field4>field4</field4>
  </config>
  <rows>
    <row>
      <field1>a</field1>
      <field2>b</field2>
      <field3>z</field3>
      <field4>test</field4>
    </row>
    <row>
      <field1>c</field1>
      <field2>g</field2>
      <field3>c</field3>
      <field4>test2</field4>
    </row>
    <row>
      <field1>a</field1>
      <field2>b</field2>
      <field3>a</field3>
      <field4>test2</field4>
    </row>
  </rows>
Using the above xml example you will see the first two rows in the output have field1=a, which means they have been grouped since the row with field1=c went down to the bottom. I hope that clears it up for you.

Quote:
As for re-writing - generally in XSLT I tend to favour trying to use template matching, as this is kind of the way XSLT is 'meant' to be used (and I use 'meant' in the very loosest sense). So you might certainly benefit your knowledge of XSLT by rewriting. It might also be easier to maintain perhaps.
Yes, but I don't know how to achieve the same result using matching, that's my issue…

Last edited by apollo13; August 10th, 2012 at 10:14 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT 1 apply template help sbutt XSLT 0 July 6th, 2012 11:08 AM
how to use recursive apply vs call template FXYLDY XSLT 2 June 15th, 2010 01:57 PM
How to apply template for the first w:p in w:tbl? senglory XSLT 11 January 31st, 2010 08:09 AM
Question on Apply Template vvenk XSLT 7 July 10th, 2008 12:21 PM
Difference between call-template ,apply-templates vikkiefd XSLT 4 March 12th, 2008 05:09 AM





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