[xsl] Creating a classification hierarcy by using XSLT to nest similarly named elements

Subject: [xsl] Creating a classification hierarcy by using XSLT to nest similarly named elements
From: Chris Coyle <chriscoyle@xxxxxxxxx>
Date: Mon, 1 Jan 2007 08:05:35 -0800 (PST)
I am using an XSLT 2.0 processor. 
 
My XML source document contains different levels of a
classification hierarchy.  The levels of the hierarchy
are (from general to specific):
 
Category
Major
Intermediate
Minor
 
Here is a snipet from the source file:
 
<DataService version="1.5">
 <Transaction>
  <Response type="Query">
   <MasterCatalogRecord etype="Entity">
    <ExternalKeys>
     <Key name="MASTERCATALOGNAME"
type="string">CMIM</Key>
     <Key name="PRODUCTID" type="string">1</Key>
     <Key name="PRODUCTIDEXT" type="string"/>
    </ExternalKeys>
    <EntityData>
     <Attribute name="CLASSIFICATION_ID"
type="string">01</Attribute>
     <Attribute name="CLASSIFICATION_DESC"
type="string">HLTHCR</Attribute>
     <Attribute name="CLASSIFICATION_LEVEL"
type="string">CATEGORY</Attribute>
     <Attribute name="CLASSIFICATION_TYPE"
type="string">CMIM</Attribute>
    </EntityData>
   </MasterCatalogRecord>
   <MasterCatalogRecord etype="Entity">
    <ExternalKeys>
     <Key name="MASTERCATALOGNAME"
type="string">CMIM</Key>
     <Key name="PRODUCTID" type="string">2</Key>
     <Key name="PRODUCTIDEXT" type="string"/>
    </ExternalKeys>
    <EntityData>
     <Attribute name="CLASSIFICATION_ID"
type="string">02</Attribute>
     <Attribute name="CLASSIFICATION_DESC"
type="string">DAIRY</Attribute>
     <Attribute name="CLASSIFICATION_LEVEL"
type="string">CATEGORY</Attribute>
     <Attribute name="CLASSIFICATION_TYPE"
type="string">CMIM</Attribute>
    </EntityData>
   <MasterCatalogRecord etype="Entity">
    <ExternalKeys>
     <Key name="MASTERCATALOGNAME"
type="string">CMIM</Key>
     <Key name="PRODUCTID" type="string">4</Key>
     <Key name="PRODUCTIDEXT" type="string"/>
    </ExternalKeys>
    <EntityData>
     <Attribute name="CLASSIFICATION_ID"
type="string">01021</Attribute>
     <Attribute name="CLASSIFICATION_DESC"
type="string">MEDICAL</Attribute>
     <Attribute name="CLASSIFICATION_LEVEL"
type="string">MAJOR</Attribute>
     <Attribute name="CLASSIFICATION_TYPE"
type="string">CMIM</Attribute>
    </EntityData>
   </MasterCatalogRecord>
 
I am looking to format the source file like this:
 
<data jsxid="jsxroot">
 <record jsxid="0" jsxtext="Categories" jsxopen="1"
jsximg="JSX/images/icons/logo_16.gif">
  <record jsxid="1" jsxtext="HLTHCR" jsxclassid="01"
jsxclasslevel="CATEGORY" jsxclasstype="CMIM">
   <record jsxid="4" jsxtext="MEDICAL"
jsxclassid="01021" jsxclasslevel="MAJOR"
jsxclasstype="CMIM"/>
   <record jsxid="5" jsxtext="GUEST SU"
jsxclassid="01050" jsxclasslevel="MAJOR"
jsxclasstype="CMIM"/>
   <record jsxid="6" jsxtext="NONPROD "
jsxclassid="01075" jsxclasslevel="MAJOR"
jsxclasstype="CMIM"/>
  </record>
  <record jsxid="2" jsxtext="DAIRY" jsxclassid="02"
jsxclasslevel="CATEGORY" jsxclasstype="CMIM">
   <record jsxid="7" jsxtext="EGGS" jsxclassid="02001"
jsxclasslevel="MAJOR" jsxclasstype="CMIM"/>
  </record>
  <record jsxid="3" jsxtext="MEATS" jsxclassid="03"
jsxclasslevel="CATEGORY" jsxclasstype="CMIM"/>
 </record>
</data>
 
So far, I have the following for my XSLT stylesheet:
 
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:output method="xml" encoding="UTF-8"
indent="yes"/>
 <xsl:template match="/">
  <data jsxid="jsxroot">
   <record jsxid="0" jsxtext="Categories" jsxopen="1"
jsximg="JSX/images/icons/logo_16.gif">
    <record>
     <xsl:apply-templates
select="//Transaction/Response"/>
    </record>
   </record>
  </data>
 </xsl:template>
 <xsl:template match="Response">
  <xsl:for-each
select="MasterCatalogRecord/EntityData">
   <record>
    <xsl:attribute name="jsxid"><xsl:value-of
select="../ExternalKeys/Key[@name='PRODUCTID']"/></xsl:attribute>
    <xsl:attribute name="jsxtext"><xsl:value-of
select="Attribute[@name='CLASSIFICATION_DESC']"/></xsl:attribute>
    <xsl:attribute name="jsxclassid"><xsl:value-of
select="Attribute[@name='CLASSIFICATION_ID']"/></xsl:attribute>
    <xsl:attribute name="jsxclasslevel"><xsl:value-of
select="Attribute[@name='CLASSIFICATION_LEVEL']"/></xsl:attribute>
    <xsl:attribute name="jsxclasstype"><xsl:value-of
select="Attribute[@name='CLASSIFICATION_TYPE']"/></xsl:attribute>
   </record>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
 
In order to achieve the nesting of elements I desire,
I believe I need to use recursion.  I just don't know
how to express it im my stylesheet.  Any suggestions
will be greatly appreciated.


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Current Thread