RE: [xsl] XML to XML

Subject: RE: [xsl] XML to XML
From: "Ross Ken" <Ken.Ross@xxxxxxxxxxxxxx>
Date: Thu, 27 Mar 2003 07:49:19 +1000
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.Ross@xxxxxxxxxxxxxx


-----Original Message-----
From: Jim Han [mailto:jhan@xxxxxxxxxxxxxxxx]
Sent: Thursday, 27 March 2003 6:25 AM
To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
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


Current Thread