xsl-list
[Top] [All Lists]

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

2016-11-05 13:22:09
Surely this doesn't satisy Eliot's requirement that the path can be 
"arbitrarily long"?

Michael Kay


Yes, as well as the fact that probably every XML parser has a limit
for the maximum depth of an XML document that it can process.

Knowing this limit, one needs to make a more precise and realistic requirement.


Cheers,
Dimitre

On Sat, Nov 5, 2016 at 11:13 AM, Michael Kay mike(_at_)saxonica(_dot_)com
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:
Surely this doesn't satisy Eliot's requirement that the path can be 
"arbitrarily long"?

Michael Kay
Saxonica

On 5 Nov 2016, at 16:50, Dimitre Novatchev dnovatchev(_at_)gmail(_dot_)com 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> 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(_at_)contrext(_dot_)com
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> 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.
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

<Prev in Thread] Current Thread [Next in Thread>