[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: XSLT 2: Best Way to Synthesize Elements Given List
> fold-left(reverse(tokenize($path, '/')), $stuff-from-somewhere-else, f:wrap#2) While I am all for using FP, this is the most unreadable of all proposed solutions. Eliot specifically asked for XSLT 2 solution. The 10-lines non-recursive, based on practical limitations XSLT 2.0 solution, compared to the XSLT 3.0 solution using fold-left() (or <xsl:iterate>) is similar to the Indian space ship that reached Mars for less than it cost to make "Gravity": http://www.theverge.com/2014/9/24/6837745/india-spacecraft-reaches-mars-orbit-less-than-gravity :) Cheers, Dimitre On Sat, Nov 5, 2016 at 11:21 AM, Michael Kay mike@xxxxxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote: > 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 >> >> >> >> >> > -- Cheers, 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.
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|