Xml 我如何获得这些ID';XSLT中的特定类型?

Xml 我如何获得这些ID';XSLT中的特定类型?,xml,xslt,Xml,Xslt,我是XSLT的初学者,最近才开始学习。基于下面的XML文件,我无法打印某个值以HTML格式显示。本质上,我希望XSL文件创建的HTML页面只显示幻想类型的所有图书ID,包括“bk102、bk102、bk104和bk105”。我已经尝试了一段时间,但老实说,我不知道如何做到这一点 如有任何帮助,我们将不胜感激,为您带来的麻烦深表歉意,并提前向您表示感谢 XML文件 <?xml version = "1.0"?> <?xml-stylesheet type =

我是XSLT的初学者,最近才开始学习。基于下面的XML文件,我无法打印某个值以HTML格式显示。本质上,我希望XSL文件创建的HTML页面只显示幻想类型的所有图书ID,包括“bk102、bk102、bk104和bk105”。我已经尝试了一段时间,但老实说,我不知道如何做到这一点

如有任何帮助,我们将不胜感激,为您带来的麻烦深表歉意,并提前向您表示感谢

XML文件

<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "books3.xsl"?>
<catalog>
   <book id = "bk101">
        <author> Gambardella, Matthew </author>
        <title> XML Developer's Guide </title>
        <genre> Computer </genre>
        <price> 44.95 </price>
        <publish>
            <publish_date> 2000 </publish_date>
            <publisher> Wiley </publisher>
        </publish>
        <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>
            <publish_date> 2000 </publish_date>
            <publisher> Penguin </publisher>
        </publish>
        <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>
            <publish_date> 2000 </publish_date> 
            <publisher> Penguin </publisher>
        </publish>
        <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>
            <publish_date> 2001 </publish_date> 
            <publisher> Penguin </publisher>
        </publish>
        <outOfPrint/>
        <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>
             <publish_date> 2002 </publish_date>    
             <publisher> Penguin </publisher>
         </publish>
         <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>
             <publish_date> 2000 </publish_date>    
             <publisher> Whitehouse </publisher>
         </publish>
         <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>
             <publish_date> 2000 </publish_date>    
             <publisher> Whitehouse </publisher>
         </publish>
         <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>
             <publish_date> 2000 </publish_date>    
         <publisher> Wiley </publisher>
         </publish>
         <outOfPrint/>
         <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>
             <publish_date> 2000 </publish_date>    
             <publisher> Penguin </publisher>
         </publish>
         <description> After an inadvertant trip through a HeisenbergUncertainty 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>
             <publish_date> 2000 </publish_date>    
             <publisher> Penguin </publisher>
         </publish>
         <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>
             <publish_date> 2000 </publish_date>    
             <publisher> Penguin </publisher>
         </publish>
         <outOfPrint/>
         <description> The Microsoft MSXML3 parser is covered in detail, with attention to XML DOM interfaces, XSLT processing, SAX and more. </description>
   </book>
   <book id = "bk112">
         <author> Galos, Mike </author>
         <title> Visual Studio 7: A Comprehensive Guide </title>
         <genre> Computer </genre>
         <price> 49.95 </price>
         <publish>
            <publish_date> 2001 </publish_date> 
            <publisher> Wiley </publisher>
         </publish>
         <description> Microsoft Visual Studio 7 is explored in depth,looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment. </description>
   </book>
</catalog>

马修·甘巴德拉
XML开发人员指南
计算机
44.95
2000
威利
深入了解如何使用XML创建应用程序。
拉尔斯,金
夜雨
幻想
5.95
2000
企鹅
一位前建筑师与企业僵尸、邪恶女巫以及她自己的童年斗争,成为世界女王。
科雷茨,伊娃
梅夫上升
幻想
5.95
2000
企鹅
在英国的纳米技术社会崩溃后,年轻的幸存者为新的社会奠定了基础。
科雷茨,伊娃
奥伯伦的遗产
幻想
5.95
2001
企鹅
在《后启示录》中的英格兰,神秘的特工奥伯伦帮助伦敦居民创造了新的生活。《玛芙上升》的续集。
科雷茨,伊娃
破碎的圣杯
幻想
5.95
2002
企鹅
梅芙的两个女儿,同父异母的姐妹,为了争夺英格兰的控制权而互相争斗。奥伯伦遗产的续集。
兰德尔,辛西娅
情人鸟
浪漫
4.95
2000
白宫
卡拉在一次鸟类学会议上遇到保罗时,脾气暴躁,因为羽毛被激怒了。
瑟曼,保拉
飞溅
浪漫
4.95
2000
白宫
一个深海潜水员在海底两万法里找到真爱。
克诺尔,斯特凡
令人毛骨悚然的爬虫
恐怖
4.95
2000
威利
关于蟑螂、蜈蚣、蝎子和其他昆虫的恐怖故事选集。
克雷斯,彼得
悖论丢失
科幻小说
6.95
2000
企鹅
詹姆斯·萨尔韦在无意中通过海森伯根装置后发现了量子存在的问题。
蒂姆·奥布莱恩
微软.NET:编程圣经
计算机
36.95
2000
企鹅
微软的.NET计划将在本深度程序员参考中详细探讨。
蒂姆·奥布莱恩
MSXML3:综合指南
计算机
36.95
2000
企鹅
本文将详细介绍MicrosoftMSXML3解析器,重点介绍XMLDOM接口、XSLT处理、SAX等。
加洛斯,迈克
VisualStudio7:综合指南
计算机
49.95
2001
威利
深入研究微软Visual Studio 7,了解Visual Basic、Visual C++、C语言和ASP+如何集成到一个全面的开发环境中。
XSLT文件

