How can I use <xsl:template match=""> to match the author

I'm trying to generate an HTML file using XML and XSL (XSLT). I want to show all the books that have been written by certain author (EX "Mario Vargas Llosa"). How can I do that using the match attribute in xsl:template?

XML CODE:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="template.xsl" type="text/xsl" ?>
<library>
  <book>
    <title language="es">La vida está en otra parte</title>
    <author>Milan Kundera</author>
    <publishDate year="1973"/>
  </book>
  <book>
    <title language="es">Pantaleón y las visitadoras</title>
    <author>Mario Vargas Llosa</author>
    <publishDate year="1973"/>
  </book>
  <book>
    <title language="es">Conversación en la catedral</title>
    <author>Mario Vargas Llosa</author>
    <publishDate year="1969"/>
  </book>
  <book>
    <title language="en">Poems</title>
    <author>Edgar Allan Poe</author>
    <publishDate year="1890"/>
  </book>  
  <book>
    <title language="fr">Les Miserables</title>
    <author>Victor Hugo</author>
    <publishDate year="1862"/>
  </book>  
  <book>
    <title language="es">Plenilunio</title>
    <author>Antonio Muñoz Molina</author>
    <publishDate year="1997"/>
  </book>  
</library>

XSLT CODE:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <html>
    <head>
        <link rel="stylesheet" href="style.css"/>
    </head>
        <body>
            <xsl:for-each select="library/book">
                <h1>Title: 
                    <xsl:value-of select="title"/>
                </h1>
                <p>
                    <strong>Author: </strong>
                    <xsl:value-of select="author"/>
                </p>
                <p>
                    <strong>Publishing date: </strong>
                    <xsl:value-of select="publishDate/@year"/>
                </p>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

Thanks in advance.

Upvotes: 0

Views: 59

Answers (2)

Yitzhak Khabinsky
Yitzhak Khabinsky

Reputation: 22321

One line modification in XSLT. You need to add a predicate.

<xsl:for-each select="library/book[author='Mario Vargas Llosa']">

Upvotes: 0

zx485
zx485

Reputation: 29062

Change your XSLT file to

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
        <head>
            <link rel="stylesheet" href="style.css"/>
        </head>
            <body>
                <xsl:for-each select="library/book[author='Mario Vargas Llosa']">
                    <h1>Title: 
                        <xsl:value-of select="title"/>
                    </h1>
                    <p>
                        <strong>Author: </strong>
                        <xsl:value-of select="author"/>
                    </p>
                    <p>
                        <strong>Publishing date: </strong>
                        <xsl:value-of select="publishDate/@year"/>
                    </p>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

and your xsl:for-each will iterate over all <book>s of "Mario Vargas Llosa". So the output (in your browser) will be

enter image description here

Upvotes: 1

Related Questions