Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - XSL newbie [Thread Next] Re: XSL newbieTo: NULL Date: 4/1/2005 3:48:00 PM Larry wrote: > Unfortunately I'm still a newbie and would need you guys to tell me how > to go about parsing this file: > > <?xml version="1.0" encoding="ISO-8859-1"?> > <collection> > <cd> > <title>Fight for your mind</title> > <artist>Ben Harper</artist> > <year>1995</year> > </cd> > <cd> > <title>Electric Ladyland</title> > <artist>Jimi Hendrix</artist> > <year>1997</year> > </cd> > <cd> > <title>Rubber Soul</title> > <artist>The Beatles</artist> > <year>1965</year> > </cd> > <cd> > <title>Revolver</title> > <artist>The Beatles</artist> > <year>1966</year> > </cd> > <cd> > <title>White Album</title> > <artist>The Beatles</artist> > <year>1966</year> > </cd> > </collection> > > I'd like (by using XSLT) to have a response xml file like this: > > <collection> > <cd> > <title>Revolver</title> > <artist>The Beatles</artist> > <year>1966</year> > </cd> > <cd> > <title>White Album</title> > <artist>The Beatles</artist> > <year>1966</year> > </cd> > </collection> > If you want to transform XML to XML it is often useful to start with the identity transformation and then write templates for those element that need special treatment, in your case for the <collection> element you want to only process those <cd> elements with a certain condition: <?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" encoding="UTF-8" indent="yes" /> <xsl:param name="artist" select="'The Beatles'" /> <xsl:param name="year" select="1966" /> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node() " /> </xsl:copy> </xsl:template> <xsl:template match="collection"> <xsl:copy> <xsl:apply-templates select="cd[artist = $artist and year = $year]" /> </xsl:copy> </xsl:template> </xsl:stylesheet> The values for the condition have been put into a global parameter so that you could pass them to the XSLT processor before running a transformation. -- Martin Honnen http://JavaScript.FAQTs.com/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