<?xml version = "1.0"?>
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0">
<xsl:output method = "html"/>
<xsl:template match = "/">
<html>
    <body>
        <xsl:for-each select = "catalog/book">

        </xsl:for-each>
    </body>
</html>
</xsl:template>
</xsl:stylesheet>

最简单的方法是仅使用此模板复制给定元素:

<xsl:template match = "/">
    <html>
        <body>
            <xsl:copy-of select = "catalog/book[normalize-space(genre)='Fantasy']" />
        </body>
    </html>
</xsl:template>
<xsl:template match = "/">
    <html>
        <body>
            <xsl:apply-templates select = "catalog/book[normalize-space(genre)='Fantasy']" />
        </body>
    </html>
</xsl:template>

<xsl:template match = "book">
    <xsl:copy>
        <!-- Simply copy or replace with your own logic -->
        <xsl:copy-of select="node() | @*" />
    </xsl:copy>
</xsl:template>

但是,如果要对
元素执行更多操作,最好使用单独的模板:

<xsl:template match = "/">
    <html>
        <body>
            <xsl:copy-of select = "catalog/book[normalize-space(genre)='Fantasy']" />
        </body>
    </html>
</xsl:template>
<xsl:template match = "/">
    <html>
        <body>
            <xsl:apply-templates select = "catalog/book[normalize-space(genre)='Fantasy']" />
        </body>
    </html>
</xsl:template>

<xsl:template match = "book">
    <xsl:copy>
        <!-- Simply copy or replace with your own logic -->
        <xsl:copy-of select="node() | @*" />
    </xsl:copy>
</xsl:template>

对于上面的示例XML,结果保持不变


[…]打印幻想类书籍的ID

这可以通过以下样式表/模板实现:

<xsl:template match = "/">
    <html>
        <body>
            <xsl:copy-of select = "catalog/book[normalize-space(genre)='Fantasy']" />
        </body>
    </html>
</xsl:template>
<xsl:template match = "/">
    <html>
        <body>
            <xsl:apply-templates select = "catalog/book[normalize-space(genre)='Fantasy']" />
        </body>
    </html>
</xsl:template>

<xsl:template match = "book">
    <xsl:copy>
        <!-- Simply copy or replace with your own logic -->
        <xsl:copy-of select="node() | @*" />
    </xsl:copy>
</xsl:template>
<?xml version = "1.0"?>
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0">
<xsl:output method = "text"/>

    <xsl:template match = "/">
        <xsl:for-each select = "catalog/book[normalize-space(genre)='Fantasy']">
            <xsl:value-of select = "concat(@id,' ')" />
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

它的产量是


bk102 bk103 bk104 bk105


上面的输出方法是“text”,因此它只作为结果输出文本。

我猜您希望执行以下操作:

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

<xsl:template match="/catalog">
    <html>
        <body>
            <xsl:for-each select="book[normalize-space(genre)='Fantasy']">
                <p>
                    <xsl:value-of select="@id" />
                </p>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

</xsl:stylesheet>



请显示预期的输出。我想输出到一个HTML文件或浏览器,显示以下内容:bk102 bk103 bk104 BK105所以只需打印幻想版本书籍的ID即可。您的输出方法是HTML。请以HTML代码的形式给出问题的准确预期输出。感谢您的回复,虽然结果是显示幻想书的ID值,而不仅仅是类型本身,但我感谢您的反馈:)@Pope052:我在回答中添加了另一种方法。这两种方法都没有任何意义。如果输出方法是
html
,则不应创建
book
元素。如果输出方法是
text
,那么为什么要创建任何元素;它们无论如何都不会出现在输出中。这正是我想要的,谢谢你澄清:)