XSLT到HTML导航栏菜单

达米

我想创建一个导航栏菜单,像这样:

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

我有一个具有该Structure的XML文件:

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

基本上,我可以chapter每个chapter都有很多title

section也可以有多个sectionwith 并且标题和多重性确实具有相同的方式titlesectionsubsectionsubsubsectionsection

我的问题是:<a href ...>当订单非常重要时,如何获得标题并放在导航栏上(还有正确的数字)例如:

1. title (referencing to chapter 1) 
1.1 title of section (referecing for chapter 1 section 1)
etc etc

我不知道如何用XSLT做到这一点。

明亮的达罗查

您可以使用<xsl:number>元素生成编号使用该count属性可以包含要计数的元素,并level="multiple"可以在多个嵌套级别对它们进行计数。format让你增量阿拉伯数字,罗马数字等。

通过包括:

<xsl:number format="1. " level="multiple" count="chapter | section | subsection | subsubsection"/>

<value-of>每个打印之前title,您可以生成多节编号。

这是一个样式表。它不使用id属性中的数据(但是您当然可以使用),而是使用带有形式的节编号生成URI片段#sec1.1.1每个项目都是在命名模板中生成的。顺序由每个调用嵌套节的模板控制:

<?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>

使用此输入:

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

您将得到以下结果:

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

您可以在此XSLT小提琴中尝试一下

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Html 导航栏下拉菜单

来自分类Dev

HTML 垂直导航栏高度到页脚

来自分类Dev

从GWT菜单栏导航到不同的UI

来自分类Dev

导航到ShinyDashboard中的特定侧边栏菜单项?

来自分类Dev

导航栏子菜单

来自分类Dev

带有子菜单html和css的导航栏

来自分类Dev

HTML Css Beginner创建下拉导航栏菜单

来自分类Dev

无法使菜单导航栏下拉

来自分类Dev

水平导航栏菜单子菜单对齐

来自分类Dev

HTML:href 到 div 然后减去粘性导航栏的高度

来自分类Dev

HTML 5导航菜单

来自分类Dev

CSS HTML导航菜单

来自分类Dev

从子域重定向到主域导航栏下拉菜单

来自分类Dev

操作栏菜单按钮无法导航到其他活动 (Android)

来自分类Dev

HTML导航栏问题

来自分类Dev

导航栏子菜单,具有完整的导航栏宽度

来自分类Dev

从透明导航到实体导航栏

来自分类Dev

HTML语义:导航栏中是否应包含用于在响应页面上触发菜单的图标?

来自分类Dev

如何仅使用CSS和HTML将响应式导航栏菜单居中?

来自分类Dev

Bootstrap 3-导航栏下拉菜单不适用于额外的HTML页面

来自分类Dev

从导航栏HTML CSS的下拉菜单中没有任何内容

来自分类Dev

Html/CSS - 导航栏子菜单不与下面的内容重叠

来自分类Dev

引导导航栏菜单与文本重叠

来自分类Dev

离子导航栏下拉菜单

来自分类Dev

笔尖视图未显示菜单或导航栏

来自分类Dev

CSS Selector Bootstrap导航栏菜单链接

来自分类Dev

我无法创建导航栏子菜单

来自分类Dev

将导航栏菜单与页面中间对齐

来自分类Dev

Bootstrap导航栏汉堡菜单无法打开