Re: [xsl] unordered lists from xml to html

Subject: Re: [xsl] unordered lists from xml to html
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 28 Apr 2002 21:07:34 -0400
At 2002-04-28 12:31 +1000, Ian Hord wrote:
You will see that the attributes in the para element determine whether the
paragraph should be in a bullet or not.

Adding structure where there is none is a far more difficult transform than flattening structure. The richer your XML, the easier your transform.


To get your solution, you need to algorithmically walk the structure determining siblings that are grouped together. This "breaks" the inherent tree processing of XSLT and the resulting XSLT is far more imperative than declarative. That isn't to say it cannot be done, just that the solution doesn't take advantage of the inherent XSLT processing model.

A solution is below. Note the power of the use of multiple predicates in an XPath expression and how the order of the predicates is critical to the answer.

I hope this helps.

................... Ken

t:\ftemp>type ian.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<summary>
<para bullet="1">This is paragraph 1
</para>
<para bullet="0">This is paragraph 2
</para>
<para bullet="1">This is paragraph 3
</para>
<para bullet="1">This is paragraph 4
</para>
</summary>
t:\ftemp>type ian.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="summary">
  <xsl:apply-templates select="para[1]"/><!--walk through one at a time-->
</xsl:template>

<xsl:template match="para"><!--either standalone or first of a group-->
  <xsl:choose>
    <xsl:when test="@bullet='1'"><!--first of a group-->
      <ul>
        <xsl:apply-templates select="." mode="in-a-group-of-siblings"/>
      </ul>
          <!--skip all those in the group and start at the next standalone-->
      <xsl:apply-templates select="following-sibling::para[@bullet='0'][1]"/>
    </xsl:when>
    <xsl:otherwise><!--standalone-->
      <p><xsl:value-of select="."/></p>
      <xsl:apply-templates select="following-sibling::para[1]"/><!--next-->
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="para" mode="in-a-group-of-siblings">
  <li><xsl:value-of select="."/></li>
                    <!--only continue if the next one belongs in the group-->
  <xsl:apply-templates select="following-sibling::para[1][@bullet='1']"
                       mode="in-a-group-of-siblings"/>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>xt ian.xml ian.xsl
<?xml version="1.0" encoding="utf-8"?>
<ul>
<li>This is paragraph 1
</li>
</ul>
<p>This is paragraph 2
</p>
<ul>
<li>This is paragraph 3
</li>
<li>This is paragraph 4
</li>
</ul>

t:\ftemp>
t:\ftemp>xt ian.xml ian.xsl
<?xml version="1.0" encoding="utf-8"?>
<ul>
<li>This is paragraph 1
</li>
</ul>
<p>This is paragraph 2
</p>
<ul>
<li>This is paragraph 3
</li>
<li>This is paragraph 4
</li>
</ul>

t:\ftemp>



--
Upcoming: 3-days XSLT/XPath and/or 2-days XSLFO: June 17-21, 2002
-       : 3-days XML Information Modeling: July 31-August 2, 2002

G. Ken Holman                mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995)
ISBN 0-13-065196-6                      Definitive XSLT and XPath
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-07-1               Practical Formatting Using XSLFO
XSL/XML/DSSSL/SGML/OmniMark services, books(electronic, printed),
articles, training(instructor-live,Internet-live,web/CD,licensed)
Next public training:                  2002-05-06,07,09,10,13,20,
-                          06-04,07,10,11,13,14,17,20,07-31,08-05


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list



Current Thread