如何使用 <xsl:template match=""> 来匹配作者

我正在尝试使用 XML 和 XSL (XSLT) 生成 HTML 文件。我想展示某个作者(EX“Mario Vargas Llosa”)写的所有书籍。如何使用 xsl:template 中的 match 属性来做到这一点?


XML代码:


<?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 代码:


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

提前致谢。


大话西游666
浏览 82回答 2
2回答

缥缈止盈

将您的 XSLT 文件更改为<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">&nbsp; &nbsp; <xsl:template match="/">&nbsp; &nbsp; &nbsp; &nbsp; <html>&nbsp; &nbsp; &nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <link rel="stylesheet" href="style.css"/>&nbsp; &nbsp; &nbsp; &nbsp; </head>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xsl:for-each select="library/book[author='Mario Vargas Llosa']">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h1>Title:&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xsl:value-of select="title"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </h1>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>Author: </strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xsl:value-of select="author"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>Publishing date: </strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xsl:value-of select="publishDate/@year"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </xsl:for-each>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </body>&nbsp; &nbsp; &nbsp; &nbsp; </html>&nbsp; &nbsp; </xsl:template></xsl:stylesheet>你的xsl:for-each意志会迭代<book>“马里奥·巴尔加斯·略萨”的所有内容。所以输出(在你的浏览器中)将是

Qyouu

XSLT 中的一行修改。您需要添加一个谓词。<xsl:for-each&nbsp;select="library/book[author='Mario&nbsp;Vargas&nbsp;Llosa']">
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5