Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - creating new element from element >Thread Next - Re: creating new element from element Re: creating new element from elementTo: NULL Date: 10/2/2008 4:05:00 PM mariolongos@g... wrote: > I've the following xml file: > > <myroot> > <element> > <FirstSecondThree> > ... some text or other children elements ... > </FirstSecondThree> > </element> > </myroot> > > Is it possibile handling elements in compound-form? (FirstSecondThree) > Is it possible by means of XSLT obtaining the xml file: > <myroot> > <element> > <Three> > <Second> > <First> > ... some text or other children elements ... > </First> > </Second> > </Three> > </element> > </myroot> Here is a sample stylesheet using XSLT 2.0: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"> <xsl:strip-space elements="*"/> <xsl:output method="xml" indent="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template name="nest-elements"> <xsl:param name="name-items" as="xs:string*"/> <xsl:choose> <xsl:when test="not(empty($name-items))"> <xsl:element name="{$name-items[1]}"> <xsl:call-template name="nest-elements"> <xsl:with-param name="name-items" select="$name-items[position() > 1]"/> </xsl:call-template> </xsl:element> </xsl:when> <xsl:otherwise> <xsl:apply-templates/> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="FirstSecondThree"> <xsl:variable name="re" select="'\p{Lu}\p{Ll}+'"/> <xsl:variable name="name-items" as="xs:string+"> <xsl:analyze-string select="local-name()" regex="{$re}"> <xsl:matching-substring> <xsl:sequence select="."/> </xsl:matching-substring> </xsl:analyze-string> </xsl:variable> <xsl:call-template name="nest-elements"> <xsl:with-param name="name-items" select="reverse($name-items)"/> </xsl:call-template> </xsl:template> </xsl:stylesheet> You can run XSLT 2.0 stylesheet with Saxon 9 (http://saxon.sourceforge.net/), AltovaXML (http://www.altova.com/altovaxml.html), and Gestalt (http://gestalt.sourceforge.net/). If you want to do the same with XSLT 1.0 then please let us know which XSLT processor you want to use as splitting up that string is a pain in pure XSLT 1.0 so if we know the XSLT processor you use then we might be able to make use of extension functions. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
