Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


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

From: Chris Coyle <chriscoyle@--------->
To:
Date: 1/1/2007 4:06:00 PM
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


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent