Andrei
Andrei

Reputation: 43

Tricky nested list XSLT

I'm new to XSLT and a bit confused about formating lists. Basicly I need my XML structure, there's a part of it:

<slideshow>
    <slide id="A1">
        <title>XML techniques</title>
        <paragraph> Slideshow prepresents different kind of <bold>XML</bold> techniques </paragraph>
        <paragraph> Most common XML Techniques are </paragraph>
        <numberedlist>
            <item> Basic XML, DTD (version 1.0) </item>
            <item> XHTML </item>
            <itemizedlist>
                <item> XHTML 1.0 </item>
                <item> XHTML basic </item>
                <numberedlist>
                    <item> for mobile phones </item>
                    <item> basic set for all XHTML documents</item>
                </numberedlist>
            </itemizedlist>
            <item> XML namespace </item>
            <item> XSL </item>
            <itemizedlist>
                <item> XSLT - template based programming language</item>
                <item> XSL-FO - formating output like CSS </item>
            </itemizedlist>

            <item> Programming API (like SAX and DOM) </item>
            <item> XML Schemas </item>
        </numberedlist>
    </slide>
..
</slideshow>

To look like this:

  1. Basic XML, DTD (version 1.0)
    • XHTML 1.0
    • XHTML basics
      1. for mobile phones
      2. basic set for all XHTML documents
  2. XHTML
  3. XML namespace
  4. XSL
    • XSLT - template based programming language
    • XSL-FO - formatting output like CSS
  5. Programming API (like SAX and DOM)
  6. XML Schemas

I wanted to do this as simple as possible, so I was just using templates and no complicated XPath masks, but seems like there's no simply way.. Could someone help? Thank you!

Upvotes: 0

Views: 1178

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116982

Actually, this is trivial in XSLT, due to it's recursive processing model.

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<xsl:template match="numberedlist">
    <ol>
        <xsl:apply-templates/>
    </ol>
</xsl:template>

<xsl:template match="itemizedlist">
    <ul>
        <xsl:apply-templates/>
    </ul>
</xsl:template>

<xsl:template match="item">
    <li>
        <xsl:value-of select="." />
    </li>
</xsl:template>

<xsl:template match="text()"/>

</xsl:stylesheet>

Result

<ol>
   <li> Basic XML, DTD (version 1.0) </li>
   <li> XHTML </li>
   <ul>
      <li> XHTML 1.0 </li>
      <li> XHTML basic </li>
      <ol>
         <li> for mobile phones </li>
         <li> basic set for all XHTML documents</li>
      </ol>
   </ul>
   <li> XML namespace </li>
   <li> XSL </li>
   <ul>
      <li> XSLT - template based programming language</li>
      <li> XSL-FO - formating output like CSS </li>
   </ul>
   <li> Programming API (like SAX and DOM) </li>
   <li> XML Schemas </li>
</ol>

rendered:

enter image description here

Upvotes: 2

Related Questions