Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Re: XSLT 2.0 question: wrapping in an arbitrary number of elements [Thread Next] Re: XSLT 2.0 question: wrapping in an arbitrary number of elementsTo: NULL Date: 3/9/2009 2:55:00 PM On Mar 9, 12:53=A0pm, Martin Honnen <mahotr...@yahoo.de> wrote: > Stryder wrote: > > Hi. =A0I'm writing XSLT 2.0 templates and am trying to do the following > > without writing extension functions. =A0I'm trying to go from this... > > > <outer> > > =A0 =A0 <wrapin>one two three</wrapin> > > =A0 =A0 <wrapme>some text</wrapme> > > </outer> > > > to this... > > > <outer> > > =A0 =A0 <one> > > =A0 =A0 =A0 =A0 <two> > > =A0 =A0 =A0 =A0 =A0 =A0 <three>some text</three> > > =A0 =A0 =A0 =A0 </two> > > =A0 =A0 </one> > > </outer> > > > wrapping the text in <wrapme> in an arbitrary number of elements based > > on and named after the text nodes under <wrapin>. =A0I can't figure out > > a way to do this. =A0I'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. =A0Any help would > > be appreciated. > > Here is a solution using a recursive function: > > <xsl:stylesheet > =A0 =A0xmlns:xsl=3D"http://www.w3.org/1999/XSL/Transform" > =A0 =A0xmlns:xsd=3D"http://www.w3.org/2001/XMLSchema" > =A0 =A0xmlns:mh=3D"http://example.com/2009/mh" > =A0 =A0exclude-result-prefixes=3D"mh xsd" > =A0 =A0version=3D"2.0"> > > =A0 =A0<xsl:output method=3D"xml" indent=3D"yes"/> > > =A0 =A0<xsl:template match=3D"outer"> > =A0 =A0 =A0<xsl:copy> > =A0 =A0 =A0 =A0<xsl:apply-templates select=3D"wrapin"/> > =A0 =A0 =A0</xsl:copy> > =A0 =A0</xsl:template> > > =A0 =A0<xsl:function name=3D"mh:wrap" as=3D"element()*"> > =A0 =A0 =A0<xsl:param name=3D"el-names" as=3D"xsd:string*"/> > =A0 =A0 =A0<xsl:param name=3D"text" as=3D"xsd:string"/> > =A0 =A0 =A0<xsl:if test=3D"exists($el-names[1])"> > =A0 =A0 =A0 =A0 =A0<xsl:element name=3D"{$el-names[1]}"> > =A0 =A0 =A0 =A0 =A0 =A0<xsl:choose> > =A0 =A0 =A0 =A0 =A0 =A0 =A0<xsl:when test=3D"not(exists($el-names[2]))"> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0<xsl:value-of select=3D"$text"/> > =A0 =A0 =A0 =A0 =A0 =A0 =A0</xsl:when> > =A0 =A0 =A0 =A0 =A0 =A0 =A0<xsl:otherwise> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0<xsl:sequence select=3D"mh:wrap($el-names[= position() gt 1], > $text)"/> > =A0 =A0 =A0 =A0 =A0 =A0 =A0</xsl:otherwise> > =A0 =A0 =A0 =A0 =A0 =A0</xsl:choose> > =A0 =A0 =A0 =A0 =A0</xsl:element> > =A0 =A0 =A0</xsl:if> > =A0 =A0</xsl:function> > > =A0 =A0<xsl:template match=3D"wrapin"> > =A0 =A0 =A0<xsl:sequence select=3D"mh:wrap(tokenize(., '\s+'), > data(following-sibling::wrapme[1]))"/> > =A0 =A0</xsl:template> > > </xsl:stylesheet> > > -- > > =A0 =A0 =A0 =A0 Martin Honnen > =A0 =A0 =A0 =A0http://JavaScript.FAQTs.com/ Thanks that's just what I needed. | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
