如何使用XSLT 1.0模板从作者那里获取书籍

时间:2012-06-15 17:30:42

标签: xslt

我对XSLT1.0有疑问。任务是用HTML写出给定作者只使用XSL模板编写的所有书籍。

<?xml version="1.0" encoding="UTF-8"?>
    <books>
        <book author="herbert">
            <name>Dune</name>
        </book>
        <book author="herbert">
            <name>Chapterhouse: Dune</name>
        </book>
        <book author="pullman">
            <name>Lyras's Oxford</name>
        </book>
        <book author="pratchett">
            <name>I Shall Wear Midnight</name>
        </book>
        <book author="pratchett">
            <name>Going Postal</name>
        </book>
        <author id="pratchett"><name>Terry Pratchett</name></author>
        <author id="herbert"><name>Frank Herbert</name></author>
        <author id="pullman"><name>Philip Pullman</name></author>
    </books>

到目前为止,我有这个解决方案。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <html>
            <head/>
            <body>
                <table border="1">
                    <xsl:apply-templates select="//author"/>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="author">
        <tr>
            <td>
                <xsl:value-of select="name/text()"/>
            </td>
            <td>
                <xsl:value-of select="@id"/>
            </td>
            <td>
                <xsl:apply-templates select="/books/book[@id=@author]"/>
                --previous XPath does not work properly, it should choose only those books that are written by the given author (that this template matches)
            </td>
        </tr>
    </xsl:template>
    <xsl:template match="book">
        <xsl:value-of select="name/text()"/>
    </xsl:template>
</xsl:stylesheet>

但是有一个问题,在评论中解释。


谢谢Martin和Marzipan - 它现在有效。还有一件事。如果我想让每个作者的书名用逗号分隔怎么办?我提出这个解决方案,但有更优雅的方法来实现这个目标吗?

...

<xsl:apply-templates select="/books/book[current()/@id=@author][not(position()=last())]" mode="notLast"/>
<xsl:apply-templates select="/books/book[current()/@id=@author][last()]"/>
                </td>
            </tr>
        </xsl:template>
        <xsl:template match="book">
            <xsl:value-of select="name/text()"/>
        </xsl:template>

        <xsl:template match="book" mode="notLast">
            <xsl:value-of select="name/text()"/>
            <xsl:text> , </xsl:text>
        </xsl:template>

    </xsl:stylesheet>

我刚刚意识到我的问题已经由Marzipan回答了。然后,Quesion就解决了。

2 个答案:

答案 0 :(得分:2)

您想要<xsl:apply-templates select="/books/book[current()/@id=@author]"/>或更好的密钥(作为xsl:stylesheet元素的子代):

<xsl:key name="book-by-author" match="books/book" use="@author"/>

然后<xsl:apply-templates select="key('book-by-author', @id)"/>

答案 1 :(得分:0)

问题在于这一行:

<xsl:apply-templates select="/books/book[@id=@author]"/>

了解当您输入谓词时,上下文将更改为前一个XPath选择器匹配的节点。上下文不再是与其所在模板匹配的节点。

为了解决这个问题,请将作者的ID提交给变量,然后在谓词中使用它。

<xsl:variable name='author_id' select='@id' />
...
[@author=$author_id]

[编辑 - 或者,正如马丁在他的回答中指出的那样,使用current()强制上下文]

您可以在此处运行:

http://www.xmlplayground.com/10FRcA