xsl-list
[Top] [All Lists]

Re: XML to XML

2003-03-26 15:18:27
This isn't necessarily the bet way but it works.

<xsl:template match="Categories">
  <xsl:apply-templates select="//Category[string-length(Code)=1]">
   <xsl:sort select="Code"/>
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="Category">
 <LevelOneCategory Code="{Code}" Description="{Description}">
<xsl:apply-templates select="../Category[string-length(Code)=2 and substring(Code,1,1)=substring(current()/Code,1,1)]" mode="two">
   <xsl:sort select="Code"/>
  </xsl:apply-templates>
 </LevelOneCategory>
</xsl:template>

<xsl:template match="Category" mode="two">
 <LevelTwoCategory Code="{Code}" Description="{Description}">
<xsl:apply-templates select="..//Category[string-length(Code)=3 and substring(Code,1,2)=substring(current()/Code,1,2)]" mode="three">
   <xsl:sort select="Code"/>
  </xsl:apply-templates>
 </LevelTwoCategory>
</xsl:template>

<xsl:template match="Category"  mode="three">
 <LevelThreeCategory Code="{Code}" Description="{Description}">
 </LevelThreeCategory>
</xsl:template>

<xsl:template match="text()">
</xsl:template>
<xsl:template match="text()"  mode="two">
</xsl:template>
<xsl:template match="text()"  mode="three">
</xsl:template>


At 02:25 PM 3/26/03 -0600, you wrote:
I need some help in writing xsl to transform XML to XML.
I am getting lost in concepts of having multiple templates

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>

Thank you!


Jim Han





 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Rick Taylor
XML Developer
PPDM Association


XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



<Prev in Thread] Current Thread [Next in Thread>