title: XSLT custom function returning nodeset or XML fragment (not simple datatype)
body: <p>I am trying to develop an XSLT custom function that could return node set or an XML fragment, let's say something like:</p>
  
  <p>Input document:</p>
  
  <pre><code>&lt;root&gt;
  &lt;!--
   author: blablabla
   usage: more blablabla
   labelC: [in=2] &lt;b&gt;formatted&lt;/b&gt; blablabla
  --&gt;
  &lt;tag1 name="first"&gt;
      &lt;tag2&gt;content a&lt;/tag2&gt;
      &lt;tag2&gt;content b&lt;/tag2&gt;
      &lt;tag3 attrib="val"&gt;content c&lt;/tag3&gt;
  &lt;/tag1&gt;
  
  &lt;!--
   author: blebleble
   usage: more blebleble
   labelC: blebleble
  --&gt;
  &lt;tag1 name="second"&gt;
      &lt;tag2&gt;content x&lt;/tag2&gt;
      &lt;tag2&gt;content y&lt;/tag2&gt;
      &lt;tag3 attrib="val"&gt;content z&lt;/tag3&gt;
  &lt;/tag1&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>So that an XSLT template such as: </p>
  
  <pre><code>    &lt;xsl:template match="//tag1/preceding::comment()[1]" xmlns:d="java:com.dummy.func"&gt;
      &lt;section&gt;
       &lt;para&gt;
        &lt;xsl:value-of select="d:genDoc(.)"/&gt;
       &lt;/para&gt;
      &lt;/section&gt;
      &lt;/xsl:template&gt;
  </code></pre>
  
  <p>Would produce:</p>
  
  <pre><code>    &lt;section&gt;
       &lt;para&gt;
        &lt;author&gt;blablabla&lt;/author&gt;
        &lt;usage&gt;more blablabla&lt;/usage&gt;
        &lt;labelC in="2"&gt;&lt;b&gt;formatted&lt;/b&gt; blablabla&lt;/labelC&gt;
       &lt;/para&gt;
      &lt;/section&gt;
  </code></pre>
  
  <p>When matched on the first occurrence of tag1
  and</p>
  
  <pre><code>    &lt;section&gt;
       &lt;para&gt;
        &lt;author&gt;blebleble&lt;/author&gt;
        &lt;usage&gt;more blebleble&lt;/usage&gt;
        &lt;labelC&gt;blebleble&lt;/labelC&gt;
       &lt;/para&gt;
      &lt;/section&gt;
  </code></pre>
  
  <p>When matched on the second occurrence.</p>
  
  <p><strong>Basically what I want to achieve with this custom function is to parse some meta-data present in the comments and use it to generate XML.</strong></p>
  
  <p>I found some examples online, one at:
  <a href="http://cafeconleche.org/books/xmljava/chapters/ch17s03.html" rel="nofollow">http://cafeconleche.org/books/xmljava/chapters/ch17s03.html</a></p>
  
  <p>According to the example, my function should return one of the following</p>
  
  <pre><code>org.w3c.dom.traversal.NodeIterator,
  org.apache.xml.dtm.DTM,
  org.apache.xml.dtm.DTMAxisIterator,
  org.apache.xml.dtm.DTMIterator,
  org.w3c.dom.Node and its subtypes (Element, Attr, etc),
  org.w3c.dom.DocumentFragment
  </code></pre>
  
  <p>I was able to implement a function returning the XML as simple type String.
  This, however poses several other problems: the main being the markers characters get escaped when inserted in the original XML.</p>
  
  <p>Does anybody have an example of how to implement such function?
  I am mostly interested in how to return a proper XML node set to the calling template.</p>