<!DOCTYPE xsl:stylesheet
[
<!ENTITY % entities SYSTEM "entities.dtd" >
%entities;
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:variable name="referencedTerms">
            <ulist role="referencedTerms">
            <xsl:for-each select="//ulist[@role='termreference']/item">
                <xsl:variable name="termRefId">
                    <xsl:value-of select="p"/>
                </xsl:variable>
                <xsl:copy-of select="doc('ws-policy-framework.xml')//termdef[string(@id)=string($termRefId)]"/>
            </xsl:for-each>
            </ulist>
        </xsl:variable>
        <glist>
            <xsl:apply-templates select="//termdef | $referencedTerms//termdef">
                <xsl:sort select="@term"/>
            </xsl:apply-templates>
        </glist>
    </xsl:template>

    <xsl:template match="termdef">
        <gitem>
            <xsl:choose>                
                <xsl:when test="ancestor::ulist[@role='referencedTerms']">
                    <!-- For terms which are defined in the framework document. These are only cited in the attachment document (the "terminology" section) and need special handling.-->
                    <label>
                        <loc>
                            <xsl:attribute name="href">
                                <xsl:text>&w3c-designation-framework;#</xsl:text>
                                <xsl:value-of select="@id"/>
                            </xsl:attribute>
                            <xsl:value-of select="@term"/>
                        </loc>
                    </label>
                    <def>
                        <p>
                            <xsl:attribute name="id">
                                <xsl:value-of select="@id"/>
                            </xsl:attribute>
                            <xsl:copy-of select="node()"/>
                        </p>
                    </def>
                </xsl:when>
                <xsl:otherwise>
                    <!-- For all terms, both in the framework and the attachment document. -->
                    <label>
                        <loc>
                            <xsl:attribute name="href">
                                <xsl:text>#</xsl:text>
                                <xsl:value-of select="@id"/>
                            </xsl:attribute>
                            <xsl:value-of select="@term"/>
                        </loc>
                    </label>
                    <def>
                        <p>
                            <xsl:copy-of select="node()"/>
                        </p>
                    </def>
                </xsl:otherwise>
            </xsl:choose>
        </gitem>
    </xsl:template>
</xsl:stylesheet>

