Subject: RE: [xsl] xml to xml transform From: Shimon Pozin <spozin@xxxxxxxxxxxxx> Date: Tue, 16 Jan 2001 13:46:22 -0500 |
Thanks, Mike, for the clue. However, when I tested this solution, I paid attention that if the source xml is slightly different: <rows> <row fld="f1" value="v1"/> <row fld="f1" value="v2"/> <row fld="f2" value="v3"/> <row fld="f1" value="v2"/> <row fld="f3" value="v4"/> </rows> then this solution will not work since f1 comes after f2 and in this case I'll see two elements f1 rather than one. Should I sort the entire collection before I use this solution? Can I use sorting capabilities of apply-templates for that? Thanks again! Shimon > -----Original Message----- > From: Mike Brown [mailto:mike@xxxxxxxx] > Sent: Monday, January 15, 2001 10:54 PM > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx > Cc: spozin@xxxxxxxxxxxxx > Subject: Re: [xsl] xml to xml transform > > > Shimon Pozin wrote: > > How can I transform document: > > <row @fld="f1" value="v1"> > > <row @fld="f1" value="v2"> > > <row @fld="f2" value="v3"> > > <row @fld="f3" value="v4"> > > This isn't even XML. > I will answer your question based on the assumption that you meant: > > <rows> > <row fld="f1" value="v1"/> > <row fld="f1" value="v2"/> > <row fld="f2" value="v3"/> > <row fld="f3" value="v4"/> > </rows> > > > to > > > > <f1> > > <v1/> > > <v2/> > > </f1> > > <f2> > > <v3/> > > </f2> > > <f3> > > <v4/> > > </f3> > > > > if I don't know values f1, f2, etc. in advance? > > This is a fun (fun in a pathetically geeky way) variation on > the grouping > FAQ. There is a design pattern for this. In your case it is along the > lines of the following: > > 1. id a node-set representing the unique values from all the @fld > attributes (one f1, one f2, one f3...). create an element with > a name that is the same as the value. in the content of that > element... > 2. iterate through that set, finding the rest of the @fld > values that > have the current value (all f1s, all f2s, all f3s...). > There's your > group. All you have to do is... > 3. Iterate through those (or in this case, through the corresponding > @value attributes) and create elements named for their values. > > <?xml version="1.0" encoding="utf-8"?> > <xsl:stylesheet > xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> > > <xsl:output method="xml" indent="yes"/> > > <xsl:template match="rows"> > <xsl:for-each select="row[not(@fld = > preceding-sibling::row/@fld)]"> > <xsl:element name="{@fld}"> > <xsl:for-each select="../row[@fld = current()/@fld]"> > <xsl:element name="{@value}"/> > </xsl:for-each> > </xsl:element> > </xsl:for-each> > </xsl:template> > > </xsl:stylesheet> > > - Mike > ____________________________________________________________________ > Mike J. Brown, software engineer at My XML/XSL resources: > webb.net in Denver, Colorado, USA http://skew.org/xml/ > > > 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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] xml to xml transform, Jeni Tennison | Thread | Re: [xsl] xml to xml transform, Mike Brown |
RE: [xsl] From fo:flow to fo:region, Sebastian Rahtz | Date | Re: [xsl] the nearest ancestor with, Jeni Tennison |
Month |