Re: [xsl] XSLT 2: Best Way to Synthesize Elements Given List of Tagnames

Subject: Re: [xsl] XSLT 2: Best Way to Synthesize Elements Given List of Tagnames
From: "Michael Kay mike@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 5 Nov 2016 18:21:27 -0000
In 3.0, apart from xsl:iterate as suggested by John Lumley, you could consider
using a fold:

<xsl:function name="f:wrap" as="element(*)">
  <xsl:param name="e" as="element(*)"/>
  <xsl:param name="n" as="xs:string"/>
  <xsl:element name="{$n}"><xsl:copy-of select="$e"/></xsl:element>
</xsl:function>

then

fold-left(reverse(tokenize($path, '/')), $stuff-from-somewhere-else,
f:wrap#2)

Michael Kay
Saxonica

> On 5 Nov 2016, at 16:19, Eliot Kimber ekimber@xxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Say I have this string: "foo/bar" (or any arbitrarily-long sequence of
> /-delimited tag names) and want to construct from it:
>
> <foo>
> <bar>
>   <!-- Stuff added here that I get from somewhere else -->
> </bar>
> </foo>
>
> Is there an easier or more obvious way to generate this than an recursive
> function?
>
> With a recursive function I can easily create child elements until the
> sequence is exhausted but it feels like there should be an easier way
> using XSLT 2 but if there is I'm not thinking of it.
>
> Cheers,
>
> Eliot
>
> --
> Eliot Kimber
> http://contrext.com

Current Thread