从内匹配元素的ID的换每个与另一种元素的ID

问题描述 投票:0回答:1

很抱歉,如果我的标题是不是很清楚:这是我很难拿出简明措辞我的问题的正确方法。

这里是我的XML结构是这样的:

<library>
    <books>
        <book writers="007,911">
            <title>Two authors wrote me</title>
            <price>4.00</price>
        </book>
        <book writers="911">
            <title>Only one here</title>
            <price>2.50</price>
        </book>
    </books>
    <authors>
        <author id="007">
            <name>James</name>
        </author>
        <author id="911">
            <name>Police</name>
        </author>
        <author id="666">
            <name>Diablo</name>
        </author>
    </authors>
</library>

我的目标,为每一位author:列出自己的孩子(在这里我只目前name),而且还列出所有books的其中当前id匹配writers的至少一个。如果没有book-找到匹配,那么应该不会出现books的该特定author列表。这份名单应该在所有的比赛降价格排序。

下面是代码的基本XSLT部分我迄今所做的:

<xsl:template match="/"> <!-- I'm skipping all the irrelevant <html> stuff, of course. -->

    <body>
        <ol> 
            <xsl:for-each select="//author">
                <li>
                    <b>ID: </b><xsl:value-of select="@id"/>
                    <ul>
                        <xsl:call-template name="auth_infos"></xsl:call-template>
                        <xsl:call-template name="books_stuff"></xsl:call-template>
                    </ul>
                </li>
            </xsl:for-each>
        </ol>
    </body>

</xsl:template>

<xsl:template name="auth_infos">
    <li><b>Name: </b><xsl:value-of select="name"/></li>
</xsl:template>

<xsl:template name="books_stuff">
    <xsl:if test="//book[//author/@id = @writers]"> <!-- This is supposed to make sure that if the author doesn't have any books listed in the database, then there shouldn't be an empty list of books. Only his name should be presented. -->
        <li><b>Book(s): </b>
            <xsl:for-each select="//book[//author/@id = @writers]">
                <xsl:sort select="price" order="descending"/> <!-- Because I would also like to sort them based on their price. -->
                <li><b>Title: </b><xsl:value-of select="//title"/></li>
            </xsl:for-each>
        </li>
    </xsl:if>
</xsl:template>

显然,我无法用这种特殊的XPath表达:"//book[//author/@id = @writers]"。我似乎无法理解我怎么能确保我只确认匹配当前author的ID被迭代上。此外,由于books可以有多个writersIDREFSauthor的ID),我不能确定如何应用contains操作。

其结果将是这样的:

-ID: 007
    -Name: James
    -Book(s):
        -Title: Two authors wrote me
-ID: 911
    -Name: Police
    -Book(s):
        -Title: Two authors wrote me
        -Title: Only one here
-ID: 666
    -Name: Diablo
xml xslt xpath xhtml
1个回答
1
投票

假设你限于XSLT 1.0,这将是一个有点尴尬:

XSLT 1.0

<xsl:stylesheet version="1.0"   
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    
<xsl:output method="html" encoding="utf-8"/>  

<xsl:template match="/library">    
   <body>
        <ol> 
            <xsl:for-each select="authors/author">
                <xsl:variable name="books" select="../../books/book[contains(concat(',', @writers, ','), concat(',', current()/@id, ','))]"/>
                <li>
                    <b>ID: </b>
                    <xsl:value-of select="@id"/>
                    <ul>
                        <li>
                            <b>Name: </b>
                            <xsl:value-of select="name"/>
                        </li>
                        <xsl:if test="$books">
                            <li>
                                <b>Book(s): </b>
                                <ul>
                                    <xsl:for-each select="$books">
                                        <xsl:sort select="price" data-type="number" order="descending"/> 
                                        <li><b>Title: </b><xsl:value-of select="title"/></li>
                                    </xsl:for-each>
                                </ul>
                            </li>
                        </xsl:if>
                    </ul>
                </li>
            </xsl:for-each>
        </ol>
    </body>
</xsl:template>    

</xsl:stylesheet>    

适用于你的榜样,结果S:

<body><ol>
<li>
<b>ID: </b>007<ul>
<li>
<b>Name: </b>James</li>
<li>
<b>Book(s): </b><ul><li>
<b>Title: </b>Two authors wrote me</li></ul>
</li>
</ul>
</li>
<li>
<b>ID: </b>911<ul>
<li>
<b>Name: </b>Police</li>
<li>
<b>Book(s): </b><ul>
<li>
<b>Title: </b>Two authors wrote me</li>
<li>
<b>Title: </b>Only one here</li>
</ul>
</li>
</ul>
</li>
<li>
<b>ID: </b>666<ul><li>
<b>Name: </b>Diablo</li></ul>
</li>
</ol></body>

呈现为:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.