XSLT to HTML navbar Menu -
i wanted create navbar menu :
<table width="100%"> <tr> <td id="navbar" width="15%"> <nav> <ul> <li><b id="menu">menu</b></li> <!-- menu here --> </ul> </nav> </td> <td width="85%"> <xsl:apply-templates select="frontmatter"/> </td> </tr> </table> and have xml file construction :
<body> <chapter id="...1"> <title>...</title> <section id="...1"> <title>...</title> <subsection id="...1"> <title>...</title> <subsubsection id="...1"> <section id="...2"> . . <chapter id="...2"> . . basically, can have lot of chapter each chapter have title.
sectioncan have multiple section title on each section and, subsection , subsubsectiondoes have same way title , multiplicity section.
my question is: how title , set on navbar <a href ...> when order of import ? (with right number also) illustration :
1. title (referencing chapter 1) 1.1 title of section (referecing chapter 1 section 1) etc etc i don't know how xslt.
you can generate numbering using <xsl:number> element. utilize count attribute include elements want count, , level="multiple" count them in multiple nesting levels. format allows increment standard arabic digits, roman digits, etc.
by including:
<xsl:number format="1. " level="multiple" count="chapter | section | subsection | subsubsection"/> before <value-of> prints each title, can generate multi-section numbering.
here stylesheet. doesn't utilize info in id attributes (but can, of course) generates uri fragment using section numbering form #sec1.1.1. each item generated in named template. order controlled each template calls nested sections:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:output indent="yes"/> <xsl:template match="/"> <table width="100%"> <tr> <td id="navbar" width="15%"> <nav> <ul> <li><b id="menu">menu</b></li> <xsl:apply-templates select="body/chapter"/> </ul> </nav> </td> <td width="85%"> <xsl:apply-templates select="frontmatter"/> </td> </tr> </table> </xsl:template> <xsl:template match="chapter"> <xsl:call-template name="generate-item"/> <xsl:apply-templates select="section"/> </xsl:template> <xsl:template match="section"> <xsl:call-template name="generate-item"/> <xsl:apply-templates select="subsection"/> </xsl:template> <xsl:template match="subsection"> <xsl:call-template name="generate-item"/> <xsl:apply-templates select="subsubsection"/> </xsl:template> <xsl:template match="subsubsection"> <xsl:call-template name="generate-item"/> </xsl:template> <xsl:template name="generate-item"> <li><a> <xsl:attribute name="href"> <xsl:text>#sec</xsl:text> <xsl:number format="1" level="multiple" count="chapter | section | subsection | subsubsection"/> </xsl:attribute> <xsl:number format="1. " level="multiple" count="chapter | section | subsection | subsubsection"/><xsl:value-of select="title"/> </a></li> </xsl:template> </xsl:stylesheet> using input:
<body> <chapter id="c1"> <title>title of chapter 1</title> <section id="c1s1"> <title>title of section</title> <subsection id="s1s1"> <title>title of subsection</title> <subsubsection id="s1s1s1"> <title>title of subsubsection</title> </subsubsection> </subsection> </section> <section id="c1s2"> <title>title of section</title> <section id="c1s2s1"> <title>title of subsection</title> <subsection id="c1s2s1s1"> <title>title of subsubsection</title> </subsection> </section> </section> </chapter> <chapter id="c2"> <title>title of chapter 2</title> <section id="c2s1"> <title>title of section</title> <subsection id="c2s1s1"> <title>title of subsection</title> <subsubsection id="c2s1s1s1"> <title>title of subsubsection</title> </subsubsection> </subsection> </section> <section id="c2s2"> <title>title of section</title> <section id="c2s2s1"> <title>title of subsection</title> <subsection id="c2s2s1s1"> <title>title of subsubsection</title> </subsection> </section> </section> </chapter> </body> you have result:
<table width="100%"> <tr> <td id="navbar" width="15%"> <nav> <ul> <li> <b id="menu">menu</b> </li> <li> <a href="#sec1">1. title of chapter 1</a> </li> <li> <a href="#sec1.1">1.1. title of section</a> </li> <li> <a href="#sec1.1.1">1.1.1. title of subsection</a> </li> <li> <a href="#sec1.1.1.1">1.1.1.1. title of subsubsection</a> </li> <li> <a href="#sec1.2">1.2. title of section</a> </li> <li> <a href="#sec2">2. title of chapter 2</a> </li> <li> <a href="#sec2.1">2.1. title of section</a> </li> <li> <a href="#sec2.1.1">2.1.1. title of subsection</a> </li> <li> <a href="#sec2.1.1.1">2.1.1.1. title of subsubsection</a> </li> <li> <a href="#sec2.2">2.2. title of section</a> </li> </ul> </nav> </td> <td width="85%"/> </tr> </table> you can seek out in xslt fiddle
html xslt
No comments:
Post a Comment