XSL:for-each不工作

时间:2014-12-17 23:05:00

标签: xml xslt

因此,我尝试过滤一系列图书,以便只显示特定类别的图书。我已经删除了我复制并粘贴的http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_filter并且只是更改了变量,因为它不起作用,但它仍然无法正常工作。问题是这一行:

<xsl:for-each select="Catalog/Book[bookCategory='database']">

如果我更改它,那么它有一个&#34; /&#34;在目录前,它可以工作,但它保持循环,我会有一长串相同的书重复:

<xsl:for-each select="/Catalog/Book[bookCategory='database']">

如果有人能弄清楚为什么那条线不起作用,我会很感激。

XML(为了理智而缩短版本,它通常很长):

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="HTMLfilter.xsl" type="text/xsl"?>
<Catalog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Users\Sean\Documents\XMLCLA~1\FINALP~1\CATALO~1.XSD">
    <Book>
        <bookID>0-07-882122-3</bookID>
        <bookTitle>Oracle: A Beginner's Guide</bookTitle>
        <bookCategory>database</bookCategory>
        <bookDescription>A beginner's guide to the complex and powerful Oracle database management system. Teaches you how to set up, query and manage your database, including principles of database design, how to manage data, generate reports and tune the system for optimal performance.</bookDescription>
        <bookPrice>30.00</bookPrice>
        <bookAuthor>Michael Abbey</bookAuthor>
        <bookImage>images/Oracle.JPG</bookImage>
        <bookInfo>Oracle.xml</bookInfo>
    </Book>
    <Book>
        <bookID>0-201-43787-7</bookID>
        <bookTitle>Creating Killer Interactive Web Sites : The Art of Integrating Interactivity and Design</bookTitle>
        <bookCategory>web</bookCategory>
        <bookDescription>Creating Killer Interactive Sites is the one-of-a-kind look into the secrets of one of the world's most preeminent Web design firms. Its highly visual, inspiring approach brings a new standard to integrating design and interactivity to create successful sites. The book's companion Web site includes all the code and examples from the book, valuable tools and utilities, plus updated new, information, and tips.</bookDescription>
        <bookPrice>39.99</bookPrice>
        <bookAuthor>Deke McClelland</bookAuthor>
        <bookImage>images/KillerInteractiveSites.gif</bookImage>
        <bookInfo>CreatingKiller.xml</bookInfo>
    </Book>
    <Book>
        <bookID>0-201-44787-8</bookID>
        <bookTitle>The Practical SQL Handbook</bookTitle>
        <bookCategory>database</bookCategory>
        <bookDescription>The best-selling guide to learning SQL, the standard language for accessing information in relational databases. Teaches SQL as it's really used, with step-by-step lessons in the basic vocabulary and functions of the language and the processes and issues involved in developing robust applications.</bookDescription>
        <bookPrice>40.12</bookPrice>
        <bookAuthor>Judith Bowman</bookAuthor>
        <bookImage>images/PracticalSQL.JPG</bookImage>
        <bookInfo>PracticalSQL.xml</bookInfo>
    </Book>
    <Book>
        <bookID>0-672-30956-4</bookID>
        <bookTitle>SQL Server 6.5 Second Edition</bookTitle>
        <bookCategory>database</bookCategory>
        <bookDescription>Comprehensive guide to Microsoft SQL Server 6.5, including the latest enhancements such as Distributed Transaction Coordinator (DTC), replication to ODBC clients, SQL Enterprise Manager and SQL Web Assistant, SQL language enhancements.</bookDescription>
        <bookPrice>60.00</bookPrice>
        <bookAuthor>David Solomon</bookAuthor>
        <bookImage>images/SQLUnleashed.JPG</bookImage>
        <bookInfo>SQLServer.xml</bookInfo>
    </Book>
    <Book>
        <bookID>0-7645-4032-7</bookID>
        <bookTitle>Photoshop 4 for Windows 95 Bible</bookTitle>
        <bookCategory>graphics</bookCategory>
        <bookDescription>Comprehensive reference for Photoshop users includes step-by-step guides to everything from working wonders with layers to customizing graphics for the World Wide Web.</bookDescription>
        <bookPrice>50.23</bookPrice>
        <bookAuthor>Deke McClelland</bookAuthor>
        <bookImage>images/PS4Bible.JPG</bookImage>
        <bookInfo>Photoshop4.xml</bookInfo>
    </Book>
    <Book>
        <bookID>1-26592-234-2</bookID>
        <bookTitle>Web Page Design : A Different Multimedia</bookTitle>
        <bookCategory>web</bookCategory>
        <bookDescription>Revealing the crucial differences between an ordinary Web page and an effective site, bestselling Internet author Mary E. S. Morris and co-author Randy J. Hinrichs go beyond the basics of web creation to show readers how to tackle the crucial problems of information overload at your Web site, getting lost in cyberspace, bandwidth constraints required to hold a user's attention, and more.</bookDescription>
        <bookPrice>19.96</bookPrice>
        <bookAuthor>David Flanagan</bookAuthor>
        <bookImage>images/WebPageDesign.gif</bookImage>
        <bookInfo>WebPageDesign.xml</bookInfo>
    </Book>
    <Book>
        <bookID>1-549514-32-1</bookID>
        <bookTitle>Information Architecture for the World Wide Web</bookTitle>
        <bookCategory>web</bookCategory>
        <bookDescription>With a swift and convincing stroke, the authors of Information Architecture for the World Wide Web tear down many entrenched ideas about Web design. Flashy animations are cool, they agree, as long as they don't irritate the viewer. Nifty clickable icons are nice, but are their meanings universal? Is the search engine providing results that are useful and relevant? This book acts as a mirror and with careful questioning causes the reader to think through all the elements and decisions required for well-crafted Web design.</bookDescription>
        <bookPrice>19.34</bookPrice>
        <bookAuthor>Louis Rosenfeld</bookAuthor>
        <bookImage>images/InformationArchitecture.gif</bookImage>
        <bookInfo>InformationArchitecture.xml</bookInfo>
    </Book>
    <Book>
        <bookID>1-56204-223-1</bookID>
        <bookTitle>The Non-Designer's Web Book : An Easy Guide to Creating, Designing, and Posting Your Own Web Site</bookTitle>
        <bookCategory>web</bookCategory>
        <bookDescription>The Non-Designer's Web Book is a pretty, full-color guide for aspiring Web designers. The authors first explain how to browse and search the Web and then discuss how to plan and post a Web site. Then they get you into the real work of designing Web sites, whether for business or personal purposes. The authors teach you basic design principles--covering such areas as alignment, proximity, repetition, and contrast--and then discuss design issues that are specific to the Web: You learn about creating pages with careful attention to color, graphics, typography, tables, and more. Finally, you learn how to test, fix, upload, update, and register your site. The book isn't an HTML primer, but you do get a few tips on tweaking your pages by editing HTML code.</bookDescription>
        <bookPrice>23.96</bookPrice>
        <bookAuthor>Robin Williams</bookAuthor>
        <bookImage>images/NonDesignerWebBook.gif</bookImage>
        <bookInfo>NonDesigner.xml</bookInfo>
    </Book>
    <Book>
        <bookID>1-56542-264-4</bookID>
        <bookTitle>Elements of Web Design : The Designer's Guide to a New Medium</bookTitle>
        <bookCategory>graphics</bookCategory>
        <bookDescription>Elements of Web Design helps designers learn how the experts plan and design their Web sites. It"s ideal for readers who haven"t yet learned what the Web is all about, but who'd like to start applying their design and management skills to Web-site production. The book provides a history of the Web, examples of well-designed sites, and information on coding HTML and preparing Web graphics.</bookDescription>
        <bookPrice>31.96</bookPrice>
        <bookAuthor>Michael Groh</bookAuthor>
        <bookImage>images/ElementsOfWebDesign.gif</bookImage>
        <bookInfo>ElementsofWebDesign.xml</bookInfo>
    </Book>
    <Book>
        <bookID>1-56592-234-4</bookID>
        <bookTitle>JavaScript: The Definitive Guide</bookTitle>
        <bookCategory>programming</bookCategory>
        <bookDescription>A thorough description of the JavaScript programming language, complete with sophisticated programming examples, as well as a definitive, in-depth reference section covering each JavaScript function, object method, property and event handler.</bookDescription>
        <bookPrice>33.23</bookPrice>
        <bookAuthor>David Flanagan</bookAuthor>
        <bookImage>images/JavaScript.JPG</bookImage>
        <bookInfo>Javascript.xml</bookInfo>
    </Book>
    <Book>
        <bookID>1-56604-286-0</bookID>
        <bookTitle>The Visual Guide to Microsoft Access for Windows 95</bookTitle>
        <bookCategory>database</bookCategory>
        <bookDescription>A graphical approach to using Microsoft Access 95 to build, customize and manage databases.</bookDescription>
        <bookPrice>35.36</bookPrice>
        <bookAuthor>Michael Groh</bookAuthor>
        <bookImage>images/MSAccess.JPG</bookImage>
        <bookInfo>MicrosoftAccess95.xml</bookInfo>
    </Book>
