Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested Nodes Unravelling 1

Status
Not open for further replies.

sizbut

Technical User
Oct 25, 2003
5,129
GB
I have a very simple XML:
[pre]<?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>
...[/pre]

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'.

[pre]<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl=" version="2.0">
<xsl:eek:utput 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>[/pre]

Stuck in a never ending cycle of file copying.
 
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:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
  xmlns:xs="[URL unfurl="true"]http://www.w3.org/2001/XMLSchema"[/URL]
  xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL]
  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:

Captura_de_ecr%C3%A3_2021-03-13_015048_re3t1q.png
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top