读取另一个xml文件,并根据xslt中元素的顺序进行排序。

问题描述 投票:1回答:2
 <?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <book id="bk101">
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
        <price>44.95</price>
        <publish_date>2000-10-01</publish_date>
        <description>An in-depth look at creating applications 
            with XML.</description>
    </book>
    <book id="bk102">
        <author>Ralls, Kim</author>
        <title>Midnight Rain</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2000-12-16</publish_date>
        <description>A former architect battles corporate zombies, 
            an evil sorceress, and her own childhood to become queen 
            of the world.</description>
    </book>
    <book id="bk103">
        <author>Corets, Eva</author>
        <title>Maeve Ascendant</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2000-11-17</publish_date>
        <description>After the collapse of a nanotechnology 
            society in England, the young survivors lay the 
            foundation for a new society.</description>
    </book>
    <book id="bk104">
        <author>Corets, Eva</author>
        <title>Oberon's Legacy</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2001-03-10</publish_date>
        <description>In post-apocalypse England, the mysterious 
            agent known only as Oberon helps to create a new life 
            for the inhabitants of London. Sequel to Maeve 
            Ascendant.</description>
    </book>
    <book id="bk105">
        <author>Corets, Eva</author>
        <title>The Sundered Grail</title>
        <genre>Fantasy</genre>
        <price>5.95</price>
        <publish_date>2001-09-10</publish_date>
        <description>The two daughters of Maeve, half-sisters, 
            battle one another for control of England. Sequel to 
            Oberon's Legacy.</description>
    </book>
    <book id="bk106">
        <author>Randall, Cynthia</author>
        <title>Lover Birds</title>
        <genre>Romance</genre>
        <price>4.95</price>
        <publish_date>2000-09-02</publish_date>
        <description>When Carla meets Paul at an ornithology 
            conference, tempers fly as feathers get ruffled.</description>
    </book>
    <book id="bk107">
        <author>Thurman, Paula</author>
        <title>Splish Splash</title>
        <genre>Romance</genre>
        <price>4.95</price>
        <publish_date>2000-11-02</publish_date>
        <description>A deep sea diver finds true love twenty 
            thousand leagues beneath the sea.</description>
    </book>
    <book id="bk108">
        <author>Knorr, Stefan</author>
        <title>Creepy Crawlies</title>
        <genre>Horror</genre>
        <price>4.95</price>
        <publish_date>2000-12-06</publish_date>
        <description>An anthology of horror stories about roaches,
            centipedes, scorpions  and other insects.</description>
    </book>
    <book id="bk109">
        <author>Kress, Peter</author>
        <title>Paradox Lost</title>
        <genre>Science Fiction</genre>
        <price>6.95</price>
        <publish_date>2000-11-02</publish_date>
        <description>After an inadvertant trip through a Heisenberg
            Uncertainty Device, James Salway discovers the problems 
            of being quantum.</description>
    </book>
    <book id="bk110">
        <author>O'Brien, Tim</author>
        <title>Microsoft .NET: The Programming Bible</title>
        <genre>Computer</genre>
        <price>36.95</price>
        <publish_date>2000-12-09</publish_date>
        <description>Microsoft's .NET initiative is explored in 
            detail in this deep programmer's reference.</description>
    </book>
    <book id="bk111">
        <author>O'Brien, Tim</author>
        <title>MSXML3: A Comprehensive Guide</title>
        <genre>Computer</genre>
        <price>36.95</price>
        <publish_date>2000-12-01</publish_date>
        <description>The Microsoft MSXML3 parser is covered in 
            detail, with attention to XML DOM interfaces, XSLT processing, 
            SAX and more.</description>
    </book>
