为什么第一个应用模板中没有选择

问题描述

| 我试图理解
apply-templates
,但是我不明白为什么我在这里
apply-templates
中不写任何select = \“ nodename \”:(我想到了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>
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>
摘自w3schools教程。它如何理解应选择哪个模板?     

解决方法

        根据规格:   在没有select属性的情况下,xsl:apply-templates指令将处理当前节点的所有子级,包括文本节点。 没有选择XPath的
apply-templates
,将按照处理器在编译过程中构建的XML树视图的层次结构应用模板,除非您明确驱动模板(如对
title
和ѭ6did所做的那样)。 您可能还想考虑内置模板规则的工作方式。这些规则在后台运行,并允许在没有成功的模式匹配的情况下进行递归过程。 因此,如果您省略了根ѭ7the的模板匹配项,那么由于内置规则,您的模板仍然可以执行。 我认为处理顺序应该是这样的: 模板与根匹配,“ 8”告诉处理器将模板应用于“ 9”元素(在调用它的位置)。 没有找到
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()\"/>
在您的特定情况下,上面三个模板中的前一个模板是将模板应用于
cd
元素的合理模板。 每次编写显式模板时,这些内置模板都会被覆盖。 例子 您可以通过以下方法获得相同的结果:
<xsl:template match=\"cd\">
    <p>
        <xsl:apply-templates select=\"title\"/>
        <xsl:apply-templates select=\"artist\"/>
    </p>
</xsl:template>
与:
<xsl:template match=\"country|company|price|year\"/>

<xsl:template match=\"cd\">
    <p>
        <xsl:apply-templates />
    </p>
</xsl:template>
对于根,在您的情况下,您还可以通过替换以下内容获得相同的信息:
<xsl:template match=\"/\">
    <html>
        <body>
            <h2>My CD Collection</h2>
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>
<xsl:template match=\"/catalog\">
    <html>
        <body>
            <h2>My CD Collection</h2>
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>
还是:
<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>