gpt4 book ai didi

html - 目录 XSL

转载 作者:行者123 更新时间:2023-11-27 23:49:53 24 4
gpt4 key购买 nike

问题:我正在做一个目录,包含章节、小节、小节和子小节。我想要的是用 xsl 处理信息(在 XML 中),转换为 HTML 页面。

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>

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

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

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
(...)

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
(...)

有什么建议吗?

最佳答案

如果您需要在 CSS 中出现编号,您可以执行以下操作。使用以下 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>

和以下 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
}

结果如你所愿。

关于html - 目录 XSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27921026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com