</Catalog>

完整的XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="Catalog/Book">
        <xsl:for-each select="Catalog/Book[bookCategory='database']">
            <h3>
                <xsl:text>Title: </xsl:text>
                <xsl:value-of select="bookTitle"/>
            </h3>
            <img alt="">
                <xsl:attribute name="src"><xsl:value-of select="bookImage"/></xsl:attribute>
            </img>
            <div>
                <xsl:text>Author: </xsl:text>
                <xsl:value-of select="bookAuthor"/>
            </div>
            <div>
                <xsl:text>ISBN: </xsl:text>
                <xsl:value-of select="bookID"/>
            </div>
            <div>
                <a>
                    <xsl:attribute name="href"><xsl:value-of select="bookInfo"/></xsl:attribute>
            Click here for details
            </a>
            </div>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="/">
        <html>
            <head>
                <link rel="stylesheet" href="catalog.css"/>
                <h4>Filter by Category: </h4>
                <A href="HTMLcategory.xml">HTML</A>  |  
                <A href="nonexist.xml">Javascript</A>  |  
                <A href="nonexist.xml">Database</A>  |  
                <A href="nonexist.xml">Graphics</A>  |  
                <A href="nonexist.xml">Programming</A>  |  
                <A href="nonexist.xml">Web</A>
            </head>
            <body>
                <xsl:apply-templates select="Catalog/Book"/>
            </body>
            <br>
                <A href="allBooks.xml">Back</A>
            </br>
        </html>
    </xsl:template>
