This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Converting attributes to elements and preserving the hierarchy


Hip hei!

> 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

You're processing all nfield elements separately. You could try e.g.

<xsl:template match="/">
  <xsl:element name="{/template/header/tmplname}">
    <!-- process the nfields that don't have an nfield parent -->
    <xsl:apply-templates
select="/template/format//nfield[not(parent::nfield)]" />
  </xsl:element>
</xsl:template>

<xsl:template match="nfield">
  <xsl:element name="{@fldname}">
    <xsl:value-of select="@reserve2"/>
    <!-- you have to process the containing nfields here -->
    <xsl:apply-templates select="nfield" />
  </xsl:element>
</xsl:template>

You didn't show the whole source document, so
/template/format//nfield[not(parent::nfield)] might not apply to the real
source, but I hope you get the idea.

Hope this helps,

Santtu

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]