[英]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.