输出应为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>
答案 0 :(得分:1)
更新:在发布此解决方案后,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
。
答案 1 :(得分:0)
此样式表:
<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>
注意:根据格式调整了空白。如果您确实需要在请求的输出中提供的确切空格,那么您需要相应地进行修改。