Xml 为什么在第一个应用模板中没有选择
我试图理解Xml 为什么在第一个应用模板中没有选择,xml,xslt,Xml,Xslt,我试图理解apply templates,但我不明白为什么我不在apply templates中编写任何select=“nodename”:(我想到了我的CD收藏下面的第一个apply templates) 输入文档的片段: <?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cd> <title>Empire Burlesque</title>
apply templates
,但我不明白为什么我不在apply templates
中编写任何select=“nodename”:(我想到了我的CD收藏下面的第一个apply templates)
输入文档的片段:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>
皇帝讽刺剧
鲍勃·迪伦
美国
哥伦比亚
10.90
1985
隐藏你的心
邦尼泰勒
英国
哥伦比亚唱片公司
9.90
1988
最成功的
多莉·帕顿
美国
RCA
9.90
1982
XSLT:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
<xsl:template match="artist">
Artist: <span style="color:#00ff00">
<xsl:value-of select="."/></span>
<br />
</xsl:template>
</xsl:stylesheet>
我的CD收藏
标题:
艺术家:
摘自w3schools教程。它如何理解应该选择哪些模板?根据规范:
如果没有select属性,xsl:apply templates指令将处理当前节点的所有子节点,包括文本节点
apply templates
如果不选择XPath,则在编译过程中按照处理器构建的XML树视图的层次结构应用模板,除非您显式驱动模板(与title
和artist
一样)
你可能还想想想如何工作。这些规则在后台运行,允许在没有成功模式匹配的情况下进行递归过程
因此,如果您忽略根/
的模板匹配,由于内置规则,您的模板将被执行
我认为处理顺序应该是这样的:
- 模板匹配根,并且
告诉处理器将模板应用到xsl:apply templates
元素(在调用它的位置)目录
- 未找到与
匹配的模板,因此内置规则允许继续处理其他子元素(目录
),直到找到具有成功模式匹配的新模板(目录
)cd
<xsl:template match="*|/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="processing-instruction()|comment()"/>
与:
关于根,在您的情况下,您也可以通过替换:
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
我的CD收藏
与
我的CD收藏
还是:
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates select="catalog"/>
</body>
</html>
</xsl:template>
<xsl:template match="catalog">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
我的CD收藏
还是:
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates select="catalog"/>
</body>
</html>
</xsl:template>
<xsl:template match="catalog">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
我的CD收藏
您可以通过添加输入文档来编辑您的答案吗;DR:“apply templates
是一个应用于树的所有节点的内部递归过程(除非显式驱动)。”英文版:apply templates
基本上是说,“将/应用包含的xsl:template
的XML节点match
下的其余内容;遍历它的XML子元素,并在这里为所有元素应用XSL。”@empo:谢谢,但是“/”是否等于catalog而不是document root?如果我想要元素根,我想我必须写“/catalog”,但这部分与“normal”不同吗“xpath?我可能会把事情搞混了…@Andreas:不,/
是文档根,而不是目录。对你来说没什么区别。看看我的答案。@empo:对不起,我还没有完全理解。我先写一个匹配“/”的模板,对吗?这是文档根目录。然后,我编写应用模板,而不选择处理当前节点的所有子节点,“/”对吗?我看不出它是如何做cd的。@Andreas:“我看不出它是如何做cd的。”这是因为apply templates
是一个应用于树的所有节点的内部递归过程(除非显式驱动)。@empo:感谢您的帮助。因此,为了检查我是否正确理解它,它的工作原理如下:模板匹配“/”,然后它尝试匹配目录,但它没有显式模板,因此它使用其内置模板并将模板应用于查找CD的子项,对吗?