×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Nested Nodes Unravelling

Nested Nodes Unravelling

Nested Nodes Unravelling

(OP)
I have a very simple XML:
<?xml version="1.0" encoding="UTF-8"?>
<nav title="Containerized IP Office J100 Series Phone User Guide"
     homepage="Introduction.html">
   <nav title="Introduction" href="Introduction.html">
      <nav title="Important Safety Information" href="Important_Safety_Information.html"/>
      <nav title="J129 Telephone" href="J129_Telephone.html"/>
      <nav title="J139 Telephone" href="J139_Telephone.html"/>
      <nav title="Appearance Buttons" href="Appearance_Buttons.html">
         <nav title="Call Appearance Buttons" href="Call_Appearance_Buttons.html"/>
         <nav title="Line Appearance Buttons" href="Line_Appearance_Buttons.html"/>
      </nav>
      <nav title="Programmable Feature Buttons" href="Feature_Buttons.html"/>
      <nav title="Status Display" href="Status_Display.html">
         <nav title="Status Icons" href="Status_Icons.html"/>
         <nav title="Status Letters" href="Status_Letters.html"/>
      </nav>
      <nav title="Icons" href="Icons.html"/>
      <nav title="The Phone Stand" href="The_Phone_Stand.html"/>
   </nav>
... 

I need to turn it into an HTML list with <ul> and <li> tags. However, being very much a novice, I am struggling to cope with the fact that some of the <nav> tags in the source contain other <nav> tags as child nodes.

The following works in part for the parent nodes when set to match 'nav' and for the child nodes when set to match 'nav/nav'.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
      <hmtl><head><title>Test</title></head>
        <ul><xsl:apply-templates/></ul>
      </hmtl>
    </xsl:template>

    <xsl:template match="nav">
        <xsl:for-each select="nav">
            <li><p><xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute><xsl:value-of select="@title"/>
            </xsl:element></p></li>    
        </xsl:for-each>
    </xsl:template>
        
</xsl:transform> 


Stuck in a never ending cycle of file copying.

RE: Nested Nodes Unravelling

Sizbut,

The unordered lists begin at the first "nav" of the branch. The first descendant and the next sibling are given the responsibility to grow the list deeper and wider, as needed:

CODE --> XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns="http://www.w3.org/1999/xhtml"
  version="1.0">
  
  <xsl:output method="html" encoding="UTF-8" />
  
  <xsl:template match="/">
    <html>
      <head><title>TOC</title></head>
      <body>
        <xsl:apply-templates select="nav"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="nav">
    <xsl:choose>
      <xsl:when test="not(preceding-sibling::nav)">
        <ul>
          <li>
            <xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="@href | @homepage"/></xsl:attribute><xsl:value-of select="@title"/></xsl:element>
          </li>
          <xsl:apply-templates select="descendant::nav[1]"/>
          <xsl:apply-templates select="following-sibling::nav[1]"/>
        </ul>
      </xsl:when>
      <xsl:otherwise>
        <li>
          <xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute><xsl:value-of select="@title"/></xsl:element>
        </li>
        <xsl:apply-templates select="descendant::nav[1]"/>
        <xsl:apply-templates select="following-sibling::nav[1]"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet> 

Resulting in:

RE: Nested Nodes Unravelling

(OP)
Many thanks. I'll get down to studying that.

Looking after a documentation website with material coming from a variety of authoring tools, I've become competent with HTML, RSS, PHP, Javascript, etc. But I suspect with XSL I'm about to discover many of my past tagged content reuse scenarios could have been much easier and powerful.

Stuck in a never ending cycle of file copying.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close