[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Converting attributes to elements and preserving the hierarchy


Subject: RE: [xsl] Converting attributes to elements and preserving the hierarchy
From: "Chris Bayes" <chris@xxxxxxxxxxx>
Date: Wed, 19 Dec 2001 08:44:27 -0000

Beginners rule #1 Never use for-each. By using for-each here you are
flattening you data. Use a template instead

	<xsl:template match="nfield">
		<xsl:element name="{@fldname}">
			 <xsl:apply-templates />
             <xsl:value-of select="@reserve2"/>
           </xsl:element>
	</xsl:template>

Ciao Chris


XML/XSL Portal
http://www.bayes.co.uk/xml


> -----Original Message-----
> From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx 
> [mailto:owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of 
> Samuel Abraham
> Sent: 19 December 2001 07:56
> To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Converting attributes to elements and 
> preserving the hierarchy
> 
> 
> Hi all,
> I am trying to transform an xml document into a resultant xml 
> where the attributes of the source are converted to elements 
> and one of the attributes as the value of the node.(Using 
> Xalan ). eg for source XML
> :
>       <nfield fldname="DETAILS" >
>       <nfield fldname="Creation_Date"  reserve2="20122001"/>
>       <nfield fldname="REPEATER_OUTER">
>         <nfield fldname="Address1"reserve2="SHENTON WAY"/>
>           <nfield fldname="REPEATER" length="0" fldtype="F">
>                <nfield fldname="Rate" reserve2="25"/>
>      </nfield>
>         </nfield>
>       </nfield>
> I am able to do the transformation but the result doent have 
> the child parent realtionship.
> 
> I used the template
>   <xsl:template match="/">
>     <xsl:element name="{/template/header/tmplname}">
>       <xsl:for-each select="/template/format//nfield">
>            <xsl:element name="{@fldname}">
>              <xsl:value-of select="@reserve2"/>
>            </xsl:element>
>       </xsl:for-each>
>     </xsl:element>
>   </xsl:template>
> 
> The result was something like this
> <DETAILS/>
> <Creation_Date>20122001</Creation_Date>
> <REPEATER_OUTER/>
>  <Address1>SHENTON WAY</Address1>
> <REPEATER/>
> <RATE>25</RATE>
> 
> Actually I need to get
>   <DETAILS>
>      <Creation_Date>20122001</Creation_Date>
>      <REPEATER_OUTER>
>           <Address1>SHENTON WAY</Address1>
>           <REPEATER>
>                <RATE>25</RATE>
>           </REPEATER>
>      </REPEATER_OUTER>
>  </DETAILS>
> 
> Where am I going wrong?Can someone please point out the 
> problem..... thanks  Samuel Abraham
> 
> 
> 
> --
> 
> This e-mail may contain confidential and/or privileged 
> information. If you are not the intended recipient (or have 
> received this e-mail in error) please notify the sender 
> immediately and destroy this e-mail. Any unauthorized 
> copying, disclosure or distribution of the material in this 
> e-mail is strictly forbidden.
> 
> 
> 
>  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
Keywords
xml