[英]numbering a table
輸出應該是html,使用xsl輸出目錄,以下是xml
<book title="D">
<author>
<name>abc</name>
</author>
<chapter title="chapter1">
<section title="section1.1"/>
<section title="section1.2">
<section title="section1.2.1"/>
<section title="section1.2.2"/>
</section>
<section title="section1.3">
<section title="section1.3.1"/>
</section>
</chapter>
<chapter title="chapter2"/>
</book>
結果是html,如下所示:
<body>
<h2>D</h2>
<p>
by abc
</p>
<h3>Table of contents</h3>
<ul>
<li>[1]chapter1
<ul>
<li>[1.1]section1.1</li>
<li>[1.2]section1.2
<ul>
<li>[1.2.1]section1.2.1</li>
<li>[1.2.2]section1.2.2</li>
</ul>
</li>
<li>[1.3]section1.3
<ul>
<li>[1.3.1]section1.3.1</li>
</ul>
</li>
</ul>
</li>
<li>[2]chapter2</li>
</ul>
</body>
更新 :發布此解決方案后,OP更改了他提供的XML。 下面的解決方案產生所需的正確編號。 我不是為了趕上OP的更新而更新它,因為我不能花所有的時間等待每次下一次更新的發生。
使用<xsl:number>
的強大功能在於,無論對標題的字符串值進行什么更新,生成的編號仍然是正確的。 :)
此轉換 :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="book">
<body>
<xsl:apply-templates select="node()|@*"/>
</body>
</xsl:template>
<xsl:template match="book/@title">
<h2>
<xsl:value-of select="."/>
</h2>
</xsl:template>
<xsl:template match="author">
<p>by
<xsl:value-of select="name"/>
</p>
<h3>Table of Contents</h3>
<ul>
<xsl:apply-templates mode="TC"
select="following-sibling::*"/>
</ul>
</xsl:template>
<xsl:template mode="TC"
match="chapter[section]|section[section]">
<li>
[<xsl:number level="multiple"
count="chapter|section"/>] <xsl:text/>
<xsl:value-of select="@title"/>
<ul>
<xsl:apply-templates mode="TC"/>
</ul>
</li>
</xsl:template>
<xsl:template mode="TC" match=
"chapter[not(section)]|section[not(section)]">
<li>
[<xsl:number level="multiple"
count="chapter|section"/>] <xsl:text/>
<xsl:value-of select="@title"/>
</li>
</xsl:template>
<xsl:template match="chapter|section"/>
</xsl:stylesheet>
當應用於提供的XML文檔時 :
<book title="D">
<author>
<name>abc</name>
</author>
<chapter title="chapter1">
<section title="section1.1"/>
<section title="section1.2">
<section title="section1.2.1"/>
<section title="section1.2.2"/></section>
<section title="section3">
<section title="section3.1"/></section>
</chapter>
<chapter title="chapter2"/>
</book>
產生想要的正確結果 :
<body>
<h2>D</h2>
<p>by
abc</p>
<h3>Table of Contents</h3>
<ul>
<li>
[1] chapter1<ul>
<li>
[1.1] section1.1</li>
<li>
[1.2] section1.2<ul>
<li>
[1.2.1] section1.2.1</li>
<li>
[1.2.2] section1.2.2</li>
</ul>
</li>
<li>
[1.3] section3<ul>
<li>
[1.3.1] section3.1</li>
</ul>
</li>
</ul>
</li>
<li>
[2] chapter2</li>
</ul>
</body>
瀏覽器將其顯示為 :
通過abc
說明 :
將<xsl:number>
與level="multiple"
計算chapter
section
。
此樣式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="book">
<body>
<h2><xsl:apply-templates select="@title" /></h2>
<p>by <xsl:apply-templates select="author/name" /></p>
<h3>Table of contents</h3>
<ul>
<xsl:apply-templates select="chapter" />
</ul>
</body>
</xsl:template>
<xsl:template match="chapter|section">
<li><xsl:call-template name="extract-num" /></li>
</xsl:template>
<xsl:template match="chapter[child::*]|section[child::*]">
<li>
<xsl:call-template name="extract-num" />
<ul>
<xsl:apply-templates />
</ul>
</li>
</xsl:template>
<xsl:template name="extract-num">
<xsl:value-of select="concat('[', substring(@title, 8), ']', @title)" />
</xsl:template>
</xsl:stylesheet>
在此輸入上:
<book title="D">
<author>
<name>abc</name>
</author>
<chapter title="chapter1">
<section title="section1.1" />
<section title="section1.2">
<section title="section1.2.1" />
<section title="section1.2.2" />
</section>
<section title="section3">
<section title="section3.1" />
</section>
</chapter>
<chapter title="chapter2" />
</book>
生產:
<body>
<h2>D</h2>
<p>by abc</p>
<h3>Table of contents</h3>
<ul>
<li>[1]chapter1
<ul>
<li>[1.1]section1.1</li>
<li>[1.2]section1.2
<ul>
<li>[1.2.1]section1.2.1</li>
<li>[1.2.2]section1.2.2</li>
</ul>
</li>
<li>[3]section3
<ul>
<li>[3.1]section3.1</li>
</ul>
</li>
</ul>
</li>
<li>[2]chapter2</li>
</ul>
</body>
注意:空格已針對格式進行了調整。 如果您確實需要在請求的輸出中提供的確切空格,則需要進行相應的修改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.