[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XML to XML

Subject: Re: XML to XML
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Wed, 26 Mar 2003 23:56:48 +0100
bae code
Hi Jim,

Try something like this:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:vendor="http://icl.com/saxon"
 xmlns:nums="f:nums" exclude-result-prefixes="nums vendor">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <nums:nums>
   <One/>
   <Two/>
   <Three/>
 </nums:nums>

   <xsl:template match="/">
     <xsl:variable name="vPass1">
       <xsl:apply-templates/>
     </xsl:variable>

     <Categories>
       <xsl:apply-templates mode="pass2"
       select="vendor:node-set($vPass1)/*
                            [string-length(@Code) = 1]"/>
     </Categories>
   </xsl:template>

   <xsl:template match="Categories">
    <xsl:apply-templates select="Category">
      <xsl:sort select="Code"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="Category">
    <xsl:variable name="vNums" select="document('')/*/nums:*"/>

    <xsl:element
name="Level{name($vNums/*[string-length(current()/Code)])}Category">
      <xsl:attribute name="Code">
        <xsl:value-of select="Code"/>
      </xsl:attribute>
      <xsl:attribute name="Description">
        <xsl:value-of select="Description"/>
      </xsl:attribute>
    </xsl:element>
  </xsl:template>

  <xsl:template match="*" mode="pass2">
    <xsl:variable name="vcurLevel" select="string-length(@Code)"/>

    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates mode="pass2"
       select="following-sibling::*[string-length(@Code)
                                   =
                                    $vcurLevel + 1
                                  and
                                    generate-id(current())
                                   =
                                    generate-id(preceding-sibling::*
                                                  [string-length(@Code)
                                                  =
                                                   $vcurLevel
                                                   ][1]
                                                )
                                    ]"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

When applied to your source.xml:

<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>

the above transformation produces the wanted result:

<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>


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

"Jim Han" <jhan@xxxxxxxxxxxxxxxx> wrote in message
news:DFA25A23407A8143B9070FD0F3AFF51C0291DCCB@xxxxxxxxxxxxxxxxxxxxxxxxx
> 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
>
>




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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.