输出应该是html,并且输出目录,以下是xml
<!DOCTYPE book SYSTEM "book.dtd">
<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>
我的模板是:
<xsl:template match="book" as="element(xhtml:html)">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>
<xsl:value-of select="@title"/>
</title>
</head>
<body>
<h2>
<xsl:value-of select="@title"/>
</h2>
<p>
by <xsl:value-of select="author"/>
</p>
<h3>Table of contents</h3>
<ul>
<xsl:apply-templates select="chapter"/>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="chapter|section" as="element()*">
<xsl:param name ="seq" as="element(section)*"/>
<li xmlns="http://www.w3.org/1999/xhtml">
<xsl:value-of select="@title"/>
<xsl:apply-templates select ="section"/>
</li>
</xsl:template>
</xsl:transform>
我的HTML错了
<body>
<h2>D</h2>
<p>
by abc
</p>
<h3>Table of contents</h3>
<ul>
<li>chapter1
<li>section1.1</li>
<li>section1.2
<li>section1.2.1</li>
<li>section1.2.2</li>
</li>
<li>section3
<li>section3.1</li>
</li>
</li>
<li>chapter2</li>
</ul>
</body>
结果应为:
<body>
<h2>D</h2>
<p>
by abc
</p>
<h3>Table of contents</h3>
<ul>
<li>chapter1
<ul>
<li>section1.1</li>
<li>section1.2
<ul>
<li>section1.2.1</li>
<li>section1.2.2</li>
</ul>
</li>
<li>section3
<ul>
<li>section3.1</li>
</ul>
</li>
</ul>
</li>
<li>chapter2</li>
</ul>
</body>
答案 0 :(得分:1)
此转化:
<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: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: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>chapter1<ul>
<li>section1.1</li>
<li>section1.2<ul>
<li>section1.2.1</li>
<li>section1.2.2</li>
</ul>
</li>
<li>section3<ul>
<li>section3.1</li>
</ul>
</li>
</ul>
</li>
<li>chapter2</li>
</ul>
</body>
,并在浏览器中显示为:
by abc
答案 1 :(得分:0)
尝试在<xsl:apply-templates select ="section"/>
标记中包装<ul>...</ul>
。免责声明:自从我使用XSLT以来已经有一段时间了。
答案 2 :(得分:0)
尝试将以下模板添加到样式表中:
<xsl:template match="section/section[1]" as="element()*">
<ul>
<li xmlns="http://www.w3.org/1999/xhtml">
<xsl:value-of select="@title"/>
</li>
<xsl:apply-templates select ="following-sibling::section"/>
</ul>
</xsl:template>
答案 3 :(得分:0)
好的,所以你提供的模板不完整,所以我不能具体说明你需要做什么。但一般来说,您需要创建一个与book
元素匹配的模板,该元素使用apply-templates
来调用子元素的模板。对于章节,它可能看起来像
<xsl:template match="book">
<xsl:apply-templates match="author" />
<ul>
<xsl:apply-templates match="chapter" />
</ul>
</xsl:template>