Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: How to select particular items from repeating 'generic' attributes? [Thread Next] Re: How to select particular items from repeating 'generic' attributes?To: NULL Date: 2/7/2006 12:24:00 AM In article <44q19bF35d53U1@i...>, peter.nosp@m... says... > 42 wrote: > > Hi, > > I'm pretty new to XSL, and I've bumped into what is probably (hopefully) > > a simple problem. > > > > I need to convert a simple xml document containing just repeated > > 'generic' data blocks of: > > > > <record> > > <data>Bob</data> > > <data>Smith</data> > > ... > > </record> > > > > to: > > > > <person> > > <firstname>Bob</firstname> > > <lastname>Smith</lastname> > > ... > > </person> > > > > The source xml contains the same number of <data> elements for each > > record, are in a consistent order, with no omissions. > > > > I've figured out how to use foreach to iterate over the person records, > > Don't. Use templates instead, it's faster and clearer. > > <xsl:template match="record"> > <person> > <xsl:apply-templates/> > </person> > </xsl:template> > > <xsl:template match="data[1]"> > <firstname> > <xsl:apply-templates/> > </firstname> > </xsl:template> > > <xsl:template match="data[2]"> > <surname> > <xsl:apply-templates/> > </surname> > </xsl:template> > > Keep for-each for things like sorting and accessing nodes out of > document order. > > > but can't see how to select the nth occurence of <data> within each > > record. > > Think like a tree, not like a chainsaw :-) Thanks much... and for the templates advice too. The real stumbler was that I didn't know about the 'indexing' syntax -- [x]; and since I didn't know it existed, didn't have a clue what to search for - everything I searched for on terms like repeating elements etc just got me into recursive templates, numbering, counting, sorting. That tidbit: data[1] & data[2] -- turned it from hard to trivial in nothing flat. :) Thanks to both responders. -regards, Dave | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
