Xml 如何应用XSLT模板规则
假设我有以下XML: XMLXml 如何应用XSLT模板规则,xml,xslt,xslt-1.0,xslt-2.0,Xml,Xslt,Xslt 1.0,Xslt 2.0,假设我有以下XML: XML <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> <
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
日常意大利语
吉娅达·德·劳伦蒂斯
2005
30
哈利·波特
J K.罗琳
2005
29.99
XQuery启动
詹姆斯·麦戈文
伯特纳
库尔特·卡格尔
詹姆斯·林恩
瓦迪亚纳坦·纳加拉扬
2003
49.99
学习XML
埃里克·T·雷
2003
39.95
以及以下XSLT:
XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
exclude-result-prefixes="fo xs fn msxsl">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<xsl:element name="rootnode">
<xsl:choose>
<xsl:when test="//bookstore/book[year!=2005]">
<xsl:message select="'book exists'" />
<xsl:apply-templates></xsl:apply-templates>
</xsl:when>
</xsl:choose>
</xsl:element>
</xsl:template>
<xsl:template match="book[year!=2005]">
<xsl:element name="not2005">
<xsl:copy-of select="." />
</xsl:element>
<!--<xsl:apply-templates select="@* | node()" />-->
</xsl:template>
</xsl:stylesheet>
转换后的xml如下所示:
结果
<?xml version="1.0" encoding="UTF-8"?>
<rootnode>Everyday ItalianGiada De Laurentiis200530.00Harry PotterJ K. Rowling200529.99<not2005>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
</not2005>
<not2005>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</not2005>
</rootnode>
<?xml version="1.0" encoding="UTF-8"?>
<rootnode>
<not2005>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
</not2005>
<not2005>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</not2005>
</rootnode>
每日意大利语Giada De Laurentiis 200530.00 Harry PotterJ K.Rowling200529.99
XQuery启动
詹姆斯·麦戈文
伯特纳
库尔特·卡格尔
詹姆斯·林恩
瓦迪亚纳坦·纳加拉扬
2003
49.99
学习XML
埃里克·T·雷
2003
39.95
而不是我所期望的:
预期结果
<?xml version="1.0" encoding="UTF-8"?>
<rootnode>Everyday ItalianGiada De Laurentiis200530.00Harry PotterJ K. Rowling200529.99<not2005>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
</not2005>
<not2005>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</not2005>
</rootnode>
<?xml version="1.0" encoding="UTF-8"?>
<rootnode>
<not2005>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
</not2005>
<not2005>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</not2005>
</rootnode>
XQuery启动
詹姆斯·麦戈文
伯特纳
库尔特·卡格尔
詹姆斯·林恩
瓦迪亚纳坦·纳加拉扬
2003
49.99
学习XML
埃里克·T·雷
2003
39.95
为什么要转换/拾取以下内容
每日意大利语吉阿达·德·劳伦蒂斯200530.00哈里·波特J K。
罗琳200529.99
还想知道为什么我的xsl:message没有出现
<xsl:message select="'book exists'" />
任何解释我明显缺少的基本概念的文章指针都将被感激…更改
<xsl:choose>
<xsl:when test="//bookstore/book[year!=2005]">
<xsl:message select="'book exists'" />
<xsl:apply-templates></xsl:apply-templates>
</xsl:when>
</xsl:choose>
到
您当前的代码仅在依赖内置模板时才起作用,但它们会输出未显式匹配的元素的任何文本节点。祝贺您:与这里的一半人不同,您不仅希望剪切和粘贴示例代码,还希望理解概念。我写这本书时考虑到了像你这样的人:来自Wiley的XSLT 2.0/XPath 2.0程序员参考。@MichaelKay感谢Michael说的这些好话