| Subject: Re: [xsl] XML to XML From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx> Date: Wed, 26 Mar 2003 17:47:40 -0500 | 
<xsl:key name="level1" match="Category[string-length(Code)=1]" use="'all'"/>
<xsl:key name="level2" match="Category[string-length(Code)=2]" use="substring(Code, 1, 1)"/>
<xsl:key name="level3" match="Category[string-length(Code)=3]" use="substring(Code, 1, 2)"/>
<xsl:template match="/">
  <Categories>
    <xsl:apply-templates select="key('level1','all')"/>
  </Categories>
</xsl:template><xsl:template match="Category[string-length(Code)=1]">
  <!-- matches only level 1 nodes -->
  <LevelOneCategory Code="{Code}" Description="{Description}">
    <xsl:apply-templates select="key('level2',Code)"/>
    <!-- goes and gets the level 2 categories keyed to the value
         of this one's Code -->
  </LevelOneCategory>
</xsl:template><xsl:template match="Category[string-length(Code)=2]">
  <!-- matches level 2 nodes -->
  <LevelTwoCategory Code="{Code}" Description="{Description}">
    <xsl:apply-templates select="key('level3',Code)"/>
    <!-- goes and gets the level 3 categories keyed to the value
         of this one's Code -->
  </LevelTwoCategory>
</xsl:template><xsl:template match="Category[string-length(Code)=3]">
  <!-- matches level 3 nodes -->
  <LevelThreeCategory Code="{Code}" Description="{Description}"/>
</xsl:template>I hope that helps, Wendell
Original: <Categories> <Category> <Code>A</Code> <Description>Airplanes</Description> </Category> <Category> <Code>AA</Code> <Description>Airplanes (ARF)</Description> </Category> <Category> <Code>AAE</Code> <Description>Airplanes (ARF), Electric</Description> </Category> <Category> <Code>AAG</Code> <Description>Airplanes (ARF), Giant</Description> </Category> <Category> <Code>AAP</Code> <Description>Airplanes (ARF), Sailplane</Description> </Category> <Category> <Code>B</Code> <Description>Boats</Description> </Category> <Category> <Code>BA</Code> <Description>Boats (ARF)</Description> </Category> <Category> <Code>BAE</Code> <Description>Boats (ARF), Electric</Description> </Category> <Categories>
Final Result - where one letter, two letter, and three letter categories are nested.
<Categories> <LevelOneCategory Code="A" Description="Airplanes"> <LevelTwoCategory Code="AA" Description="Airplanes (ARF)"> <LevelThreeCategory Code="AAE" Description="Airplanes (ARF), Electric"/> <LevelThreeCategory Code="AAG" Description="Airplanes (ARF), Giant"/> <LevelThreeCategory Code="AAP" Description="Airplanes (ARF), Sailplane"/> </LevelTwoCategory> </LevelOneCategory> <LevelOneCategory Code="B" Description="Boats"> <LevelTwoCategory Code="BA" Description="Boats (ARF)"> <LevelThreeCategory Code="BAE" Description="Boats (ARF), Electric"/> </LevelTwoCategory> </LevelOneCategory> <Categories>
====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================
| Current Thread | 
|---|
| 
 | 
| <- Previous | Index | Next -> | 
|---|---|---|
| Re: [xsl] XML to XML, Rick Taylor | Thread | RE: [xsl] XML to XML, Wendell Piez | 
| Re: [xsl] example of really good XS, S Woodside | Date | RE: [xsl] XML to XML, Wendell Piez | 
| Month |