Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: XSLT 2.0 question: wrapping in an arbitrary number of elements

From: Martin Honnen <mahotrash@-----.-->
To: 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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent