[英]use xsl to output the html
輸出應該是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>
此轉換 :
<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>
並在瀏覽器中顯示為 :
通過abc
嘗試將<xsl:apply-templates select ="section"/>
包裝在<ul>...</ul>
標簽中。 免責聲明:自從我使用XSLT已經有一段時間了。
嘗試將以下模板添加到樣式表:
<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>
好的,因此您提供的模板不完整,因此我無法具體說明您需要做什么。 但是通常,您需要制作一個與book
元素匹配的apply-templates
,該模板使用apply-templates
調用子元素的模板。 對於各章,它看起來像
<xsl:template match="book">
<xsl:apply-templates match="author" />
<ul>
<xsl:apply-templates match="chapter" />
</ul>
</xsl:template>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.