</xsl:stylesheet>

如果有人发现问题,请告诉我!

谢谢!

3 个答案:

答案 0 :(得分:1)

模板本身,

<xsl:template match="Catalog/Book">

已经选择了所有Book元素。你放在“里面”的所有内容都与XPath相关。将/放在Catalog前面会使其全局读取 ,每本书一次。这就是为什么你什么也得不到,或者为每本书重复整个清单。

这里你需要的只是每本书的单一测试:

<xsl:for-each select=".[bookCategory='database']">

但是,既然(或应该)只有一个,你也可以使用简单的if而不是<xsl:for-each>

<xsl:if test="bookCategory='database'">

但是,您应该避免这样的测试,并且只在必要时调用Book的模板,方法是删除for-each/if(因此它总是写出其输出)。将模板匹配本身更改为

<xsl:template match="Book">

并在/的模板中更改行

<xsl:apply-templates select="Catalog/Book"/>

<xsl:apply-templates select="Catalog/Book[bookCategory='database']"/>

通过这种方式,您可以轻松地在此一行下方添加更多行以添加其他类别,所有这些调用都将紧密相连,您可以仅在一个位置调整每个Book的模板。< / p>

答案 1 :(得分:1)

您的<xsl:template match="Catalog/Book">与每本书相匹配。

