[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Generating implicit wrapper element

  • To: Ferdinand Soethe <xml-dev@s...>, xml-dev@l...
  • Subject: Re: Generating implicit wrapper element
  • From: Mukul Gandhi <mukul_gandhi@y...>
  • Date: Tue, 30 Aug 2005 07:50:22 -0700 (PDT)
  • Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:Received:Date:From:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=BGjETZBPvNNtl2JhyTxUM3y0k/MY5MtiiRFDFDfrI+UnzSUHDhbfbePii/dYNRIalQsuORHF7RyfWvdGa9jMN6qP/i5iD53eRqdoQTjp2mFkhOVaVNsoRTSoOG71lTZCdgQjAWenIeg4Atiwj21j+Z5yqVqqRA26Zvt857z8leo= ;
  • In-reply-to: <14310060524.20050830145620@s...>

Re:  Generating implicit wrapper element
(I am sorry to continue this thread on xml-dev)

Please try this XSLT stylesheet

<?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

--- Ferdinand Soethe <xml-dev@s...> wrote:

> 
> 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>
> 
> My attempt to solve this with
> 
> <xsl:template match="Aufzaehlungspunkt">
>     
>   <xsl:if test="not(preceding-sibling::li)">
>     <ul>
>   </xsl:if>
>   
>   <li><xsl:apply-templates/></li>
> 
>   <xsl:if test="not(following-sibling::li)">
>     </ul>
>   </xsl:if>
> 
> </xsl:template>
> 
> ran into two separate problems:
> 
> 1. I'm not allowed to use <ul> without the matching
> </ul> (even though
>    that is in a separate if-branch below.
> 
> 2. The preceding-sibling and following-sibling-axes
> are only true when
>    dealing with the very first and the very last
> <li> in the document.
>    So rather than meaning 'previous element is not
> <li>' the meaning
>    'there is no more previous <li>-element in this
> document'.
> 
> Any ideas how to solve these problems with XML?
> 
> Oh, btw. I saw the solution suggested some years ago
> 
> <officers>
> <xsl:for-each select='/doc/person[@er="officer"]'>
> <person><xsl:value-of select='.'/></person>
> </xsl:for-each>
> </officers>
> 
> <xsl:for-each
> select='/doc/person[not(@er="officer")]'>
> <person><xsl:value-of select='.'/></person>
> </xsl:for-each>
> 
> </perslist>
>  </xsl:template>
> 
>  but this doesn't seem to be an option since I'm not
> using the
>  procedural style of processing my document.
> 
>  Thanks for any input,
> 
>  --
>  Ferdinand
> 
> --
> Ferdinand Soethe



		
____________________________________________________
Start your day with Yahoo! - make it your home page 
http://www.yahoo.com/r/hs 
 

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.