目录XSL [英] Table of contents XSL

查看:108
本文介绍了目录XSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我正在制作目录,包含章节,章节,小节和子节。我想要的是使用xsl处理信息(使用XML),转换成HTML页面。



XML

 < chapter id =c1> 
< title>第1章< / title>
< section id =s1.1>
< title>动机< / title>
(...)

< section id =s1.2>
(...)
< chapter id =c2>

< title>第2章< / title>
< section id =s2.1>
< title>遗传学< / title>
< subsection id =ss2.1.1>
< title>简史< / title>
(...)

XSL

 < xsl:template match =toc> 
< h3>目录< / h3>
< ul>
< xsl:sort />
< / xsl:apply-templates>
< / ul>
< / xsl:template>


< xsl:template mode =indicematch =chapter>
< h3>
< xsl:value-of select =title/>
< / h3>


    < xsl:for-each select =section>
    < li>
    < a href =#{@ id}>
    < xsl:value-of select =title/>
    < / a>
    < ol>
    < xsl:for-each select =subsection>
    < li>
    < a href =#{@ id}>
    < xsl:value-of select =title/>
    < / a>
    < ol>
    < xsl:for-each select =subsubsection>
    < li>
    < a href =#{@ id}>
    < xsl:value-of select =title/>
    < / a>
    < / li>
    < / xsl:for-each>
    < / ol>
    < / li>
    < / xsl:for-each>
    < / ol>
    < / li>
    < / xsl:for-each>
    < / ol>
    < / xsl:template>

我使用以下CSS样式在有序列表中创建子列表:

 < style> 
body {
text-align:justify;
}
ol {counter-reset:item}
li {display:block}
li:before {content:counters(item,。)。; counter-increment:item}
< / style>

HTML中的当前结果:

<$ p $第1章
1.动机
2.目标
3.论文结构

第2章
1.遗传学
1.1。简史
1.2。话题
2.基因:学习
2.1。 Eucariots
2.2。 Procaritots
3.东西
3.1。东西
3.2。东西
3.2.1。东西
3.2.2。
(...)

HTML中的所需结果:

 第1章
1.1。动机
1.2。目标
1.3。论文结构

第2章
2.1。遗传
2.1.1简史
2.1.2主题
2.2。基因:研究
2.2.1。 Eucariots
2.2.2。 Procaritots
2.3。材料
2.3.1。东西
2.3.2。东西
2.3.2.1。东西
2.3.2.2。
(...)

有何建议?

解决方案

如果您要求在CSS中进行编号,您可以这样做。
使用以下XSL:

 < xsl:template match =toc> 
< h3>目录< / h3>
< ul>
< xsl:apply-templates mode =indiceselect =// chapter/>
< / ul>
< / xsl:template>


< xsl:template mode =indicematch =chapter>
<! - 在与章节相关的列表项上添加一个类 - >
< li class =chapter>
< xsl:value-of select =title/>
< ol>
< xsl:for-each select =section>
< li>
< a href =#{@ id}>
< xsl:value-of select =title/>
< / a>
< xsl:if test =subsection>
< ol>
< xsl:for-each select =subsection>
< li>
< a href =#{@ id}>
< xsl:value-of select =title/>
< / a>
< xsl:if test =subsubsection>
< ol>
< xsl:for-each select =subsubsection>
< li>
< a href =#{@ id}>
< xsl:value-of select =title/>
< / a>
< / li>
< / xsl:for-each>
< / ol>
< / xsl:if>
< / li>
< / xsl:for-each>
< / ol>
< / xsl:if>
< / li>
< / xsl:for-each>
< / ol>
< / li>
< / xsl:template>

和以下CSS:

  body {
text-align:justify;
}
ol,ul {
counter-reset:item;
}
li {
display:list-item;
list-style-type:none;
}
li.chapter :: before {
content:;
}
li:之前{
content:counters(item,。)。;
counter-increment:item
}

结果就是您所期望的。

Problem: I'm doing a table of contents, with chapters, sections, subsections and subsubsections. What I want is to process the information (that is in XML) with xsl, transforming into an HTML page.

XML:

