我试图只从我的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本身不包含作者,我希望它不输出任何内容,但它会在降序发布日期列出所有书籍,如果它不包含作者,则不希望包含书籍。< / p>
<?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,所需的输出将是包含我问题中所述作者的任何书籍将显示在输出中。
答案 0 :(得分:0)
你的问题出现在这段代码中
appsettings.json
主要问题是你无法在<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
(当然,这不会在您的示例XML中返回任何结果,因为它没有任何由他们创作的书籍。)
试试这个XSLT
<xsl:apply-templates select="bookstore/book[authors/author='McGraw-Hill']">
<xsl:sort select="year" data-type="number" order="descending"/>
</xsl:apply-templates>