xsl-list
[Top] [All Lists]

RE: XML to XML

2003-03-26 14:49:19
Untested but try the following approach

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

<xsl:template match="Category">
        <xsl:variable name="code" select="string(Code)"/>
        <xsl:variable name="code-length" select="length(string(Code))"/>
        <xsl:variable name="name">
                <xsl:choose>
                        <xsl:when 
test="length(string(Code))=1">LevelOneCategory</xsl:when>
                        ...etc
                </xsl:choose>
        </xsl:variable>
        <xsl:element name="{$name}" Code="{$code}" Description="{Description}">
                <xsl:apply-templates 
select="following-sibling::Category[substring(string(Code),1,$code-length)=string($code)]"
 />
        </xsl:element>
</xsl:template>

following-sibling:: could be replaced with ../ if the order is not guaranteed.

HTH,

Ken Ross
Ph: +61 7 32359370
Mob: +61 (0)419 772299
Email: Ken(_dot_)Ross(_at_)iie(_dot_)qld(_dot_)gov(_dot_)au


-----Original Message-----
From: Jim Han [mailto:jhan(_at_)horizonhobby(_dot_)com]
Sent: Thursday, 27 March 2003 6:25 AM
To: XSL-List(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] XML to XML


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

--------------------------------------------------------------------------------------------------------------------------
IMPORTANT: This email (including any attachments) may contain confidential,
private or legally privileged information and may be protected by copyright. 
You may
only use it if you are the person(s) it was intended to be sent to and if you 
use it in an
authorised way. No one is allowed to use, review, alter, transmit, disclose, 
distribute,
print or copy this e-mail without appropriate authority.
 
If this e-mail was not intended for you and was sent to you by mistake, please
telephone or e-mail me immediately, destroy any hard copies of this e-mail and 
delete
it and any copies of it from your computer system. Any legal privilege and 
confidentiality attached to this e-mail is not waived or destroyed by that 
mistake.
 
It is your responsibility to ensure that this e-mail does not contain and is 
not affected
by computer viruses, defects or interference by third parties or replication 
problems
(including incompatibility with your computer system).




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



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