Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: How to select particular items from repeating 'generic' attributes?

From: 42 <nospam@------.--->
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


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent