【问题标题】:How can I use <xsl:template match=""> to match the author如何使用 <xsl:template match=""> 来匹配作者
【发布时间】:2020-03-25 16:04:04
【问题描述】:

我正在尝试使用 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>

提前致谢。

【问题讨论】:

    标签: html xml xslt xsd


    【解决方案1】:

    将您的 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[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>
    

    您的xsl:for-each 将遍历“Mario Vargas Llosa”的所有&lt;book&gt;s。所以输出(在您的浏览器中)将是

    【讨论】:

      【解决方案2】:

      XSLT 中的一行修改。您需要添加一个谓词。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-22
        • 1970-01-01
        • 2017-03-26
        • 2023-03-27
        • 2019-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多