xsl-list
[Top] [All Lists]

Building a hierarchy based on siblings

2004-06-10 18:34:38
Hi all,

I am having some trouble getting the correct template matches written to
build a hierarchy.

I have similar to the following input XML:

<sect1>
        <para>First Paragraph</para>
        <para>* First list item</para>
        <para>* Second list item</para>
        <para>* Third list item <emphasis>emphasized text</emphasis></para>
        <para>Second Paragraph</para>
        <para>Third paragraph</para>
        <!-- Start new list-->
        <para>* First list item #2</para>
        <para>* Second list item #2</para>
        <para>Another paragraph</para>
</sect1>

I would like to put all para nodes that begin with "* " into a list
structure and cut off the first two characters when processed, to get the
following output:

<para>First Paragraph</para>
<itemizedlist>
        <listitem>
                <para>First list item</para>
        </listitem>
        <listitem>      
                <para>Second list item</para>
        </listitem>
        <listitem>
                <para>Third list item <emphasis>emphasized
text</emphasis></para>
        <listitem>
</itemizedlist>
<para>Second Paragraph</para>
<para>Third paragraph</para>
<itemizedlist>
        <listitem>
                <para>First list item #2</para>
        </listitem>
        <listitem>      
                <para>Second list item #2</para>
        </listitem>
</itemizedlist>
<para>Another paragraph</para>

I was using this to get the <listitems> built:

<xsl:template match="para">
  <xsl:choose>
    <xsl:when test="substring(node()[1], 0, 3) = '* '>
      <itemizedlist>
          <listitem>
          <xsl:value-of select="substring(node()[1], 3)"/>
          <xsl:apply-templates select="node()[position() > 1]"/>
          </listitem>
        </itemizedlist>
    </xsl:when>
    <xsl:otherwise>
      <para>
          <xsl:apply-templates/>
        </para>
    </xsl:otherwise>
</xsl:template>

But with this match, every <para> that starts with "* " becomes its own
itemizedlist with one listitem.  Is there an efficient way to do this using
a recursive template, without creating duplicate output?

I appreciate any and all suggestions!

Thanks,
Ryan


<Prev in Thread] Current Thread [Next in Thread>