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: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 5 Nov 2016 16:49:49 -0000
I'd suggest this non-recursive solution:

<xsl:stylesheet version="2.0" xmlns:xsl="";>
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:param name="pPath" select="'/foo/bar/baz/bam/bak/bap/bah/baw'"/>

 <xsl:variable name="vNames" select="tokenize($pPath, '/')[.]"/>

  <xsl:template match="*[count(current()/ancestor::*) lt count($vNames)]">
    <xsl:element name="{$vNames[count(current()/ancestor::node())]}">
      <xsl:apply-templates select="*"/>

  <xsl:template match="text()"/>

When applied on:


the result is:



On Sat, Nov 5, 2016 at 9:18 AM, 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

Dimitre Novatchev
Truly great madness cannot be achieved without significant intelligence.
To invent, you need a good imagination and a pile of junk
Never fight an inanimate object
To avoid situations in which you might make mistakes may be the
biggest mistake of all
Quality means doing it right when no one is looking.
You've achieved success in your field when you don't know whether what
you're doing is work or play
To achieve the impossible dream, try going to sleep.
Facts do not cease to exist because they are ignored.
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
Sanity is madness put to good use.
I finally figured out the only reason to be alive is to enjoy it.

Current Thread