Home > Software design >  XSLT book display multiple authors
XSLT book display multiple authors

Time:01-31

I want the html to display the book twice, once with the first author and the second time with the second author. With this code the first and last name are missing. Is there a way to to that? The xml file looks like this:

<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>

and the xslt looks like this:

<?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>

CodePudding user response:

Your desired output is not clear.

Please try the following 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>

CodePudding user response:

I suspect it might be more efficient to eliminate the repeated walk up and down the tree and do:

<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>
  • Related