Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - XSLT 2.0 question: wrapping in an arbitrary number of elements >Thread Next - Re: XSLT 2.0 question: wrapping in an arbitrary number of elements Re: XSLT 2.0 question: wrapping in an arbitrary number of elementsTo: NULL Date: 3/9/2009 6:54:00 PM Stryder wrote: > Hi. I'm writing XSLT 2.0 templates and am trying to do the following > without writing extension functions. I'm trying to go from this... > > <outer> > <wrapin>one two three</wrapin> > <wrapme>some text</wrapme> > </outer> > > to this... > > <outer> > <one> > <two> > <three>some text</three> > </two> > </one> > </outer> > > wrapping the text in <wrapme> in an arbitrary number of elements based > on and named after the text nodes under <wrapin>. I can't figure out > a way to do this. I'm getting fairly well versed in XSLT 2.0, > including using regular expressions and the new grouping elements/ > functions but I haven't figured out a way to do this. Any help would > be appreciated. Here is a solution using a recursive function: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:mh="http://example.com/2009/mh" exclude-result-prefixes="mh xsd" version="2.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="outer"> <xsl:copy> <xsl:apply-templates select="wrapin"/> </xsl:copy> </xsl:template> <xsl:function name="mh:wrap" as="element()*"> <xsl:param name="el-names" as="xsd:string*"/> <xsl:param name="text" as="xsd:string"/> <xsl:if test="exists($el-names[1])"> <xsl:element name="{$el-names[1]}"> <xsl:choose> <xsl:when test="not(exists($el-names[2]))"> <xsl:value-of select="$text"/> </xsl:when> <xsl:otherwise> <xsl:sequence select="mh:wrap($el-names[position() gt 1], $text)"/> </xsl:otherwise> </xsl:choose> </xsl:element> </xsl:if> </xsl:function> <xsl:template match="wrapin"> <xsl:sequence select="mh:wrap(tokenize(., '\s+'), data(following-sibling::wrapme[1]))"/> </xsl:template> </xsl:stylesheet> -- Martin Honnen http://JavaScript.FAQTs.com/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
