<?xml version="1.0" encoding="UTF-8"?>

<!--
     unspsc2tm.xsl
     A stylesheet for converting an XML dump of the UNSPSC catalog
     into a topic map.
     (c) 2002, Khalil Ahmed. All rights reserved.
     Permission is granted to use, copy and redistribute this
     file as long as this notice remains intact.
     The author requests to be notified of any significant
     enhancements or bug fixes made to this file. Notification
     may be made by email to kal@techquila.com
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns="http://www.topicmaps.org/xtm/1.0/"
    xmlns:xlink="http://www.w3.org/1999/xlink">

    <xsl:template match="/">
        <topicMap>
          <topic id="entry">
            <baseName><baseNameString>UNSPSC Entry</baseNameString></baseName>
          </topic>

          <topic id="version">
            <baseName><baseNameString>UNSPSC Version</baseNameString></baseName>
          </topic>

            <topic id="segment">
                <baseName><baseNameString>UNSPSC Segment</baseNameString></baseName>
            </topic>
            
            <topic id="family">
                <baseName><baseNameString>UNSPSC Family</baseNameString></baseName>
            </topic>
            
            <topic id="class">
                <baseName><baseNameString>UNSPSC Class</baseNameString></baseName>
            </topic>
            
            <topic id="commodity">
                <baseName><baseNameString>UNSPSC Commodity</baseNameString></baseName>
            </topic>
            
            <topic id="assoc-segment-family">
                <baseName><baseNameString>UNSPSC Segment Consists Of Families</baseNameString></baseName>
                <baseName>
                    <scope><topicRef xlink:href="#segment"/></scope>
                    <baseNameString>Families</baseNameString>
                </baseName>
                <baseName>
                    <scope><topicRef xlink:href="#family"/></scope>
                    <baseNameString>Belongs To Segment</baseNameString>
                </baseName>
            </topic>
            
            <topic id="assoc-family-class">
                <baseName><baseNameString>UNSPSC Family Consists Of Classes</baseNameString></baseName>
                <baseName>
                    <scope><topicRef xlink:href="#family"/></scope>
                    <baseNameString>Classes</baseNameString>
                </baseName>
                <baseName>
                    <scope><topicRef xlink:href="#class"/></scope>
                    <baseNameString>Belongs To Family</baseNameString>
                </baseName>
            </topic>
            
            <topic id="assoc-class-commodity">
                <baseName><baseNameString>UNSPSC Class Consists Of Commodities</baseNameString></baseName>
                <baseName>
                    <scope><topicRef xlink:href="#class"/></scope>
                    <baseNameString>Commodities</baseNameString>
                </baseName>
                <baseName>
                    <scope><topicRef xlink:href="#commodity"/></scope>
                    <baseNameString>Belongs To Class</baseNameString>
                </baseName>
            </topic>

            <topic id="assoc-entry-version">
              <baseName><baseNameString>UNSPSC Entry Added In List Version</baseNameString></baseName>
              <baseName>
                <scope><topicRef xlink:href="#version"/></scope>
                <baseNameString>Introduced/Updated entries</baseNameString>
              </baseName>
              <baseName>
                <scope><topicRef xlink:href="#entry"/></scope>
                <baseNameString>Introduced/Updated in Version</baseNameString>
              </baseName>
            </topic>

            <xsl:apply-templates/>
        </topicMap>
    </xsl:template>

    <xsl:key name="adds" match="add" use="@ver"/>
    
    <xsl:template match="unspsc">
        <xsl:apply-templates mode="group" select=".//add[count(.|key('adds', @ver)[1])=1]"/>
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="add" mode="group">
        <topic id="ver.{@ver}">
            <instanceOf><topicRef xlink:href="#version"/></instanceOf>
            <baseName><baseNameString>UNSPSC Version <xsl:value-of select="@ver"/></baseNameString></baseName>
        </topic>
    </xsl:template>
    
    <xsl:template match="segment">
        <topic id="entry.{entry}">
            <instanceOf>
                <topicRef xlink:href="#segment"/>
            </instanceOf>
            <subjectIdentity>
                <subjectIndicatorRef xlink:href="urn:x-unspsc:{entry}"/>
            </subjectIdentity>
            <baseName>
                <baseNameString><xsl:value-of select="title"/></baseNameString>
            </baseName>
        </topic>
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="family">
        <topic id="entry.{entry}">
          <instanceOf><topicRef xlink:href="#family"/></instanceOf>
          <subjectIdentity>
            <subjectIndicatorRef xlink:href="urn:x-unspsc:{entry}"/>
          </subjectIdentity>
          <baseName><baseNameString><xsl:value-of select="title"/></baseNameString></baseName>
        </topic>
        <association>
            <instanceOf><topicRef xlink:href="#assoc-segment-family"/></instanceOf>
            <member>
                <roleSpec><topicRef xlink:href="#segment"/></roleSpec>
                <topicRef xlink:href="#entry.{../entry}"/>
            </member>
            <member>
                <roleSpec><topicRef xlink:href="#family"/></roleSpec>
                <topicRef xlink:href="#entry.{entry}"/>
            </member>
        </association>
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="class">
        <topic id="entry.{entry}">
          <instanceOf><topicRef xlink:href="#class"/></instanceOf>
          <subjectIdentity>
            <subjectIndicatorRef xlink:href="urn:x-unspsc:{entry}"/>
          </subjectIdentity>
          <baseName><baseNameString><xsl:value-of select="title"/></baseNameString></baseName>
        </topic>
        <association>
            <instanceOf><topicRef xlink:href="#assoc-family-class"/></instanceOf>
            <member>
                <roleSpec><topicRef xlink:href="#family"/></roleSpec>
                <topicRef xlink:href="#entry.{../entry}"/>
            </member>
            <member>
                <roleSpec><topicRef xlink:href="#class"/></roleSpec>
                <topicRef xlink:href="#entry.{entry}"/>
            </member>
        </association>
        <xsl:apply-templates/>
    </xsl:template>
    
        <xsl:template match="commodity">
        <topic id="entry.{entry}">
          <instanceOf><topicRef xlink:href="#commodity"/></instanceOf>
          <subjectIdentity>
            <subjectIndicatorRef xlink:href="urn:x-unspsc:{entry}"/>
          </subjectIdentity>
          <baseName><baseNameString><xsl:value-of select="title"/></baseNameString></baseName>
        </topic>
        <association>
            <instanceOf><topicRef xlink:href="#assoc-class-commodity"/></instanceOf>
            <member>
                <roleSpec><topicRef xlink:href="#class"/></roleSpec>
                <topicRef xlink:href="#entry.{../entry}"/>
            </member>
            <member>
                <roleSpec><topicRef xlink:href="#commodity"/></roleSpec>
                <topicRef xlink:href="#entry.{entry}"/>
            </member>
        </association>
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="add">
        <association>
            <instanceOf><topicRef xlink:href="#assoc-entry-version"/></instanceOf>
            <member>
                <roleSpec><topicRef xlink:href="#entry"/></roleSpec>
                <topicRef xlink:href="#entry.{../entry}"/>
            </member>
            <member>
                <roleSpec><topicRef xlink:href="#version"/></roleSpec>
                <topicRef xlink:href="#ver.{@ver}"/>
            </member>
        </association>
    </xsl:template>

    <xsl:template match="*">
      <!-- suppress -->
    </xsl:template>

</xsl:stylesheet>