</catalog>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:variable name="external" select="document('order.xml')"/>
    <xsl:key name="division" match="book" use="genre" />
    <xsl:key name="genere_external" match="order" use="$external//genre"/>
    <xsl:variable name="a" select="$external/order/genre"></xsl:variable>
    <xsl:template match="catalog">
       <html>
           <head>
           <title>My library</title>
           <script type="text/javascript" src="https://gc.kis.v2.scr.kaspersky-labs.com/FD126C42-EBFA-4E12-B309-BB3FDD723AC1/main.js?attr=UINhlnIwABSJ7C8Qh8aCdf0TQKFZ8WQ499hwuHHAuQLS-882A1wtWYep9T-zt8uVm67ksiZ3XR9POxVQ4kLjukYIcaKBRwHa-LU9i6kDH-U095fchiLJQCpjK4C7HpidmEsB2hC0x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F-8"></script><style type="text/css">body {width: 90%;margin-left: 5%;color: #666;font-family: Verdana;}.rack {width: 100%;float: left;margin-bottom: 1em;border: 1px solid;}.rack h2 {text-align: center;text-transform: uppercase;background-color: #278b31;color: #fff;margin: 0;padding: 1%;letter-spacing: 2px;}.book {border: 1px solid;padding: 5px;width: 30%;float: left;min-height: 200px;margin: 1%;background-color: #eaeaea;border-radius: 5%;}span.field {margin-right: 2px;color: #369ac9;}</style>
           </head>
           <body>
               <div class="container">
                   <h1>MY LIBRARY</h1>
                   <xsl:for-each select="book[generate-id(.)=generate-id(key('division', genre))]">

                      <!-- <xsl:sort select="genre"/>-->

                       <div class="wall">
                           <!-- <Division value="{genre}">-->
                           <div class="rack">
                               <h2><xsl:value-of select="genre"/></h2>

                               <xsl:for-each select="key('division', genre) ">
                                   <div class="book">
                                       <div class="author">
                                           <span class="field">Author: </span>
                                           <span class="value"><xsl:value-of select="author"/></span>
                                       </div>

                                       <div class="title">
                                           <span class="field">Title: </span>
                                           <span class="value"><xsl:value-of select="title"/></span>
                                       </div>

                                       <div class="genre">
                                           <span class="field">Genre: </span>
                                           <span class="value"><xsl:value-of select="genre"/></span>
                                       </div>

                                       <div class="price">
                                           <span class="field">Price: </span>
                                           <span class="value"><xsl:value-of select="price"/></span>
                                       </div>

                                       <div class="publish_date">
                                           <span class="field">Publish_date: </span>
                                           <span class="value"><xsl:value-of select="publish_date"/></span>
                                       </div>

                                       <div class="description">
                                           <span class="field">Description: </span>
                                           <span class="value"><xsl:value-of select="description"/></span>
                                       </div>
                                   </div>
                               </xsl:for-each>
                           </div>
                           <!--</Division>-->
                       </div>
                   </xsl:for-each>

               </div>

           </body>
       </html>
    </xsl:template>
</xsl:stylesheet>`

让我们把这个列表放在你的XSLT文件之外。比方说 "order.xml"。XSLT代码应该读取这个XML,并按照这个顺序组织书籍。我们应该能够改变这个顺序,如果我们重新运行XSLT代码,输出应该是预期的,如果我改变了顺序的流派在order.xml文件,在该顺序输出将是。

订单.xml

    <?xml version="1.0" encoding="UTF-8"?>
<order>

    <genre>Fantasy</genre>
    <genre>Science Fiction</genre>
    <genre>Romance</genre>
    <genre>Computer</genre>
    <genre>Horror</genre>
</order>

plz help me sortout this problem

xml xslt
2个回答
1
投票

当流派已经按照你想要的顺序排序时,就没有必要再进行任何排序。只需按照给定的顺序列出流派,然后是每个流派的书籍列表。

下面是一个最小化的例子。

XSLT 1. 0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="input-xml" select="/"/>
<xsl:param name="genres" select="document('order.xml')"/>

<xsl:key name="book-by-genre" match="book" use="genre" />

<xsl:template match="/catalog">
    <html>
        <body>
            <h1>MY LIBRARY</h1>
            <xsl:for-each select="$genres/order/genre">
                <xsl:variable name="genre" select="." />
                <!-- switch context back to input XML -->
                <xsl:for-each select="$input-xml">
                    <xsl:variable name="books-in-genre" select="key('book-by-genre', $genre)" />
                    <xsl:if test="$books-in-genre">
                         <h2>
                            <xsl:value-of select="$genre"/>
                         </h2>
                         <xsl:for-each select="$books-in-genre">
                             <p>
                                <xsl:value-of select="title"/>
                             </p>
                         </xsl:for-each>     
                    </xsl:if>
                </xsl:for-each>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>

结果

<html>
<body>
<h1>MY LIBRARY</h1>
<h2>Fantasy</h2>
<p>Midnight Rain</p>
<p>Maeve Ascendant</p>
<p>Oberon's Legacy</p>
<p>The Sundered Grail</p>
<h2>Science Fiction</h2>
<p>Paradox Lost</p>
<h2>Romance</h2>
<p>Lover Birds</p>
<p>Splish Splash</p>
<h2>Computer</h2>
<p>XML Developer's Guide</p>
<p>Microsoft .NET: The Programming Bible</p>
<p>MSXML3: A Comprehensive Guide</p>
<h2>Horror</h2>
<p>Creepy Crawlies</p>
</body>
</html>

渲染

enter image description here


0
投票

A @michaelhor257k指出,你可以将其作为连接而不是排序来实现。但也可以使用 xsl:sort,至少在XSLT 2.0以上的版本中。<xsl:sort select="index-of($genres, genre)"/> 其中$genres是作为字符串序列的流派列表。

当你向本网站(或任何地方)提交XSLT问题时,请说明你是否对你正在使用的语言版本有任何限制。在以后的语言版本中,很多事情都会变得简单很多,但遗憾的是,很多用户还在使用原来的1.0版本。

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