<?xml version="1.0" encoding="ISO-8859-1"?>
<!--

XML
This is the example of the XML this XSL understands. The string containing this XML
should be passed to the above xslt_process function as $xml parameter.
===============================
<!DOCTYPE question [

<!ELEMENT question (prefix,datapath,title?,optiontitles?,subquestion*)>
<!ELEMENT prefix (#PCDATA)>
<!ELEMENT datapath (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT optiontitles (title*)>
<!ELEMENT subquestion (title, options)>
<!ELEMENT options (option*)>
<!ELEMENT option (#PCDATA)>

<!ATTLIST title id ID #REQUIRED>
<!ATTLIST subquestion id ID #REQUIRED>
<!ATTLIST option id ID #REQUIRED>
<!ATTLIST option checked (1) #IMPLIED>

]>
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" omit-xml-declaration="yes" />
    <xsl:param name="type" select="'editor'" />
    <xsl:variable name="quot"><![CDATA["]]></xsl:variable>
    <xsl:variable name="datapath" select="question/datapath" />
    <xsl:template match="question">
        <xsl:choose>
            <xsl:when test="$type='editor'">
                <xsl:apply-templates mode="editor" select="."/>
            </xsl:when>
            <xsl:when test="$type='plaintext'">
                <xsl:apply-templates mode="plaintext" select="."/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates mode="table" select="."/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

<!-- editor mode -->
    <xsl:template match="question" mode="editor">
        <style type="text/css">@import url("<xsl:value-of select="$datapath" />/style.css");</style>
        <script type="text/javascript" src="{$datapath}/InputTable.js">//blank</script>

        <table border="0" cellspacing="0" cellpadding="0" id="{prefix}" class="tbl">
            <xsl:comment>Dynamic Table</xsl:comment>
        </table>
        <script type="text/javascript">
            <xsl:text>
                var Quests = Array(
                    Array("</xsl:text>
            <xsl:value-of select="title" />
            <xsl:text>"</xsl:text>
            <xsl:for-each select="optiontitles/title">
                <xsl:sort select="@id" />
                <xsl:text>, "</xsl:text>
                <xsl:call-template name="FixQuotes">
                    <xsl:with-param name="String" select="." />
                </xsl:call-template>
                <xsl:text>"</xsl:text>
            </xsl:for-each>
            <xsl:text>)</xsl:text>
            <xsl:if test="count(subquestion) != 0">
                <xsl:text>,
                </xsl:text>
            </xsl:if>
            <xsl:for-each select="subquestion">
                <xsl:sort select="@id" />
                <xsl:text>Array("</xsl:text>
                <xsl:call-template name="FixQuotes">
                    <xsl:with-param name="String" select="title" />
                </xsl:call-template>
                <xsl:text>"</xsl:text>
                <xsl:for-each select="options/option">
                    <xsl:sort select="@id" />
                    <xsl:text>, "</xsl:text>
                    <xsl:call-template name="FixQuotes">
                        <xsl:with-param name="String" select="." />
                    </xsl:call-template>
                    <xsl:text>"</xsl:text>
                </xsl:for-each>
                <xsl:text>)</xsl:text>
                <xsl:if test="count(//subquestion[@id &gt; current()/@id]) != 0">
                    <xsl:text>,
                    </xsl:text>
                </xsl:if>
            </xsl:for-each>
            <xsl:text>
                );

                var tb = new InputTable("</xsl:text>
            <xsl:call-template name="FixQuotes">
                <xsl:with-param name="String" select="prefix" />
            </xsl:call-template>
            <xsl:text>", "tb");
                tb.datapath = "</xsl:text>
            <xsl:call-template name="FixQuotes">
                <xsl:with-param name="String" select="$datapath" />
            </xsl:call-template>
            <xsl:text>";
                tb.setup(Quests);
            </xsl:text>
        </script>
    </xsl:template>

<!-- table mode -->

    <xsl:template match="question" mode="table">
        <table border="0" cellpadding="0" cellspacing="0" class="sq-backend-table" id="{prefix}">
            <tr>
                <td class="sq-backend-table-header">
                    <xsl:if test="count(title) = 0">
                        <div align="center">This question has not been properly defined.
                            <xsl:if test="$type='passive'">
                                It will be incorrectly displayed on the front end.
                            </xsl:if>
                        </div>
                    </xsl:if>
                    <xsl:value-of select="title" />
                </td>
                <xsl:for-each select="optiontitles/title">
                    <xsl:sort select="@id"/>
                    <td class="sq-backend-table-header">
                        <xsl:value-of select="." />
                    </td>
                </xsl:for-each>
            </tr>
            <xsl:for-each select="subquestion">
                <xsl:sort select="@id" />
                <xsl:variable name="row" select="@id" />
                <tr>
                    <xsl:if test="position() mod 2 = 0">
                        <xsl:attribute name="class">even</xsl:attribute>
                    </xsl:if>
                    <td class="sq-backend-table-header">
                        <xsl:value-of select="title" />
                    </td>
                    <xsl:for-each select="options/option">
                        <xsl:sort select="@id" />
                        <td class="sq-backend-table-cell">
                            <xsl:choose>
                                <xsl:when test="string(.)=''">
                                    <xsl:text disable-output-escaping="yes"><![CDATA[&mdash;]]></xsl:text>
                                </xsl:when>
                                <xsl:when test="string(.)!='' and $type='passive'">
                                    <xsl:value-of select="." />
                                </xsl:when>
                                <xsl:otherwise>
                                    <input type="checkbox" name="{../../../prefix}[{$row}][{@id}]" id="{../../../prefix}[{$row}][{@id}]">
                                        <xsl:if test="@checked=1">
                                            <xsl:attribute name="checked"/>
                                        </xsl:if>
                                    </input>
                                    <span onclick="this.previousSibling.previousSibling.checked = !this.previousSibling.previousSibling.checked">
                                        <xsl:value-of select="." />
                                    </span>
                                </xsl:otherwise>
                            </xsl:choose>
                        </td>
                    </xsl:for-each>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>

<!-- Change all '"' to '\"' -->

    <xsl:template name="FixQuotes">
        <xsl:param name="String" select="''" />
        <xsl:choose>
        <xsl:when test="contains($String, $quot)">
            <xsl:value-of select="concat(substring-before($String,$quot),'\',$quot)" disable-output-escaping="yes" />
            <xsl:call-template name="FixQuotes">
                <xsl:with-param name="String" select="substring-after($String,$quot)" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$String" disable-output-escaping="yes" />
        </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

<!-- plain text mode -->

    <xsl:template match="question" mode="plaintext">
        <xsl:for-each select="subquestion[options/option[@checked=1]]">
            <xsl:sort select="@id"/>
            <xsl:value-of select="concat(title, ': ')" />
            <xsl:for-each select="options/option[@checked=1]">
                <xsl:sort select="@id"/>
                <xsl:value-of select="concat(//optiontitles/title[@id=current()/@id], '(', ., ')')" />
                <xsl:if test="count(../option[@checked=1][@id &gt; current()/@id])">
                    <xsl:text>, </xsl:text>
                </xsl:if>
                <xsl:if test="count(//subquestion[options/option[@checked=1]][@id &gt; current()/../../@id])">
                    <xsl:text>,
</xsl:text>
                </xsl:if>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>