xsl-list
[Top] [All Lists]

Re: [xsl] How to cast current-group() ?

2009-04-28 11:20:01
Dear Michael

thank you for your input. I work with Saxon SA-9.1.0.6 which is not to bad with typing and not foolish strict neither...

The problem is the XSLT processor allows me to use this code snippet

  <xsl:for-each-group select="/results/match" group-by="team">
<xsl:copy-of select="myfn:MakeTableRow(current-grouping- key(),current-group())"/>
  </xsl:for-each-group>

with the function only without the type control...

<xsl:function name="myf:MakeTableRow" as="schema- element(xhtml:tr)"> <xsl:param name="team"/> <!-- as="schema-element(res:team)" --> <xsl:param name="matches"/> <!-- as="schema- element(res:match)*" -->
        <tr xsl:validation="strict">
           ...
        </tr>
    </xsl:function>

your comment

current-grouping-key() is an atomic value, not an element. You could
construct an element and give it a type by validating it, but I suspect
that's not what you want to do.


is a solution to the problem, however not the most elegant one. Type casting would be nicer...


I put the whole file in the end.

Thanks again
Leo




<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform "
    xmlns:xs="http://www.w3.org/2001/XMLSchema";
    xmlns:res="http://results.bla";
    xmlns:myf="http://leostuder.ch/funtions";

    xmlns="http://www.w3.org/1999/xhtml";
xmlns:xhtml="http://www.w3.org/1999/xhtml"; exclude-result- prefixes="xs res myf xhtml">

    <xsl:output method="xhtml" encoding="ISO-8859-1" indent="yes"
        doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd "/>

<xsl:import-schema namespace="http://results.bla"; schema- location="results.xsd"/>

    <xsl:import-schema namespace="http://www.w3.org/1999/xhtml";
schema-location="http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd "/>



<xsl:function name="myf:MakeTableRow" as="schema- element(xhtml:tr)"> <xsl:param name="team"/> <!-- as="schema-element(res:team)" --> <xsl:param name="matches"/> <!-- as="schema- element(res:match)*" -->
        <tr xsl:validation="strict">
            <td>
                <xsl:value-of select="$team"/>
            </td>
            <td>
                <xsl:value-of select="count($matches)"/>
            </td>
            <xsl:variable name="won"
select="count($matches[res:team[.=$team]/@score gt res:team[.!=$team]/@score])"
                as="xs:integer"/>
            <td>
                <xsl:value-of select="$won"/>
            </td>
            <td>
                <xsl:value-of
select="count($matches[res:team[.=$team]/@score lt res:team[.!=$team]/@score])"/>
            </td>
            <xsl:variable name="drawn"
select="count($matches[res:team[.=$team]/@score eq res:team[.!=$team]/@score])"
                as="xs:integer"/>
            <td>
                <xsl:value-of select="$drawn"/>
            </td>
            <td>
<xsl:value-of select="sum($matches/res:team[.=$team]/ @score)"/>
            </td>
            <td>
<xsl:value-of select="sum($matches/res:team[.!=$team]/ @score)"/>
            </td>
            <td>
                <xsl:number value="3*$won+$drawn"/>
            </td>
        </tr>
    </xsl:function>

    <xsl:template match="/">
        <html xsl:validation="strict">
            <!-- xsl:validation="strict" possible in html tag -->
            <head>
                <title>Spielresultate</title>
            </head>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="res:results">
        <h2>Results</h2>

        <table cellpadding="10">
            <thead>
                <tr>
                    <td>Team</td>
                    <td>Played</td>
                    <td>Won</td>
                    <td>Lost</td>
                    <td>Drawn</td>
                    <td>For</td>
                    <td>Against</td>
                    <td>Points</td>
                </tr>
            </thead>

<xsl:variable name="teamResults" as="schema- element(xhtml:tr)*"> <xsl:for-each-group select="/res:results/res:match" group-by="res:team"> <xsl:sequence select="myf:MakeTableRow(current- grouping-key(),current-group())"/>
                </xsl:for-each-group>
            </xsl:variable>
            <xsl:for-each select="$teamResults">
<xsl:sort select="./xhtml:td[8]" order="descending" data-type="number"/>
                <xsl:sort select="./xhtml:td[6]" order="descending"/>
                <xsl:sort select="./xhtml:td[7]" order="ascending"/>
                <xsl:copy-of select="."/>
            </xsl:for-each>

        </table>


    </xsl:template>


</xsl:stylesheet>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--