<chapter id="c1">
        <title>Chapter 1</title>
        <section id="s1.1">
            <title>Motivation</title>
                 (...)

        <section id= "s1.2">
                 (...)
 <chapter id="c2">

        <title>Chapter 2 </title>
        <section id="s2.1">
             <title> Genetics </title>
                 <subsection id="ss2.1.1">
                      <title> Brief History </title> 
                     (...)

XSL:

<xsl:template match="toc">
        <h3>Table of contents</h3>
        <ul> 
            <xsl:apply-templates mode="indice" select="//chapter">
            <xsl:sort/>
            </xsl:apply-templates>
        </ul>
</xsl:template>


<xsl:template mode="indice" match="chapter">      
    <h3> 
            <xsl:value-of select="title"/>
    </h3>

    <ol>
        <xsl:for-each select="section">               
            <li>
                <a href="#{@id}"> 
                    <xsl:value-of select="title"/>
                </a>  
                <ol>
                <xsl:for-each select="subsection">                      
                        <li>
                            <a href="#{@id}"> 
                                <xsl:value-of select="title"/>
                            </a>
                            <ol>
                                <xsl:for-each select="subsubsection">                      
                                    <li>
                                        <a href="#{@id}"> 
                                            <xsl:value-of select="title"/>
                                        </a>
                                    </li>
                                </xsl:for-each> 
                            </ol>
                        </li>                                               
                </xsl:for-each> 
                </ol>
            </li>               
        </xsl:for-each>
    </ol>
</xsl:template>

I've used the following CSS style to make sublists in ordered lists:

<style>
     body{
        text-align: justify;
     }
     ol {counter-reset: item}
     li {display: block}
     li:before {content: counters(item, ".") ". "; counter-increment: item}
</style>

Current Result in HTML:

 Chapter 1
       1. Motivation
       2. Objectives
       3. Thesis structure

 Chapter 2
       1. Genetics
          1.1. Brief History
          1.2. Topics
       2. Genes: study
          2.1. Eucariots
          2.2. Procaritots
       3. Stuff
          3.1. stuff
          3.2. stuff
                 3.2.1. stuff
                 3.2.2. stuff
          (...) 

Desired Result in HTML:

 Chapter 1
       1.1. Motivation
       1.2. Objectives
       1.3. Thesis structure

 Chapter 2
       2.1. Genetics
          2.1.1 Brief History
          2.1.2 Topics
       2.2. Genes: study
          2.2.1. Eucariots
          2.2.2. Procaritots
       2.3. Stuff
          2.3.1. stuff
          2.3.2. stuff
                 2.3.2.1. stuff
                 2.3.2.2. stuff
          (...) 

Any suggestions?

解决方案

If you required the numbering to occur in CSS, here is what you can do. With the following XSL :

<xsl:template match="toc">
      <h3>Table of contents</h3>
      <ul>
          <xsl:apply-templates mode="indice" select="//chapter"/>
      </ul>
</xsl:template>


<xsl:template mode="indice" match="chapter">
  <!-- Add a class on the list items related to a chapter -->
  <li class="chapter"> 
    <xsl:value-of select="title"/>
    <ol>
        <xsl:for-each select="section">               
            <li>
                <a href="#{@id}">
                  <xsl:value-of select="title"/>
                </a>
                <xsl:if test="subsection">
                  <ol>
                    <xsl:for-each select="subsection">
                      <li>
                          <a href="#{@id}"> 
                              <xsl:value-of select="title"/>
                          </a>
                          <xsl:if test="subsubsection">
                            <ol>
                              <xsl:for-each select="subsubsection">                      
                                <li>
                                  <a href="#{@id}"> 
                                      <xsl:value-of select="title"/>
                                   </a>
                                </li>
                              </xsl:for-each> 
                          </ol>
                        </xsl:if>
                      </li>                                               
                    </xsl:for-each> 
                  </ol>
                </xsl:if>
            </li>               
        </xsl:for-each>
    </ol>
  </li>
</xsl:template>

and the following CSS :

   body {
      text-align: justify;
   }
   ol, ul {
    counter-reset: item;
   }
   li {
    display: list-item;
    list-style-type: none;
   }
   li.chapter::before {
    content: "";
   }
   li:before {
    content: counters(item, ".") ". ";
    counter-increment: item
   }

the result is what you expect.

这篇关于目录XSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