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 17:08:55 -0000 |
On Sat, Nov 5, 2016 at 10:01 AM, Eliot Kimber ekimber@xxxxxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote: > That's very clever. I'm not sure it's clearer than my current recursive > solution though :-) Why, this is non-recursive, 10-lines transformation that contains just two XPath expressions, not counting the tokenization -- the template match pattern and the indexing of the tokens to get the current element-to-construct name. Also, as an added benefit, one could experiment with the used XML document's depth, to reveal what the limits of their XSLT processor/XML-parser are :) Cheers, Dimitre > > Cheers, > > E. > -- > Eliot Kimber > http://contrext.com > > > > > > > > On 11/5/16, 11:49 AM, "Dimitre Novatchev dnovatchev@xxxxxxxxx" > <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote: > >>I'd suggest this non-recursive solution: >> >><xsl:stylesheet version="2.0" >>xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> >> <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:element> >> </xsl:template> >> >> <xsl:template match="text()"/> >></xsl:stylesheet> >> >> >>When applied on: >> >><x1> >> <x2> >> <x3> >> <x4> >> <x5> >> <x6> >> <x7> >> <x8> >> <x9> >> <x10> >> <x11> >> <x12> >> <x13> >> <x14> >> <x15> >> <x16> >> <x17> >> <x18> >> <x19> >> <x20> >> </x20> >> </x19> >> </x18> >> </x17> >> </x16> >> </x15> >> </x14> >> </x13> >> </x12> >> </x11> >> </x10> >> </x9> >> </x8> >> </x7> >> </x6> >> </x5> >> </x4> >> </x3> >> </x2> >></x1> >> >>the result is: >> >><foo> >> <bar> >> <baz> >> <bam> >> <bak> >> <bap> >> <bah> >> <baw/> >> </bah> >> </bap> >> </bak> >> </bam> >> </baz> >> </bar> >></foo> >> >> >>Cheers, >>Dimitre >> >>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 >>> 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. >> >> > -- 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.
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] XSLT 2: Best Way to Synth, Eliot Kimber ekimber | Thread | Re: [xsl] XSLT 2: Best Way to Synth, G. Ken Holman g.ken. |
Re: [xsl] XSLT 2: Best Way to Synth, Eliot Kimber ekimber | Date | Re: [xsl] XSLT 2: Best Way to Synth, G. Ken Holman g.ken. |
Month |