Re: [xsl] Generating implicit wrapper element

Subject: Re: [xsl] Generating implicit wrapper element
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Wed, 31 Aug 2005 13:22:57 +0530
Hi,
  I posted this stylesheet yesterday on xml-dev list..

<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">

<xsl:output method="xml" indent="yes" />

<xsl:template match="/root">
   <root>
    <xsl:for-each select="*">
      <xsl:choose>
        <xsl:when test="self::par">
         <p><xsl:value-of select="." /></p>
        </xsl:when>
        <xsl:otherwise>
          <xsl:if test="(name(preceding-sibling::*[1])
= 'par') or (position() = 1)">
            <ul>
              <xsl:call-template name="makegroup">

                <xsl:with-param name="nodeset"
select="self::* | following-sibling::*" />
              </xsl:call-template>
            </ul>
          </xsl:if>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
   </root>
</xsl:template>

<xsl:template name="makegroup">
   <xsl:param name="nodeset" />

   <xsl:if test="name($nodeset[1]) = 'li'">
     <xsl:copy-of select="$nodeset[1]" />
     <xsl:call-template name="makegroup">

       <xsl:with-param name="nodeset"
select="$nodeset[position() &gt; 1]" />
     </xsl:call-template>
   </xsl:if>

</xsl:template>

</xsl:stylesheet>

For e.g. when it is applied to XML
<root>
  <par>my first para</par>
  <par>second para</par>
  <li>first list item of first list</li>
  <li>second list item of first list</li>
  <li>third list item of first list</li>
  <par>third para</par>
  <li>first list item of second list</li>
  <par>fourth para</par>
  <li>first list item of third list</li>
  <li>second list item of third list</li>
</root>

The output produced is
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <p>my first para</p>
  <p>second para</p>
  <ul>
    <li>first list item of first list</li>
    <li>second list item of first list</li>
    <li>third list item of first list</li>
  </ul>
  <p>third para</p>
  <ul>
    <li>first list item of second list</li>
  </ul>
  <p>fourth para</p>
  <ul>
    <li>first list item of third list</li>
    <li>second list item of third list</li>
  </ul>
</root>

Regards,
Mukul

On 8/30/05, Ferdinand Soethe <xsl-list@xxxxxxxxxx> wrote:
>
> I have a question about the problem below which has already been
> identified as a 'positional grouping problem'. (I think) I understand
> the Muenchian method-based solutions suggested (at least in theory).
>
> What I don't understand is what is wrong about using the following simple
> solution that I pieced together from different postings:
>
> <xsl:template match="li">
>    <xsl:if test="(name(preceding-sibling::*[1])!='li' )">
>        <xsl:text disable-output-escaping="yes">&lt;ul&gt;</xsl:text>
>    </xsl:if>
>    <li><xsl:copy-of select="@*"/><xsl:apply-templates/></li>
>    <xsl:if test="(name(following-sibling::*[1])!='li' )">
>        <xsl:text disable-output-escaping="yes">&lt;/ul&gt;</xsl:text>
>    </xsl:if>
>  </xsl:template>
>
> Seems like it fits much better into my context of using
> 'apply-templates' as much as possible (no need to process all li's at
> once and ignore them later) and - except for that hack of
> smuggling half the ul-element into my output - it also seems quite
> reasonable xsl, not?
>
> Your input much appreciated,
>
> --
> Ferdinand Soethe
>
>
> --- The problem ---
>
> I have an XML-document with paragraphs and list items that have no
> wrapper element around each list.
>
> Something like this:
>
> <par>my first para</par>
> <par>second para</par>
> <li>first list item of first list</li>
> <li>second list item of first list</li>
> <li>third list item of first list</li>
> <par>third para</par>
> <li>first list item of second list</li>
> <par>fourth para</par>
> <li>first list item of third list</li>
> <li>second list item of third list</li>
>
> In my transformation I would like to add these implicit wrapper
> element around each of the list to get something like
>
> <p>my first para</p>
> <p>second para</p>
> <ul>
> <li>first list item of first list</li>
> <li>second list item of first list</li>
> <li>third list item of first list</li>
> </ul>
> <p>third para</p>
> <ul>
> <li>first list item of second list</li>
> </ul>
> <p>fourth para</p>
> <ul>
> <li>first list item of third list</li>
> <li>second list item of third list</li>
> </ul>

Current Thread