Lexical relations from right to left

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:output method="xml" version="1.0" encoding="utf-8" 
      omit-xml-declaration="yes" indent="yes"/>
<!-- Root element match feature structure -->    
   <xsl:template match="/fs">
      <xsl:apply-templates/>
   </xsl:template>

<!-- we see a feature, we look at the value -->
   <xsl:template match="f">
      <xsl:element name="relation">
         <xsl:attribute name="type">
            <xsl:text>is_subcategory_of</xsl:text>
         </xsl:attribute>
         <xsl:element name="source">
            <xsl:attribute name="idref">
               <xsl:value-of select="@name/generate-id()"/>
            </xsl:attribute>
         </xsl:element>
         <xsl:element name="target">
           <xsl:attribute name="idref">
             <xsl:value-of select="../../@name/generate-id()"/>
         </xsl:attribute>
        </xsl:element>
      </xsl:element>
      <xsl:apply-templates/>
   </xsl:template>

   <xsl:template match="fs">
<!-- This is for the inverse reentrancy -->
<!-- Refered fs need to have an id, so let's look at them -->
      <xsl:if test="@id">
         <xsl:variable name="fs_id">
            <xsl:value-of select="@id"/>
         </xsl:variable>

         <xsl:for-each select="//f[@fVal=$fs_id]">
            <xsl:variable name="target_id">
               <xsl:value-of select="@name/generate-id()"/>
            </xsl:variable>
            <xsl:element name="relation">
              <xsl:attribute name="type">
                <xsl:text>is_subcategory_of</xsl:text>
              </xsl:attribute>
              <xsl:for-each select="//fs[@id=$fs_id]/f">
                <xsl:element name="source">
                  <xsl:attribute name="idref">
                    <xsl:value-of select="@name/generate-id()"/>
                    </xsl:attribute>
                 </xsl:element>
              </xsl:for-each>
              <xsl:element name="target">
                <xsl:attribute name="idref">
                  <xsl:value-of select="$target_id"/>
                </xsl:attribute>
               </xsl:element>
            </xsl:element>
         </xsl:for-each>
      </xsl:if>
      <xsl:apply-templates/>
   </xsl:template>

<!-- Here we have the terminal symbols str and plus 
 we can deal with them easily by referring to them and 
 looking at  the feature -->
<!-- Strings are attribute value structures, easy to handle --> 
   <xsl:template match="str">
      <xsl:element name="relation">
         <xsl:attribute name="type">
            <xsl:text>is_value_of</xsl:text>
         </xsl:attribute>
         <xsl:element name="source">
            <xsl:attribute name="idref">
               <xsl:value-of select="./generate-id()"/>
            </xsl:attribute>
         </xsl:element>
         <xsl:element name="target">
            <xsl:attribute name="idref">
               <xsl:value-of select="../@name/generate-id()"/>
            </xsl:attribute>
         </xsl:element>
      </xsl:element>
      <xsl:apply-templates/>
   </xsl:template>

   <xsl:template match="plus">
      <xsl:element name="relation">
         <xsl:attribute name="type">
            <xsl:text>is_value_of</xsl:text>
         </xsl:attribute>
         <xsl:element name="source">
            <xsl:attribute name="idref">
               <xsl:value-of select="./generate-id()"/>
            </xsl:attribute>
         </xsl:element>
         <xsl:element name="target">
            <xsl:attribute name="idref">
               <xsl:value-of select="../@name/generate-id()"/>
            </xsl:attribute>
         </xsl:element>
      </xsl:element>
      <xsl:apply-templates/>
   </xsl:template>

</xsl:stylesheet>
Thorsten Trippel 2006-11-18