<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://icl.com/saxon"
    exclude-result-prefixes="saxon xs" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xsl:variable name="worktitle">ITS 2.0</xsl:variable>
    <xsl:variable name="draftURI">http://www.w3.org/TR/2012/WD-its20-20121206/</xsl:variable>
    <xsl:variable name="stage">Second Last Call</xsl:variable>
    <xsl:variable name="productnum">6</xsl:variable>
    <xsl:variable name="commentsPeriodStartDate" as="xs:date">2013-05-21</xsl:variable>
    <xsl:variable name="commentsPeriodEndDate" as="xs:date">2013-06-11</xsl:variable>
    <xsl:preserve-space elements="*"/>

    <xsl:output method="xhtml" encoding="utf-8" indent="yes"
        doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

    <xsl:template match="/">
         <xsl:variable name="comments"
            select="database/issues/issue[product=$productnum or product='9'][(created) &gt;= $commentsPeriodStartDate and xs:date(created) &lt;= $commentsPeriodEndDate]"/>  
        <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
            <head>
                <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
                <title><xsl:copy-of select="$worktitle"/> Disposition of <xsl:copy-of
                        select="$stage"/> Comments</title>
                <style type="text/css"> table.overview tr {background: #ddd;} *.description {
                    border-width:1px; border-style:solid; border-color:blue; padding:2.5em;
                    text-align:justify; width: 100%; } table.issues-table {empty-cells: show;}
                    *.unknown {color: red;} *.duplicate { color: grey; text-decoration: line-through}</style>
            </head>
            <body>
                <div class="head">
                    <h1><xsl:copy-of select="$worktitle"/> Disposition of <xsl:copy-of
                            select="$stage"/> Comments</h1>
                    <h2>Version: <xsl:value-of select="substring(string(current-date()),1,10)"
                        /></h2>
                </div>
                <h2>Abstract</h2>

                <p>This document outlines the way in which the <a
                        href="http://www.w3.org/International/multilingualweb/lt/"
                        >MultilingualWeb-LT Working Group</a> addressed the comments submitted
                    during the <a href="{$draftURI}"><xsl:copy-of select="$worktitle"
                            /><xsl:text> </xsl:text><xsl:copy-of select="$stage"/></a> period,
                    starting <xsl:value-of select="$commentsPeriodStartDate"/> and ending (after
                    extension) 2013-06-11.</p>

                <p>Please send detailed comments on this document to <a
                        href="mailto:public-multilingualweb-lt-comments@w3.org"
                        >public-multilingualweb-lt-comments@w3.org</a>.</p>

                <p>This document is auto-generated with an <a
                        href="http://www.w3.org/International/multilingualweb/lt/drafts/its20/tools/xslt/disco-for-its20.xsl"
                        >XSLT stylesheet</a> based on the <a
                        href="http://www.w3.org/2006/07/SWD/RDFa/disco">DisCo</a> tool, using <a
                        href="http://www.w3.org/International/multilingualweb/lt/wiki/Comments_Handling#Tracker_handling"
                        >pre-defined keywords in tracker</a>.</p>

                <h2>Summary</h2>
                <p>Total number of comments: <xsl:value-of select="count($comments)"/>.</p>
                <xsl:variable name="duplicate"
                    select="$comments[notes/note[contains(description,'DUPLICATE-OF:')]]"/>
                <p>Duplicates: <xsl:value-of select="count($duplicate)"/>.</p>
                <xsl:variable name="acceptedIssues"
                    select="$comments[contains(substring-after(notes/note[contains(description,'RESOLUTION:')][last()]/description,'RESOLUTION:'),'accepted')]"/>
                <xsl:variable name="rejectedIssues"
                    select="$comments[contains(substring-after(notes/note[contains(description,'RESOLUTION:')][last()]/description,'RESOLUTION:'),'rejected')]"/>
