需要我的XSLT仅从XML输出特定作者(如果可用)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了需要我的XSLT仅从XML输出特定作者(如果可用)相关的知识,希望对你有一定的参考价值。
我试图只从我的XML Author属性输出,它等于McGraw-Hill,我对XSLT很新。我想知道我需要改变什么才能使它按预期工作。
?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>Book's list</title>
<link type="text/css" rel="stylesheet" href="style.css"/>
</head>
<body>
<xsl:apply-templates select="bookstore/book">
<xsl:sort select="year" data-type="number" order="descending"/>
<xsl:for-each select="bookstore/book/authors">
<xsl:if test="author = 'McGraw-Hill'">
</xsl:if>
</xsl:for-each>
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="bookstore">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="book">
<div class="book">
<br/>
<strong> <xsl:value-of select="title" /> </strong>
<span> Price: $ <xsl:value-of select="price" /> </span>
</div>
<div class="author">
Author(s):
<br/>
<xsl:for-each select="authors/author" >
<xsl:value-of select="."/>
<br/>
</xsl:for-each>
</div>
</xsl:template>
</xsl:stylesheet>
XML本身不包含作者,我希望它不输出任何内容,但它会在降序发布日期列出所有书籍,如果它不包含作者,则不希望包含书籍。
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Portfolio4.xsl"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<authors>
<author>Giada De Laurentiis</author>
</authors>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<authors>
<author>J K. Rowling</author>
</authors>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<authors>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
</authors>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<authors>
<author>Erik T. Ray</author>
</authors>
<year>2003</year>
<price>39.95</price>
</book>
<book category= "engineering" cover="hardcover">
<title lang="en">Network Fundamentals, CCNA Exploration Companion Guide</title>
<authors>
<author>Mark Dye</author>
<author>Rick McDonald</author>
<author>Antoon Rufi</author>
</authors>
<year>2007</year>
<price>7.25</price>
</book>
<book category= "engineering" cover="hardcover">
<title lang="en">Modern Operating Systems</title>
<authors>
<author>Andrew S. Tanenbaum</author>
<author>Herbert Bos</author>
</authors>
<year>2001</year>
<price>14.94</price>
</book>
<book category= "engineering" cover="hardcover">
<title lang="en">The Art of Computer Programming, Volume 1: Fundamental Algorithms</title>
<authors>
<author>Donald Ervin Knuth</author>
</authors>
<year>1997</year>
<price>75.32</price>
</book>
</bookstore>
编辑:添加了XML,所需的输出将是包含我的问题中所述作者的任何书籍将显示在输出中。
答案
您的问题出在这段代码中
<xsl:apply-templates select="bookstore/book">
<xsl:sort select="year" data-type="number" order="descending"/>
<xsl:for-each select="bookstore/book/authors">
<xsl:if test="author = 'McGraw-Hill'">
</xsl:if>
</xsl:for-each>
</xsl:apply-templates>
主要的问题是你无法在xsl:for-each
中嵌套xsl:apply-templates
。
你应该简化它
<xsl:apply-templates select="bookstore/book[authors/author='McGraw-Hill']">
<xsl:sort select="year" data-type="number" order="descending"/>
</xsl:apply-templates>
(当然,这不会在您的示例XML中返回任何结果,因为它没有任何由他们创作的书籍)。
试试这个XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>Book's list</title>
<link type="text/css" rel="stylesheet" href="style.css"/>
</head>
<body>
<xsl:apply-templates select="bookstore/book[authors/author='McGraw-Hill']">
<xsl:sort select="year" data-type="number" order="descending"/>
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="book">
<div class="book">
<br/>
<strong> <xsl:value-of select="title" /> </strong>
<span> Price: $ <xsl:value-of select="price" /> </span>
</div>
<div class="author">
Author(s):
<br/>
<xsl:for-each select="authors/author" >
<xsl:value-of select="."/>
<br/>
</xsl:for-each>
</div>
</xsl:template>
</xsl:stylesheet>
以上是关于需要我的XSLT仅从XML输出特定作者(如果可用)的主要内容,如果未能解决你的问题,请参考以下文章