1

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

<?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 回答了。那么问题就解决了。

4

2 回答 2

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)"/>

于 2012-06-15T17:38:53.277 回答
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

于 2012-06-15T17:45:24.927 回答