<!--                 <xsl:variable name="resolutionUnknown"
                    select="$comments[contains(substring-after(notes/note[contains(description,'RESOLUTION:')][last()]/description,'RESOLUTION:'),'unknown')]"/>  -->
                <xsl:variable name="wgDecisionUnknown"
                    select="$comments except ($acceptedIssues union $duplicate union $rejectedIssues union $duplicate)"/>
                <xsl:variable name="reviewerSatisfied"
                    select="$comments[contains(substring-after(notes/note[contains(description,'COMMENTER-RESPONSE:')][last()]/description,'COMMENTER-RESPONSE:'),'&#x20;satisfied')]"/>
                <xsl:variable name="reviewerNotSatisfied"
                    select="$comments[contains(substring-after(notes/note[contains(description,'COMMENTER-RESPONSE:')][last()]/description,'COMMENTER-RESPONSE:'),'unsatisfied')]"/>
                <xsl:variable name="acceptedResponseUnknown" select="$acceptedIssues except ($reviewerSatisfied union $reviewerNotSatisfied union $duplicate)"/>
                <xsl:variable name="rejectedResponseUnknown" select="$rejectedIssues except ($reviewerSatisfied union $reviewerNotSatisfied union $duplicate)"/>
                <xsl:variable name="substantiveChanges" select="$comments[contains(substring-after(notes/note[contains(description,'CHANGE-TYPE:')][last()]/description,'CHANGE-TYPE:'),'substantive')]"/>
                <table width="100%" class="overview">
                    <tr>
                        <td>-</td>
                        <td align="center">Reviewer satisfied</td>
                        <td align="center">Reviewer not satisfied</td>
                        <td align="center">No reply from reviewer</td>
                        <td align="center">No resolution yet</td>
                    </tr>
                    <tr>
                        <td>WG accepted comment</td>
                        <td align="center">
                            <xsl:value-of
                                select="count($acceptedIssues intersect $reviewerSatisfied)"/>
                        </td>
                        <td align="center">
                            <xsl:value-of
                                select="count($acceptedIssues intersect $reviewerNotSatisfied)"/>
                        </td>
                        <td align="center">
                            <xsl:value-of select="count($acceptedResponseUnknown)"/>
                        </td>
                        <td rowspan="2" align="center">
                            <xsl:value-of select="count($wgDecisionUnknown)"/>
                        </td>
                    </tr>
                    <tr>
                        <td>WG rejected comment</td>
                        <td align="center">
                            <xsl:value-of
                                select="count($rejectedIssues intersect $reviewerSatisfied)"/>
                        </td>
                        <td align="center">
                            <xsl:value-of
                                select="count($rejectedIssues intersect $reviewerNotSatisfied)"/>
                        </td>
                        <td align="center">
                            <xsl:value-of select="count($rejectedResponseUnknown)"/>
                        </td>
                    </tr>
                </table>
                <h2>Issues list</h2>
                <ul>
                    <li id="accepted-issues">
                        <xsl:text>Accepted issues: </xsl:text>
                        <xsl:for-each select="$acceptedIssues">
                            <a href="{concat('#issue-',id)}">
                                <xsl:value-of select="concat('issue-',id)"/>
                            </a>
                            <xsl:text> </xsl:text>
                        </xsl:for-each>
                    </li>
                    <li id="rejected-issues">
                        <xsl:text>Rejected issues: </xsl:text>
                        <xsl:for-each select="$rejectedIssues">
                            <a href="{concat('#issue-',id)}">
                                <xsl:value-of select="concat('issue-',id)"/>
                            </a>
                            <xsl:text> </xsl:text>
                        </xsl:for-each>
                    </li>
                    <li id="duplicates-issues">
                        <xsl:text>Duplicates: </xsl:text>
                        <xsl:for-each select="$duplicate">
                            <a href="{concat('#issue-',id)}">
                                <xsl:value-of select="concat('issue-',id)"/>
                            </a>
                            <xsl:text> </xsl:text>
                        </xsl:for-each>
                    </li>
                    <li id="unresolved-issues">
                        <xsl:text>Unresolved issues: </xsl:text>
                        <xsl:for-each select="$wgDecisionUnknown"> <a href="{concat('#issue-',id)}">
                            <xsl:value-of select="concat('issue-',id)"/>
                        </a>
                            <xsl:text> </xsl:text></xsl:for-each>
                    </li>
                    <li id="no-response">
                        <xsl:text>No reponse from reviewer yet on accepted issues: </xsl:text>
                        <xsl:for-each select="$acceptedResponseUnknown"> <a href="{concat('#issue-',id)}">
                            <xsl:value-of select="concat('issue-',id)"/>
                        </a>
                            <xsl:text> </xsl:text></xsl:for-each>
                    </li>
                    <li id="no-response">
                        <xsl:text>No reponse from reviewer yet on rejected issues: </xsl:text>
                        <xsl:for-each select="$rejectedResponseUnknown"> <a href="{concat('#issue-',id)}">
                            <xsl:value-of select="concat('issue-',id)"/>
                        </a>
                            <xsl:text> </xsl:text></xsl:for-each>
                    </li>
                    <li id="substantive-changes">Substantive changes:  <xsl:for-each select="$substantiveChanges"> <a href="{concat('#issue-',id)}">
                        <xsl:value-of select="concat('issue-',id)"/>
                    </a>
                        <xsl:text> </xsl:text></xsl:for-each></li>
                </ul>
                <table cellpadding="5" border="2" class="issues-table" width="100%">
                    <tr>
                        <th>Issue</th>
                        <th>First recorded</th>
                        <th>Resolution<br/>("accepted" or "rejected")</th>
                        <th>Commenter feedback<br/>("satisfied" or "unsatisfied")</th>
                        <th>Change type<br/>("editorial" or "substantive" or "none")</th>
                        <th>Decision details</th>
                    </tr>
                    <xsl:apply-templates select="$comments"/>
                </table>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="issue[contains(notes,'DUPLICATE-OF:')]">
        <xsl:variable name="mainIssue"
            select="translate(substring-after(notes/note[contains(description,'DUPLICATE-OF:')][last()]/description,'DUPLICATE-OF:'),' ','')"/>
        <tr class="duplicate" xmlns="http://www.w3.org/1999/xhtml">
            <td>
                <a
                    href="{concat('https://www.w3.org/International/multilingualweb/lt/track/issues/',id)}">
                    <xsl:attribute name="id">issue-<xsl:value-of select="id"/></xsl:attribute>
                    <xsl:value-of select="concat('issue-',id)"/></a>: <xsl:value-of select="title"/>
            </td>
            <td>
                <xsl:value-of select="created"/>
            </td>
            <td>n/a</td>
            <td>n/a</td>
            <td>n/a</td>
            <td>Duplicate of <a href="{concat('#',$mainIssue)}"><xsl:value-of select="$mainIssue"
                    /></a></td>
        </tr>
    </xsl:template>
    <xsl:template match="issue">
        <xsl:variable name="resolution"
            select="substring-after(notes/note[contains(description,'RESOLUTION:')][last()]/description,'RESOLUTION:')"/>
        <xsl:variable name="commentors-response"
            select="substring-after(notes/note[contains(description,'COMMENTER-RESPONSE:')][last()]/description,'COMMENTER-RESPONSE:')"/>
        <xsl:variable name="change-type"
            select="substring-after(notes/note[contains(description,'CHANGE-TYPE:')][last()]/description,'CHANGE-TYPE:')"/>
        <xsl:variable name="decision-details"
            select="substring-after(notes/note[contains(description,'DECISION-DETAILS:')][last()]/description,'DECISION-DETAILS:')"/>
        <xsl:variable name="relatedComments" select="notes/note[contains(description,'RELATED-COMMENT:')]"></xsl:variable>
        <tr xmlns="http://www.w3.org/1999/xhtml">
            <td>
                <a
                    href="{concat('https://www.w3.org/International/multilingualweb/lt/track/issues/',id)}">
                    <xsl:attribute name="id">issue-<xsl:value-of select="id"/></xsl:attribute>
                    <xsl:value-of select="concat('issue-',id)"/></a>: <xsl:value-of select="title"/>
                <xsl:if test="$relatedComments">
                    [Related comments:
                    <xsl:for-each select="$relatedComments">
                        <xsl:variable name="relatedComment" select="translate(substring-after(description,'RELATED-COMMENT:'),' ','')"/>
                        <a href="{concat('#',$relatedComment)}"><xsl:value-of select="$relatedComment"/></a>
                    </xsl:for-each>]
                </xsl:if>
            </td>
            <td>
                <xsl:value-of select="created"/>
            </td>
            <td>
                <xsl:choose>
                    <xsl:when test="contains($resolution,'accepted')">accepted</xsl:when>
                    <xsl:when test="contains($resolution,'rejected')">rejected</xsl:when>
                    <xsl:otherwise>
                        <span class="unknown">unknown</span>
                    </xsl:otherwise>
                </xsl:choose>
            </td>
            <td>
                <xsl:choose>
                    <xsl:when test="contains($commentors-response,'unsatisfied')"
                        >unsatisfied</xsl:when>
                    <xsl:when test="contains($commentors-response,'&#x20;satisfied')"
                        >satisfied</xsl:when>
                    <xsl:otherwise>
                        <span class="unknown">unknown</span>
                    </xsl:otherwise>
                </xsl:choose>
                <xsl:if test="contains($commentors-response,'http://') and contains($commentors-response,'satisfied')">
                    <xsl:variable name="mail" select="replace($commentors-response,'(.*)(http)','$2')"/>
                    (<a href="{$mail}">mail</a>)
                </xsl:if>
            </td>
            <td>
                <xsl:choose>
                    <xsl:when test="contains($change-type,'editorial')">editorial</xsl:when>
                    <xsl:when test="contains($change-type,'substantive')">substantive</xsl:when>
                    <xsl:when test="contains($change-type,'none')">none</xsl:when>
                    <xsl:otherwise>
                        <span class="unknown">unknown</span>
                    </xsl:otherwise>
                </xsl:choose>
            </td>
            <td>
                <xsl:choose>
                    <xsl:when test="$decision-details">
                        <xsl:value-of select="$decision-details"/>
                    </xsl:when>
                    <xsl:otherwise>-</xsl:otherwise>
                </xsl:choose>
            </td>
        </tr>
    </xsl:template>
</xsl:transform>
