我想了解apply-templates但我不明白为什么我不在这里写任何select ="nodename" apply-templates:(我想到My 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>
Run Code Online (Sandbox Code Playgroud)
XSLT:
<?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>
Run Code Online (Sandbox Code Playgroud)
取自w3schools教程.它如何理解应该选择哪些模板?
如规格:
在没有select属性的情况下,xsl:apply-templates指令处理当前节点的所有子节点,包括文本节点.
apply-templates没有XPath的选择,适用于在编译过程中由处理器构建,除非你明确地驱动模板的XML树视图的层次结构下面的模板(如你为title和artist).
您可能还想要考虑内置模板规则的工作原理.这些规则在幕后运行,并允许递归过程在没有成功模式匹配的情况下继续.
因此,如果您省略了根的模板匹配,那么/无论如何都要执行模板,这要归功于内置规则.
我认为处理顺序应该是这样的:
xsl:apply-templates告诉处理器将模板应用于catalog元素(在调用它的位置).catalog,因此内置规则允许继续处理其他后代元素(catalog),直到找到具有成功模式匹配的新模板(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()"/>
Run Code Online (Sandbox Code Playgroud)
在您的特定情况下,上述三个模板中的前一个模板是将模板应用于cd元素的一个模板.
每次编写显式模板时,都会覆盖这些内置模板.
例子
你可以通过替换来获得相同的:
<xsl:template match="cd">
<p>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
有:
<xsl:template match="country|company|price|year"/>
<xsl:template match="cd">
<p>
<xsl:apply-templates />
</p>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
关于root,在你的情况下,你也可以通过替换来获得相同的:
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
同
<xsl:template match="/catalog">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
或者仍然:
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates select="catalog"/>
</body>
</html>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
或者仍然:
<xsl:template match="catalog">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1871 次 |
| 最近记录: |