[英]XSLT book display multiple authors
我想讓 html 顯示這本書兩次,一次是第一作者,第二次是第二作者。 使用此代碼,缺少名字和姓氏。 有辦法嗎? xml 文件如下所示:
<books>
<book>
<authors>
<author>
<firstname>AuthorName1</firstname>
<lastname>AuthorLastName1</lastname>
</author>
<author>
<firstname>AuthorName2</firstname>
<lastname>AuthorLastName2</lastname>
</author>
</authors>
<name>BookName</name>
<year>2010</year>
</book>
</books>
xslt 看起來像這樣:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>Books</title>
</head>
<body>
<xsl:for-each select="books/book">
<p> FirstName:</p><xsl:value-of select="firstname" />
<p>LastName:</p><xsl:value-of select="lastname" />
<p>Book:</p><xsl:value-of select="name" />
<p>Year:</p><xsl:value-of select="year" />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
您想要的 output 不清楚。
請嘗試以下XSLT。
XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/books">
<html>
<head>
<title>Books</title>
</head>
<body>
<xsl:for-each select="book/authors/author">
<p>FirstName: <xsl:value-of select="firstname"/></p>
<p>LastName: <xsl:value-of select="lastname"/></p>
<p>Book: <xsl:value-of select="../../name"/></p>
<p>Year: <xsl:value-of select="../../year"/></p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
我懷疑消除重復在樹上上下行走並執行以下操作可能更有效:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>Books</title>
</head>
<body>
<xsl:for-each select="books/book">
<xsl:variable name="book">
<p>Book:</p><xsl:value-of select="name"/>
<p>Year:</p><xsl:value-of select="year"/>
</xsl:variable>
<xsl:for-each select="authors/author">
<p>FirstName:</p><xsl:value-of select="firstname"/>
<p>LastName:</p><xsl:value-of select="lastname"/>
<xsl:copy-of select="$book"/>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.