XML technologies / XSLT / XSLT Cookbook / Recipes for vertical XSLT applications / Generating XTM topic maps from UML through XMI
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You want to create XTM topic maps with your preferred XMI-capable UML tool.
Solution
If unfamiliar with topic maps, read the Discussion section before studying the solution.
UML was not designed for topic maps, so this example needs conventions, mostly special UML stereotypes. A stereotype extends UML by associating an identifier with a symbol indicating custom semantics. Use the stereotypes in this table:
Table: UML conventions for topic maps.
Stereotype | UML context | Meaning |
| Class | Represents a topic. Optional because this is a class’s default role in our conventions. |
| Class | Models a subject indicator, identifying the class as a subject when targeted by a |
| Class | Represents a resource targeted by a topic-occurrence relationship. |
| Class | Models an alternative topic name, implying scope. Scope members are associated with class |
| Association | Points to a resource that is an occurrence of the topic. |
| Association | Specifies the scope of the topic map characteristic. Its nature depends on the target-class stereotype ( |
| Generalization association and class | A generalization association with stereotype |
Further UML mappings are:
- UML class
Models a topic unless a non-
topicstereotype exists. The class name names the topic. If it has an attribute namedID, its default is the topic ID; otherwise the class name is also its ID.- UML association
Models a topic-map association unless a stereotype says otherwise. UML association and role names become topic-map association and role names.
- UML instantiation association
Models the topic-map relationship
instanceOf.- Unadorned UML generalization (inheritance)
Shorthand for canonical relationship
superclass-subclasswith roles assigned automatically assuperclassandsubclass. It also links classes bearing stereotypebaseNameto topics they name in alternative scopes. Generalizations with stereotypevariantalso identify variants.
Note:
If Rational Rose cannot save XMI, download the XMI or RoseXMI extension. Only Rose was tested here, but other XMI tools such as TogetherSoft’s Together Control Center should also work.
The XMI-to-XTM stylesheet follows. Several entities make long XMI element names manageable:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xslt [
<!--= = = = = = = = = = = = = = = = = = = = = = = = = = =-->
<!-- XMI-Organisationsstrukturen auf oberer Ebene -->
<!--= = = = = = = = = = = = = = = = = = = = = = = = = = =-->
<!ENTITY FC "Foundation.Core">
<!ENTITY FX "Foundation.Extension_Mechanisms">
<!ENTITY FD "Foundation.Data_Types">
<!ENTITY MM "Model_Management.Model">
<!ENTITY ME "&FC;.ModelElement">
<!--= = = = = = = = = = = = = = = = = = = = = = = = = = =-->
<!-- Abkürzungen für Grundelemente von XMI-Dateien, die für dieses Stylesheet von besonderem Interesse sind. -->
<!--= = = = = = = = = = = = = = = = = = = = = = = = = = =-->
<!-- Einige generische UML-Elemente -->
<!ENTITY ELEM "&FC;.Namespace.ownedElement">
<!-- Die Assoziation als Ganzes -->
<!ENTITY ASSOC "&FC;.Association">
<!-- Der Verbindungsteil der Assoziation -->
<!ENTITY CONN "&ASSOC;.connection">
<!-- Die Enden der Assoziation. -->
<!ENTITY END "&ASSOC;End">
<!ENTITY CONNEND "&CONN;/&END;">
<!ENTITY ENDTYPE "&END;.type">
<!-- Eine UML-Klasse -->
<!ENTITY CLASS "&FC;.Class">
<!-- Der Name eines UML-Entitys -->
<!ENTITY NAME "&FC;.ModelElement.name">
<!-- Ein UML-Sterotyp -->
<!ENTITY STEREOTYPE "&ME;.stereotype/&FX;.Stereotype">
<!-- Der Ort, an dem UML-Dokumentation in XMI gespeichert wird, die wir für Ressourcendaten benutzen -->
<!ENTITY TAGGEDVALUE "&ME;.taggedValue/&FX;.TaggedValue/&FX;.TaggedValue.value">
<!-- Eine Supertyp-Relation (Vererbung) -->
<!ENTITY SUPERTYPE "&FC;.Generalization.supertype">
<!ENTITY SUBTYPE "&FC;.Generalization.subtype">
<!ENTITY SUPPLIER "&FC;.Dependency.supplier">
<!ENTITY CLIENT "&FC;.Dependency.client">
<!ENTITY DEPENDENCY "/XMI/XMI.content/&MM;/&ELEM;/&FC;.Dependency">
<!ENTITY EXPRBODY "&FC;.Attribute.initialValue/&FD;.Expression/&FD;.Expression.body">
<!ENTITY ATTR "&CLASS;ifier.feature/&FC;.Attribute">
<!-- Wird benutzt, um auf Standard-XTM-PSIs zu zeigen -->
<!ENTITY TM.ORG "http://www.topicmaps.org/xtm/index.html">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xtm="http://www.topicmaps.org/xtm/1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:param name="termOnErr" select="true( )"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>Keys simplify stereotype identification and navigation of associations cross-referencing attributes xmi.id and xmi.idref:
<!-- Indexklassen nach ihren Namen -->
<xsl:key name="classKey" match="&CLASS;" use="@xmi.id"/>
<!-- Indexstereotypen nach Namen und xmi.id -->
<xsl:key name="stereotypeKey" match="&FX;.Stereotype" use="@xmi.id"/>
<xsl:key name="stereotypeKey" match="&FX;.Stereotype" use="&NAME;"/>
<!-- Die xmi.ids von Stereotypen haben bisher Topic Maps in UML kodiert. Wir benutzen sie hier als effizientes Mittel, um zu überprüfen, ob ein Sterotyp an ein Element angehängt ist. -->
<xsl:variable name="OCCURRENCE_ID" select="key('stereotypeKey','occurrence')/@xmi.id"/>
<xsl:variable name="RESOURCE_ID" select="key('stereotypeKey','resource')/@xmi.id"/>
<xsl:variable name="TOPIC_ID" select="key('stereotypeKey','topic')/@xmi.id"/>
<xsl:variable name="SUBJECT_ID" select="key('stereotypeKey','subject')/@xmi.id"/>
<xsl:variable name="BASENAME_ID" select="key('stereotypeKey','baseName')/@xmi.id"/>
<xsl:variable name="SCOPE_ID" select="key('stereotypeKey','scope')/@xmi.id"/>
<xsl:variable name="VARIANT_ID" select="key('stereotypeKey','variant')/@xmi.id"/>Conversion takes two passes: import topics from classes, then XTM associations from UML associations.
<xsl:template match="/">
<xtm:topicMap>
<xsl:apply-templates mode="topics"/>
<xsl:apply-templates mode="associations"/>
</xtm:topicMap>
</xsl:template>Only classes with no stereotype or stereotype topic become topics. Others represent things topics reference, such as subject indicators and resources:
<!--= = = = = = = = = = = = = = = = = = = = = = =-->
<!-- Übersetzung von UML-Klassen nach TOPICS -->
<!--= = = = = = = = = = = = = = = = = = = = = = =-->
<xsl:template match="&ELEM;/&CLASS;" mode="topics">
<!-- Topics werden als Klassen modelliert, deren Stereotyp entweder leer oder gleich 'topic' ist. -->
<xsl:if test="not(&STEREOTYPE;/@xmi.idref) or &STEREOTYPE;/@xmi.idref = $TOPIC_ID">
<xsl:variable name="topicId">
<xsl:call-template name="getTopicId">
<xsl:with-param name="class" select="."/>
<xsl:with-param name="prefix" select="''"/>
</xsl:call-template>
</xsl:variable>
<xtm:topic id="{$topicId}">
<!-- Dieses for-each dient allein dazu, den Kontext zu dem optionalen Attribut Core.Attribute namens 'subjectIdentityid' zu wechseln. -->
<xsl:for-each select="&ATTR;[&NAME; = 'subjectIdentity']">
<xtm:subjectIdentity>
<xtm:subjectIndicatorRef xlink:href="{&EXPRBODY;}"/>
</xtm:subjectIdentity>
</xsl:for-each>
<xtm:baseName>
<xtm:baseNameString>
<xsl:value-of select="&NAME;"/>
</xtm:baseNameString>
</xtm:baseName>
<xsl:apply-templates select="." mode="getAlternateBaseNames"/>
<xsl:apply-templates select="." mode="getVariants"/>
<xsl:apply-templates select="." mode="getInstanceOf">
<xsl:with-param name="classId" select="@xmi.id"/>
</xsl:apply-templates>
<xsl:apply-templates select="." mode="getOccurrences"/>
</xtm:topic>
</xsl:if>
</xsl:template>
<!-- Gibt die Topic-ID einer Topic-Klasse zurück, die deren id-Attributwert oder Name ist. -->
<xsl:template name="getTopicId">
<xsl:param name="class"/>
<xsl:param name="prefix" select="'#'"/>
<xsl:for-each select="$class">
<xsl:choose>
<xsl:when test="&ATTR;/&NAME; = 'id' ">
<xsl:value-of select="&ATTR;[&NAME; = 'id']/&EXPRBODY;"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat($prefix,&NAME;)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<!-- Gibt die Aussagegegenstands-ID der Aussagegegenstandsklasse zurück, die aus ihrem subjectIdentity-Attributwert oder Namen besteht. -->
<xsl:template name="getSubjectIdentity">
<xsl:param name="class"/>
<xsl:for-each select="$class">
<xsl:choose>
<xsl:when test="&ATTR;/&NAME; = 'subjectIdentity' ">
<xsl:value-of select="&ATTR;[&NAME; = 'subjectIdentity']&EXPRBODY;"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('#',&NAME;)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<!-- Gibt die Ressourcen-ID einer Ressourcenklasse zurück, die aus ihrem resourceName-Attributwert oder Namen besteht. -->
<xsl:template name="getResourceIdentity">
<xsl:param name="class"/>
<xsl:for-each select="$class">
<xsl:choose>
<xsl:when test="&ATTR;/&NAME; = 'resourceName' ">
<xsl:value-of select="&ATTR;[&NAME; = 'resourceName']/&EXPRBODY;"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('#',&NAME;)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>Alternative names and variants specialize the base class using UML generalization. Whether natural or an abuse, it works and provides visual cues beyond stereotype tags:
<!-- Alternative Benennungen werden durch die Traversierung von UML-Generalisierungsrelationen und die Suche nach baseName-Stereotypen gefunden. -->
<xsl:template match="&ELEM;/&CLASS;" mode="getAlternateBaseNames">
<xsl:variable name="xmiId" select="@xmi.id"/>
<xsl:for-each select="../&FC;.Generalization [&SUPERTYPE;/&CLASS;/@xmi.idref = $xmiId]">
<xsl:variable name="subtypeXmiId" select="&FC;.Generalization.subtype/&CLASS;/@xmi.idref"/>
<xsl:variable name="class" select="key('classKey',$subtypeXmiId)"/>
<xsl:if test="$class/&STEREOTYPE;/@xmi.idref = $BASENAME_ID">
<xsl:variable name="name" select="$class/&NAME;"/>
<xtm:baseName>
<xsl:call-template name="getScope">
<xsl:with-param name="class" select="$class"/>
</xsl:call-template>
<xtm:baseNameString>
<xsl:value-of select="substring-after($name,'::')"/>
</xtm:baseNameString>
</xtm:baseName>
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- Varianten werden durch die Traversierung von UML-Generalisierungsrelationen und die Suche nach baseName-Stereotypen gefunden. -->
<xsl:template match="&ELEM;/&CLASS;" mode="getVariants">
<xsl:variable name="xmiId" select="@xmi.id"/>
<xsl:for-each select="../&FC;.Generalization [&SUPERTYPE;/&CLASS;/@xmi.idref = $xmiId]">
<xsl:variable name="subtypeXmiId" select="&FC;.Generalization.subtype/&CLASS;/@xmi.idref"/>
<xsl:variable name="variantClass" select="key('classKey',$subtypeXmiId)"/>
<xsl:if test="$variantClass/&STEREOTYPE;/@xmi.idref = $VARIANT_ID">
<xsl:variable name="name" select="$variantClass/&NAME;"/>
<xtm:variant>
<xtm:variantName>
<xsl:call-template name="resourceRep">
<xsl:with-param name="class" select="$variantClass"/>
</xsl:call-template>
</xtm:variantName>
<xtm:parameters>
<xsl:call-template name="getVariantParams">
<xsl:with-param name="class" select="$variantClass"/>
</xsl:call-template>
</xtm:parameters>
<!-- Wechselt Kontext zu dieser Variante, um zu verschachtelten Varianten zu kommen, falls vorhanden. -->
<xsl:apply-templates select="$variantClass" mode="getVariants"/>
</xtm:variant>
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- Holt die Parameter einer Variante aus den Attributen der Variantenklasse -->
<xsl:template name="getVariantParams">
<xsl:param name="class"/>
<xsl:if test="not($class/&ATTR;)">
<xsl:message terminate="{$termOnErr}">A variant must have at least one parameter.</xsl:message>
</xsl:if>
<xsl:for-each select="$class/&ATTR;">
<!-- Ein Parameter wird entweder als subjectIndicatorRef oder als topicRef modelliert -->
<xsl:choose>
<xsl:when test="&STEREOTYPE;/@xmi.idref = $SUBJECT_ID">
<xtm:subjectIndicatorRef xlink:href="{&EXPRBODY;}"/>
</xsl:when>
<xsl:otherwise>
<xtm:topicRef xlink:href="{&EXPRBODY;}"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>Occurrences relate to classes holding resource data or references. Inline data may be too large for attribute values, so attribute descriptions offer alternative storage:
<!-- Topic Map-Belegstellen werden als Assoziationen zu Klassen modelliert, die Ressourcenreferenzen oder -daten enthalten. -->
<xsl:template match="&ELEM;/&CLASS;" mode="getOccurrences">
<xsl:variable name="xmiId" select="@xmi.id"/>
<!-- Suche in den Assoziationen, an denen diese Klasse beteiligt ist -->
<xsl:for-each select="../&ASSOC;[&CONN;/*/&ENDTYPE;/&CLASS;/@xmi.idref = $xmiId]">
<!-- Teste Vorhandensein des Stereotyps occurrence -->
<xsl:if test="&STEREOTYPE;/@xmi.idref = $OCCURRENCE_ID">
<!-- Hole die id der Ressource, indem das andere Ende der occurrence-Assoziation betrachtet wird. -->
<xsl:variable name="resourceId" select="&CONN;/*/&ENDTYPE;/&CLASS;[@xmi.idref != $xmiId]/@xmi.idref"/>
<!-- Hole die Klasse, die die Ressource darstellt -->
<xsl:variable name="resourceClass" select="key('classKey',$resourceId)"/>
<xtm:occurrence>
<xsl:apply-templates select="." mode="getInstanceOf">
<xsl:with-param name="classId" select="$resourceId"/>
</xsl:apply-templates>
<!-- TODO: Das kann noch nicht modelliert werden!
<xsl:call-template name="getScope">
<xsl:with-param name="class"/>
</xsl:call-template>
-->
<!-- Wir können entweder eine Ressourcenreferenz oder Ressourcendaten haben. Falls die Klasse das Attribut resourceData hat, dann ist Letzteres der Fall. -->
<xsl:call-template name="resourceRep">
<xsl:with-param name="class" select="$resourceClass"/>
</xsl:call-template>
</xtm:occurrence>
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- Dieses Template bestimmt, wie die Ressource dargestellt wird -->
<xsl:template name="resourceRep">
<xsl:param name="class" />
<xsl:variable name="resourceData">
<!-- for-each für einen Kontextwechsel -->
<xsl:for-each select="$class/&ATTR;[&NAME; = 'resourceData']">
<xsl:choose>
<!-- Die Ressourcendaten wurden in der Dokumentation des UML-Attributs kodiert -->
<xsl:when test="&TAGGEDVALUE;">
<xsl:value-of select="&TAGGEDVALUE;"/>
</xsl:when>
<!-- Die Ressourcendaten wurden im UML-Attributwert kodiert -->
<xsl:otherwise>
<xsl:value-of select="&EXPRBODY;"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<!-- Wenn wir Ressourcendaten haben, benutzen wir sie, sonst nehmen wir an, der Benutzer meinte eine Referenz. -->
<xsl:choose>
<xsl:when test="string($resourceData)">
<xtm:resourceData>
<xsl:value-of select="$resourceData"/>
</xtm:resourceData>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="resource">
<xsl:call-template name="getResourceIdentity">
<xsl:with-param name="class" select="$class"/>
</xsl:call-template>
</xsl:variable>
<xtm:resourceRef xlink:href="{$resource}"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>XTM relationships of type instanceOf are modeled as UML dependency associations, also called instantiations. This represents instanceOf naturally:
<!-- Dieses Template findet heraus, ob eine Topic-Klasse irgendwelche instanceOf-Beziehungen hat. -->
<xsl:template match="&ELEM;/&CLASS;" mode="getInstanceOf">
<xsl:param name="classId"/>
<!-- Wir iterieren über die dependency-Relationen und bestimmen, wie die Instanz dargestellt wird. -->
<xsl:for-each select="&DEPENDENCY;[&CLIENT;/&CLASS;/@xmi.idref = $classId]">
<xtm:instanceOf>
<xsl:variable name="instanceClass" select="key('classKey',&SUPPLIER;/&CLASS;/@xmi.idref)"/>
<!-- Finde heraus, ob die Instanz als Aussagegegenstand oder als Topic modelliert ist. -->
<xsl:variable name="sterotypeId" select="$instanceClass/&STEREOTYPE;/@xmi.idref"/>
<xsl:choose>
<!-- Das gilt bei der Beschreibung eines Aussagegegenstands -->
<xsl:when test="$sterotypeId = $SUBJECT_ID">
<xsl:variable name="subjectIdentity">
<xsl:call-template name="getSubjectIdentity">
<xsl:with-param name="class" select="$instanceClass"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="not(normalize-space($subjectIdentity))">
<xsl:message terminate="{$termOnErr}">Subject with no identity!</xsl:message>
</xsl:if>
<xtm:subjectIndicatorRef xlink:href="{$subjectIdentity}"/>
</xsl:when>
<!-- Sonst wird die Instanz durch ein Topic dargestellt -->
<xsl:when test="not($sterotypeId) or $sterotypeId = $TOPIC_ID">
<xsl:variable name="topicId">
<xsl:call-template name="getTopicId">
<xsl:with-param name="class" select="$instanceClass"/>
</xsl:call-template>
</xsl:variable>
<xsl:if test="not(normalize-space($topicId))">
<xsl:message terminate="{$termOnErr}">Topic with no id!</xsl:message>
</xsl:if>
<topicRef xlink:href="{$topicId}"/>
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="{$termOnErr}">
<xsl:text>instanceOf must point to a topic or a subject. </xsl:text>
<xsl:value-of select="$instanceClass/&NAME;"/>
<xsl:text> is a </xsl:text>
<xsl:value-of select="key('stereotypeKey',$sterotypeId)/&NAME;"/>
<xsl:text>.
</xsl:text>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xtm:instanceOf>
</xsl:for-each>
</xsl:template>
<xsl:template name="getScope">
<xsl:param name="class"/>
<xsl:variable name="classesAssociations" select="/*/XMI.content/*/&ELEM;/&ASSOC;[&CONN;/*/&FC;.AssociationEnd.type/&CLASS;/@xmi.idref = $class/@xmi.id]"/>
<xsl:variable name="scopeAssociations" select="$classesAssociations[&FC;.ModelElement.stereotype/&FX;.Stereotype/@xmi.idref = $SCOPE_ID]"/>
<xsl:if test="$scopeAssociations">
<xtm:scope>
<xsl:for-each select="$scopeAssociations">
<xsl:variable name="targetClassId" select="&CONN;/*/&ENDTYPE;/&CLASS;[@xmi.idref != $class/@xmi.id]/@xmi.idref"/>
<xsl:variable name="targetClass" select="key('classKey',$targetClassId)"/>
<xsl:call-template name="getScopeRef">
<xsl:with-param name="class" select="$targetClass"/>
</xsl:call-template>
</xsl:for-each>
</xtm:scope>
</xsl:if>
</xsl:template>
<xsl:template name="getScopeRef">
<xsl:param name="class"/>
<xsl:variable name="stereotypeId" select="$class/&FC;.ModelElement.stereotype/&FX;.Stereotype/@xmi.idref"/>
<xsl:choose>
<xsl:when test="not($stereotypeId) or $stereotypeId = $TOPIC_ID">
<xsl:variable name="topidId">
<xsl:call-template name="getTopicId">
<xsl:with-param name="class" select="$class"/>
</xsl:call-template>
</xsl:variable>
<xtm:topicRef xlink:href="{$topidId}"/>
</xsl:when>
<xsl:when test="$stereotypeId = $SUBJECT_ID">
<xsl:variable name="subjectId">
<xsl:call-template name="getSubjectIdentity">
<xsl:with-param name="class" select="$class"/>
</xsl:call-template>
</xsl:variable>
<xtm:subjectIndicatorRef xlink:href="{$subjectId}"/>
</xsl:when>
<xsl:when test="$stereotypeId = $RESOURCE_ID">
<xsl:variable name="resourceId">
<xsl:call-template name="getResourceIdentity">
<xsl:with-param name="class" select="$class"/>
</xsl:call-template>
</xsl:variable>
<xtm:resourceRef xlink:href="{$resourceId}"/>
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="{$termOnErr}">A Scope must be either a topicRef, subjectRef, or resourceRef!</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text( )" mode="topics"/>
<!--= = = = = = = = = = = = = = = = = = = = = = = = =-->
<!-- UML-ASSOZIATION NACH TOPIC-BEZIEHUNGEN -->
<!--= = = = = = = = = = = = = = = = = = = = = = = = =-->
<xsl:template match="&ASSOC;" mode="associations">
<!-- Nur benannte UML-Assoziationen sind Topic Map-Beziehungen -->
<xsl:if test="normalize-space(&NAME;)">
<xtm:association id="{&NAME;}">
<xtm:instanceOf>
<topicRef xlink:href="{key('stereotypeKey',&STEREOTYPE;/@xmi.idref)/&NAME;}"/>
</xtm:instanceOf>
<xsl:for-each select="&CONNEND;">
<xtm:member>
<xtm:roleSpec>
<xtm:topicRef xlink:href="{&NAME;}"/>
</xtm:roleSpec>
<xsl:variable name="topicId">
<xsl:call-template name="getTopicId">
<xsl:with-param name="class" select="key('classKey',&ENDTYPE;/&CLASS;/@xmi.idref)"/>
</xsl:call-template>
</xsl:variable>
<xtm:topicRef xlink:href="{$topicId}"/>
</xtm:member>
</xsl:for-each>
</xtm:association>
</xsl:if>
</xsl:template>
<xsl:template match="&ELEM;/&FC;.Generalization" mode="associations">
<xsl:variable name="subClassId" select="&SUBTYPE;/&CLASS;/@xmi.idref"/>
<xsl:variable name="subClass" select="key('classKey',$subClassId)"/>
<xsl:variable name="superClassId" select="&SUPERTYPE;/&CLASS;/@xmi.idref"/>
<xsl:variable name="superClass" select="key('classKey',$superClassId)"/>
<!-- Wenn es eine Generalisierungsbeziehung zwischen zwei Topics gibt, verwenden wir diese als Hinweis auf eine kanonische Oberklasse-Unterklasse-Beziehung, Idealerweise würden wir das Fehlen eines Stereotyps bei der Generalisierung verwenden, aber meine XMI-Version speichert keine Stereotyp-Angaben über Generalisierungen. -->
<xsl:if test="(not($subClass/&STEREOTYPE;/@xmi.idref) or $subClass/&STEREOTYPE;/@xmi.idref = $TOPIC_ID) and (not($superClass/&STEREOTYPE;/@xmi.idref) or $superClass/&STEREOTYPE;/@xmi.idref = $TOPIC_ID)">
<xtm:association>
<xsl:variable name="id">
<xsl:choose>
<xsl:when test="normalize-space(&NAME;)">
<xsl:value-of select="&NAME;"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@xmi.id"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:attribute name="id">
<xsl:value-of select="$id"/>
</xsl:attribute>
<xtm:instanceOf>
<subjectIndicatorRef xlink:href="&TM.ORG;#psi-superclass-subclass"/>
</xtm:instanceOf>
<xtm:member>
<xtm:roleSpec>
<xtm:subjectIndicatorRef xlink:href="&TM.ORG;#psi-superclass"/>
</xtm:roleSpec>
<xsl:variable name="superClassTopicId">
<xsl:call-template name="getTopicId">
<xsl:with-param name="class" select="$superClass"/>
</xsl:call-template>
</xsl:variable>
<xtm:topicRef xlink:href="{$superClassTopicId}"/>
</xtm:member>
<xtm:member>
<xtm:roleSpec>
<xtm:subjectIndicatorRef xlink:href="&TM.ORG;#psi-subclass"/>
</xtm:roleSpec>
<xsl:variable name="subClassTopicId">
<xsl:call-template name="getTopicId">
<xsl:with-param name="class" select="$subClass"/>
</xsl:call-template>
</xsl:variable>
<xtm:topicRef xlink:href="{$subClassTopicId}"/>
</xtm:member>
</xtm:association>
</xsl:if>
</xsl:template>
<xsl:template match="text( )" mode="associations"/>
</xsl:stylesheet>Second-pass templates convert remaining UML associations not handled by stereotypes. An association’s stereotype is the topicRef determining its type. UML has no better place for this information. Scoped associations are ignored here. Otherwise UML associations closely match topic-map associations:
<!--= = = = = = = = = = = = = = = = = = = = = = =-->
<!-- UML-ASSOZIATION NACH TOPIC-BEZIEHUNG -->
<!--= = = = = = = = = = = = = = = = = = = = = = =-->
<xsl:template match="&ASSOC;" mode="associations">
<!-- Nur benannte UML-Assoziationen sind Topic Map-Beziehungen -->
<xsl:if test="normalize-space(&NAME;)">
<xtm:association id="{&NAME;}">
<xtm:instanceOf>
<topicRef xlink:href="{key('stereotypeKey',&STEREOTYPE;/@xmi.idref)/&NAME;}"/>
</xtm:instanceOf>
<xsl:for-each select="&CONNEND;">
<xtm:member>
<xtm:roleSpec>
<xtm:topicRef xlink:href="{&NAME;}"/>
</xtm:roleSpec>
<xsl:variable name="topicId">
<xsl:call-template name="getTopicId">
<xsl:with-param name="class" select="key('classKey',&ENDTYPE;/&CLASS;/@xmi.idref)"/>
</xsl:call-template>
</xsl:variable>
<xtm:topicRef xlink:href="{$topicId}"/>
</xtm:member>
</xsl:for-each>
</xtm:association>
</xsl:if>
</xsl:template>
<xsl:template match="&ELEM;/&FC;.Generalization" mode="associations">
<xsl:variable name="subClassId" select="&SUBTYPE;/&CLASS;/@xmi.idref"/>
<xsl:variable name="subClass" select="key('classKey',$subClassId)"/>
<xsl:variable name="superClassId" select="&SUPERTYPE;/&CLASS;/@xmi.idref"/>
<xsl:variable name="superClass" select="key('classKey',$superClassId)"/>Generalization between topics indicates a canonical superclass-subclass relationship. XTM explicitly supports it through published subject indicators ( published subject indicators, or PSI). Ideally use an absent generalization stereotype, but my XMI version does not store those stereotypes:
<xsl:if test="(not($subClass/&STEREOTYPE;/@xmi.idref) or $subClass/&STEREOTYPE;/@xmi.idref = $TOPIC_ID) and (not($superClass/&STEREOTYPE;/@xmi.idref) or $superClass/&STEREOTYPE;/@xmi.idref = $TOPIC_ID)">
<xtm:association>
<xsl:variable name="id">
<xsl:choose>
<xsl:when test="normalize-space(&NAME;)">
<xsl:value-of select="&NAME;"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@xmi.id"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:attribute name="id">
<xsl:value-of select="$id"/>
</xsl:attribute>
<xtm:instanceOf>
<subjectIndicatorRef xlink:href="&TM.ORG;#psi-superclass-subclass"/>
</xtm:instanceOf>
<xtm:member>
<xtm:roleSpec>
<xtm:subjectIndicatorRef xlink:href="&TM.ORG;#psi-superclass"/>
</xtm:roleSpec>
<xsl:variable name="superClassTopicId">
<xsl:call-template name="getTopicId">
<xsl:with-param name="class" select="$superClass"/>
</xsl:call-template>
</xsl:variable>
<xtm:topicRef xlink:href="{$superClassTopicId}"/>
</xtm:member>
<xtm:member>
<xtm:roleSpec>
<xtm:subjectIndicatorRef xlink:href="&TM.ORG;#psi-subclass"/>
</xtm:roleSpec>
<xsl:variable name="subClassTopicId">
<xsl:call-template name="getTopicId">
<xsl:with-param name="class" select="$subClass"/>
</xsl:call-template>
</xsl:variable>
<xtm:topicRef xlink:href="{$subClassTopicId}"/>
</xtm:member>
</xtm:association>
</xsl:if>
</xsl:template>
<xsl:template match="text( )" mode="associations"/>
</xsl:stylesheet>Discussion
Topic maps represent knowledge about real-world subjects, helping people and computers find relevant information faster and more accurately. First discussed in a Davenport Group working paper in 1993, the group was founded by a Unix vendor and other members including O’Reilly & Associates. The ideas grew in the GCA Research Institute, now IDEAlliance, around applications of HyTime. XTM resulted from this work coordinated by independent organization TopicMaps.org.
A topic is an electronic proxy for a real subject. Rock musician Ozzy Osborne is a subject, but cannot be stored in a computer, so create a topic representing him. Topics have a name called a base name. A topic may have a universal name, properly called unconstrained ( unconstrained ), and other names specific to a scope. A scope ( scope) is a context where a characteristic holds. For names, it can indicate that topic Ozzy Osborne in scope legal equals John Michael Osbourne. Unless he legally changed his name to Ozzy, in which case his mother may be the only one interested in this scope!
A topic can reference an occurrence ( occurrence) as a relevant resource. A resource can reference addressable content (resourceRef) or contain the data itself (resourceData).
A special relationship called instanceOf identifies a specific instance of a general class. That class is referenced by another topic or subject indicator. Subject indicators ( subject indicators) clarify subjects by linking to standard published addresses, perhaps maintained by a national standards body.
Topics connect through relationships. An association ( association) names a relation between two or more topics, each with a role.
Topic maps have further features for subject knowledge. Interested readers should consult the accessible XTM specification at TopicMaps.org. Most XTM features map to UML as described above. Scope is the chief difficulty. In topic maps, a scope limits a characteristic to an environment. Names, associations and occurrences can be scoped. These conventions support names’ scopes but not those of associations and occurrences, modeled as normally context-independent UML associations. UML constraints ( constraints) could model this, but Rose’s XMI cannot handle them. Scopes are advanced features many users do not need, so beginners may not mind this limitation.
The next UML diagram models a tomato in a meal. Though quirky, Sam Hunting used the same example in XML Topic Maps: Creating and Using Topic Maps for the Web (Addison-Wesley, 2002), demonstrating features useful for checking the resulting XMI.
Figure: UML diagram of a tomato topic map.
Part of the resulting XTM file follows:
<xtm:topicMap xmlns:xtm="http://www.topicmaps.org/xtm/1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
<xtm:topic id="EN">
<xtm:subjectIdentity>
<xtm:subjectIndicatorRef xlink:href="http://www.topicmaps.org/xtm/1.0/language.xtm#en"/>
</xtm:subjectIdentity>
<xtm:baseName>
<xtm:baseNameString>EN</xtm:baseNameString>
</xtm:baseName>
</xtm:topic>
<xtm:topic id="FR">
<xtm:subjectIdentity>
<xtm:subjectIndicatorRef xlink:href="http://www.topicmaps.org/xtm/1.0/language.xtm#fr"/>
</xtm:subjectIdentity>
<xtm:baseName>
<xtm:baseNameString>FR</xtm:baseNameString>
</xtm:baseName>
</xtm:topic>
<xtm:topic id="myTomato">
<xtm:subjectIdentity>
<xtm:subjectIndicatorRef xlink:href="http://www.fed.gov/usda/doc/tomato.htm#gradeA"/>
</xtm:subjectIdentity>
<xtm:baseName>
<xtm:baseNameString>tomato</xtm:baseNameString>
</xtm:baseName>
<xtm:baseName>
<xtm:scope>
<xtm:topicRef xlink:href="#EN"/>
</xtm:scope>
<xtm:baseNameString>tomato</xtm:baseNameString>
</xtm:baseName>
<xtm:baseName>
<xtm:scope>
<xtm:topicRef xlink:href="#FR"/>
</xtm:scope>
<xtm:baseNameString>tomato</xtm:baseNameString>
</xtm:baseName>
<xtm:variant>
<xtm:variantName>
<xtm:resourceData>TMT</xtm:resourceData>
</xtm:variantName>
<xtm:parameters>
<xtm:topicRef xlink:href="cell_phone"/>
<xtm:topicRef xlink:href="TMT"/>
</xtm:parameters>
</xtm:variant>
<xtm:occurrence>
<xtm:resourceRef xlink:href="#tomato.gif"/>
</xtm:occurrence>
</xtm:topic>
<xtm:topic id="myConfite">
<xtm:baseName>
<xtm:baseNameString>tomato confite farcie aux douze saveurs</xtm:baseNameString>
</xtm:baseName>
<xtm:instanceOf>
<topicRef xlink:href="#desert"/>
</xtm:instanceOf>
</xtm:topic>
<!-- Ausgelassen -->
<xtm:association id="tomato_confite_association">
<xtm:instanceOf>
<topicRef xlink:href="ingredient_of"/>
</xtm:instanceOf>
<xtm:member>
<xtm:roleSpec>
<xtm:topicRef xlink:href="anIngredient"/>
</xtm:roleSpec>
<xtm:topicRef xlink:href="myTomato"/>
</xtm:member>
<xtm:member>
<xtm:roleSpec>
<xtm:topicRef xlink:href="aDish"/>
</xtm:roleSpec>
<xtm:topicRef xlink:href="myConfite"/>
</xtm:member>
</xtm:association>
<xtm:association id="caramels_confite">
<xtm:instanceOf>
<topicRef xlink:href="ingredient_of"/>
</xtm:instanceOf>
<xtm:member>
<xtm:roleSpec>
<xtm:topicRef xlink:href="anIngredient"/>
</xtm:roleSpec>
<xtm:topicRef xlink:href="myCarmel"/>
</xtm:member>
<xtm:member>
<xtm:roleSpec>
<xtm:topicRef xlink:href="aDish"/>
</xtm:roleSpec>
<xtm:topicRef xlink:href="myConfite"/>
</xtm:member>
</xtm:association>
<!-- Ausgelassen -->
</xtm:topicMap>See also
UML and XMI are OMG (Object Management Group) standards. Further information: UML.org and CORBA®, XML And XMI® Resource Page.
TopicMaps.org is the official topic maps and XTM site.
Recipe Generating websites from XTM topic maps shows how to generate websites from topic maps.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de