如果在该模板中,则使用

<xsl:for-each select="Catalog/Book[bookCategory='database']">

无法找到,因为选择是相对于当前位置的(即 当前/目录/书籍下的东西)并且没有。

使用

<xsl:for-each select="/Catalog/Book[bookCategory='database']">

您正在查找指定数据库的所有书籍,但是从根目录开始,而不考虑您在文档中的位置,并且在重复匹配模板时再次找到它们。

@Jongware的答案给出了一个可能的解决方案。

另一种方法是更改​​您的模板,并为每个人进行合理的协作:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="Catalog">
        <xsl:for-each select="Book[bookCategory='database']">
            <h3>
                <xsl:text>Title: </xsl:text>
                <xsl:value-of select="bookTitle"/>
            </h3>
            <img alt="">
                <xsl:attribute name="src"><xsl:value-of select="bookImage"/></xsl:attribute>
            </img>
            <div>
                <xsl:text>Author: </xsl:text>
                <xsl:value-of select="bookAuthor"/>
            </div>
            <div>
                <xsl:text>ISBN: </xsl:text>
                <xsl:value-of select="bookID"/>
            </div>
            <div>
                <a>
                    <xsl:attribute name="href"><xsl:value-of select="bookInfo"/></xsl:attribute>
            Click here for details
            </a>
            </div>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="/">
        <html>
            <head>
                <link rel="stylesheet" href="catalog.css"/>
                <h4>Filter by Category: </h4>
                <A href="HTMLcategory.xml">HTML</A>  |  
                <A href="nonexist.xml">Javascript</A>  |  
                <A href="nonexist.xml">Database</A>  |  
                <A href="nonexist.xml">Graphics</A>  |  
                <A href="nonexist.xml">Programming</A>  |  
                <A href="nonexist.xml">Web</A>
            </head>
            <body>
                <xsl:apply-templates select="Catalog/Book"/>
            </body>
            <br>
                <A href="allBooks.xml">Back</A>
            </br>

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

答案 2 :(得分:0)

XSLT的一个更惯用的解决方案:

<!-- Books with the right category have their contents formatted as HTML -->
<xsl:template match="Book[bookCategory='database']" priority="1">
      <h3>
            <xsl:text>Title: </xsl:text>
            <xsl:value-of select="bookTitle"/>
        </h3>
        <img alt="">
            <xsl:attribute name="src"><xsl:value-of select="bookImage"/></xsl:attribute>
        </img>
        <div>
            <xsl:text>Author: </xsl:text>
            <xsl:value-of select="bookAuthor"/>
        </div>
        <div>
            <xsl:text>ISBN: </xsl:text>
            <xsl:value-of select="bookID"/>
        </div>
        <div>
            <a>
                <xsl:attribute name="href"><xsl:value-of select="bookInfo"/></xsl:attribute>
        Click here for details
        </a>
        </div>

</xsl:template>

<!-- Other Books produce no output, and thus are discarded. -->
<xsl:template match="Book"/>

使用这两个模板,您可以让XSLT的模板匹配为您完成所有选择工作。 (优先级调整可确保具有正确类别的图书与该模板匹配,而不是捕获其他所有内容的更一般的图书。)

XSLT是一种声明性的,非过程的模式匹配语言。如果您学会根据模板编写代码并应用而非应用显式循环,那么您将获得最高效率,并从中获得最大价值。

如果您真的坚持为这两种情况设置一个模板,那么@Jongware的答案是正确的 - 只需使用&#39; xsl:if&#39;,然后将其应用到当前节点(& #39;&#39;。)

请注意,如果您从头开始设计此项,我建议您将大部分的儿童元素转换为属性。如果该值不具有进一步的子结构,则属性通常更易于使用以及更紧凑。