Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - xslt question: change one xml schema to another ? >Thread Next - Re: xslt question: change one xml schema to another ? Re: xslt question: change one xml schema to another ?To: NULL Date: 3/2/2006 8:31:00 PM parth wrote: > I have the following XML file - > > <root> > <book> > <section>art</section> > <title>abc</title> > <author>mark</author> > </book> > I want to convert the above schema to look like the following schema > <root> > <section> > <name>art</name> > <book> > <title>abc</title> > <author>mark</author> > </book> > Can this be done using XSLT? Yes, a stylesheet alike <?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:key name="books-by-section" match="book" use="section" /> <xsl:template match="root"> <xsl:copy> <xsl:copy-of select="@*" /> <xsl:apply-templates select="book[generate-id() = generate-id(key('books-by-section', section)[1])]" mode="group" /> </xsl:copy> </xsl:template> <xsl:template match="book" mode="group"> <section> <name><xsl:value-of select="section" /></name> <xsl:apply-templates select="key('books-by-section', section)" /> </section> </xsl:template> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()" /> </xsl:copy> </xsl:template> <xsl:template match="section" /> </xsl:stylesheet> manages that. Note that you have not shown any schema (like an XSD schema or RelaxNG schema) but simply two instance XML documents. And the XSLT takes one instance and transforms to the other instance. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
