This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: unordered lists from xml to html
- From: "Joerg Heinicke" <joerg dot heinicke at gmx dot de>
- To: <xsl-list at lists dot mulberrytech dot com>
- Date: Sun, 28 Apr 2002 13:10:26 +0200
- Subject: Re: [xsl] unordered lists from xml to html
- References: <LPBBLKDCEPEKCEAEBNFGOEIPCGAA.ian@hord.com>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Hello Ian,
when creating structured XML, mostly <xsl:key> is a good help. In your case
it's important to find always the first para with @bullet='1' after a
@bullet!='1' or the first para in the tree if @bullet='1'. Expressed in XSL:
<!-- Create a key for all <para>s with @bullet='1' -->
<xsl:key name="list-paras" match="para[@bullet='1']"
<!-- For this use the ID of this element ... -->
use="generate-id(
<!-- which is the first para with @bullet='1' after the first preceding para
not with @bullet='1' -->
(preceding-sibling::para[@bullet!='1'][1]/
following-sibling::para[1]
|
<!-- and the first para in this subtree (para[1] has no preceding para with
@bullet!='1' to test for) -->
../para[1]
)
<!-- from these one or two elements selected use the last one in document
order (the nearest to the current para) -->
[last()]
)"/>
(Remove the comments for testing.)
After this more or less complex expression, it's relative easy:
<xsl:template match="summary">
<!-- select all <para>s which are "starter" of a <ul> or whose
@bullet!='1' -->
<xsl:apply-templates select="para[count( key('list-paras',
generate-id())) > 0] | para[@bullet!='1']"/>
</xsl:template>
<xsl:template match="para[@bullet='1']">
<ul>
<!-- select all <para>s belonging to this list -->
<xsl:apply-templates select="key('list-paras', generate-id())"
mode="li"/>
</ul>
</xsl:template>
<xsl:template match="para" mode="li">
<li><xsl:value-of select="."/></li>
</xsl:template>
<xsl:template match="para[@bullet!='1']">
<p><xsl:value-of select="."/></p>
</xsl:template>
Hope this helps and is understandable,
Joerg
----- Original Message -----
From: "Ian Hord" <ian@hord.com>
> Hi,
>
> I am new to xsl and struggling with converting some xml to html. For
example
> I have the following
>
> <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>
>
> I want to convert it to the following html using xsl
>
> <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>
>
> You will see that the attributes in the para element determine whether the
> paragraph should be in a bullet or not.
>
> If anyone can suggest a solution I will be most grateful.
> Thanks in advance,
>
> Ian Hord
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list