我尝试将我的XSL文件生成为HTML格式,但它不起作用。我认为问题来自我定义的模板匹配。
以下是我构建模板匹配的XML:
<?xml version="1.0" encoding="ISSO−8859−15" ?>
<Library>
<authors>
<Author id="author1" name="Einstein"/>
</authors>
<publications>
<Book year="1900" title="7 Kingdoms" author="author1"/>
<Magazine year="2010" number="203" title="The News"'/>
<Book year="1956" title="The Fall" author="author1"/>
</publications>
</Library>
以下是我想要的HTML格式显示作者,书籍和出版物:
<html>
<h1>Library</h1>
<h2>Authors</h2>
<ul>
<li id="author1">Einstein</li>
</ul>
<h2>Books</h2>
<ul>
<li>7 Kingdoms, 1942, <a href="#author">Einstein</a></li>
<li>The Fall, 1956, <a href="#author">Einstein</a></li>
</ul>
<h2>Magazines</h2> <ul>
<li>The News (203), 2010</li>
</ul>
</html>
这是我建立的模板匹配:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="Author">
<li id= "library/author/Author/@id">
<xsl:value-of select="library/authors/Author/name"/>
</li>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Magazines">
<li>
<xsl:value-of select="library/publications/magazine[@title]"/>
(
<xsl:value-of select="library/publications/magazine[@number]"/>
),
<xsl:value-of select="library/publications/magazine[@year]"/>
</li>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Book">
<li>
<xsl:value-of select="library/publications/book[@year]"/>
,
<xsl:value-of select="library/publications/book[@title]"/>
,
<xsl:value-of select="library/publications/book/author/@ref"/>
</li>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
如前所述,我认为问题来自模板匹配,这会阻止正确填充HTML文件。
非常感谢任何帮助。
答案 0 :(得分:0)
据我所知(!),你想做:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:key name="author" match="Author" use="@id" />
<xsl:template match="/Library">
<html>
<h1>Library</h1>
<h2>Authors</h2>
<ul>
<xsl:apply-templates select="authors/Author"/>
</ul>
<h2>Books</h2>
<ul>
<xsl:apply-templates select="publications/Book"/>
</ul>
<h2>Magazines</h2>
<ul>
<xsl:apply-templates select="publications/Magazine"/>
</ul>
</html>
</xsl:template>
<xsl:template match="Author">
<li id="{@id}">
<xsl:value-of select="@name"/>
</li>
</xsl:template>
<xsl:template match="Book">
<li>
<xsl:value-of select="@title"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="@year"/>
<xsl:text>, </xsl:text>
<a href="#author">
<xsl:value-of select="key('author', @author)/@name"/>
</a>
</li>
</xsl:template>
<xsl:template match="Magazine">
<li>
<xsl:value-of select="@title"/>
<xsl:text> (</xsl:text>
<xsl:value-of select="@number"/>
<xsl:text>), </xsl:text>
<xsl:value-of select="@year"/>
</li>
</xsl:template>
</xsl:stylesheet>
应用于您的输入示例(在删除第7行中的额外撇号之后),结果将是:
<html>
<h1>Library</h1>
<h2>Authors</h2>
<ul>
<li id="author1">Einstein</li>
</ul>
<h2>Books</h2>
<ul>
<li>7 Kingdoms, 1900, <a href="#author">Einstein</a></li>
<li>The Fall, 1956, <a href="#author">Einstein</a></li>
</ul>
<h2>Magazines</h2>
<ul>
<li>The News(203), 2010</li>
</ul>
</html>