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

RE: can restructure xml??

Subject: RE: can restructure xml??
From: Jarno.Elovirta@xxxxxxxxx
Date: Wed, 15 Jan 2003 15:00:47 +0200
motom template
Hi,

Yes, but hard coding the structure like that is evil. I don't know what's the best practice on these kinds of grouping problems nowadays (i.e. how to use xsl:key etc.), but I would recommend something like this:

<xsl:key name="title" match="list/*[not(self::title)]" use="generate-id(preceding-sibling::title[1])" />

<xsl:template match="list">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates select="title" mode="group"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="title" mode="group">
  <item>
    <xsl:attribute name="num">
      <!-- you could use position() here, too -->
      <xsl:number count="title"/>
    </xsl:attribute>
    <xsl:apply-templates select=".|key('title', generate-id())"/>
  </item>  
</xsl:template>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

Cheers,

Jarno - Feindflug: Vollstreckung

> -----Original Message-----
> From: ext Laura [mailto:xsl_list@xxxxxxxxxxx]
> Sent: 15 January, 2003 14:28
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Re:  can restructure xml??
> 
> 
> You could do simple things like
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>     <xsl:output omit-xml-declaration="yes" indent="yes" method="xml"/>
>     <xsl:template match="/">
>         <root>
>             <list>
>                    <xsl:for-each select="/root/list/title">
>                     <item num = "{position()}">
>                         <title>
>                             <xsl:value-of select="."/>
>                         </title>
>                         <writer>
>                             <xsl:value-of
> select="following-sibling::writer"/>
>                         </writer>
>                         <type>
>                             <xsl:value-of 
> select="following-sibling::type"/>
>                         </type>
>                         <remark>
>                             <xsl:value-of
> select="following-sibling::remark"/>
>                         </remark>
>                     </item>
>                 </xsl:for-each>
>             </list>
>         </root>
>     </xsl:template>
> </xsl:stylesheet>
> this gets first writer,type and remark elements after each 
> title element and
> puts them under the item element. The number attribute takes 
> the value of
> the position() function. so the number of <item> elements you 
> would have
> will be the number of  <title> element you have.
> But as Jarno suggested, you could as well modify the code 
> that generates the
> XML to produce the right kind of structure
> Hope this helps
> Vasu
> -- Original Message -----
> From: "a847356549/mail.h7.dion.ne.jp" <motom@xxxxxxxxxxxxx>
> To: <XSL-List@xxxxxxxxxxxxxxxxxxxxxx>
> Sent: Wednesday, January 15, 2003 11:32 AM
> Subject:  can restructure xml??
> 
> 
> > hello.
> > I just started putting my data into xml.
> > now I only have a xml like ---
> >
> > <root>
> >   <list>
> >     <title>aaa</title>
> >     <writer>bbb</writer>
> >     <type>ccc</type>
> >     <remark>ddd</remark>
> >     <title>eee</title>
> >     <writer>fff</writer>
> >     <type>ggg</type>
> >     <remark>hhh</remark>
> >     ...
> >   </cd>
> > </list>
> >
> > my ideal structure is like ---
> >
> > <root>
> >   <list>
> >    <item num="1">
> >     <title>aaa</title>
> >     <writer>bbb</writer>
> >     <type>ccc</type>
> >     <remark>ddd</remark>
> >    </item>
> >    <item num="2">
> >     <title>eee</title>
> >     <writer>fff</writer>
> >     <type>ggg</type>
> >     <remark>hhh</remark>
> >    </item>
> >     ...
> >   </list>
> > </root>
> >
> > is there a way to do this throgh xsl?  any advice and hint will
> >  be grateful.
> >
> > ttkaya
> >
> >  XSL-List info and archive:  
http://www.mulberrytech.com/xsl/xsl-list
>

 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.