在《Sample XHTML 1.0 document》里看到图像热区也可以跟链接关联到一起,即点热区和点链接是一样的地址。看下具体的玩法。
正常的玩法:
=========================HTML===================================
<img src=”dot.png” alt=”image” usemap=”#map” height=”40″ width=”40″ />
<div> <map xml:lang=”en” dir=”ltr” id=”map” class=”map”
title=”map” name=”map” lang=”en”> <area href=”#grouping.heading”
id=”area” class=”area” title=”area” xml:lang=”en” dir=”ltr”
shape=”rect” coords=”0,0,19,19″ alt=”Grouping elements: div and span”
tabindex=”5″ lang=”en” /> <area href=”#” shape=”rect”
coords=”20,0,39,20″ alt=”Headings: h1, h2, h3, h4, h5, h6″ tabindex=”6″
/> <area href=”#” shape=”rect” coords=”0,20,20,39″ alt=”Address:
address” tabindex=”7″ /> <area shape=”rect” coords=”20,20,39,39″
nohref=”nohref” /> </map></div>
===============================================================
关联链接:
=========================HTML===================================
<img src=”dot.png” alt=”image” usemap=”#map2″ height=”40″ width=”40″ />
<div> <map xml:lang=”en” dir=”ltr” id=”map2″ class=”map” title=”map” name=”map2″ lang=”en”>
<ul>
<li><a href=”#” shape=”rect” coords=”0,0,19,19″>Grouping elements: div and span</a></li>
<li><a href=”#” shape=”rect” coords=”20,0,39,20″>Headings: h1, h2, h3, h4, h5, h6</a></li>
<li><a href=”#” shape=”rect” coords=”0,20,20,39″>Address: address</a></li>
</ul>
</map></div>
===============================================================
当一个节点匹配在 XSLT 模板中建立的多个模式(也称为规则)时,处理器就会按照 XSLT 规范中描述的冲突解决指导原则来确定使用哪一个模式。这些指导原则表明,当发生冲突时,会调用优先级最高的模板。然而,确定模板实际优先级的算法还需要附带解释一下。
要确定哪个模板具有最高优先级,处理器首先会消除导入的所有模板(使用 xsl:import 元素);自动导入的模板比经过导入转换的模板优先级低。然后处理器确定其余模板的优先级值。
可以通过 priority 属性显式指定模板的优先级。例如,以下模板被赋予优先级 1:
<xsl:template match="/foo/bar" priority="1"> <!-- do something interesting --> </xsl:template>
如果每个模板都赋予了优先级,则处理器可以使用这个值来确定哪个模板具有最高优先级。如果没有显式指定优先级,则处理器会为模板计算一个默认值。由处理器指定的默认优先级范围是从 -0.5 到 +0.5。基本上,模式越特殊,其默认优先级就越高。由于范围是从 -0.5 到 +0.5,因此如果显式指定一个模板的优先级为 1,就总会超过默认优先级。
| Pattern Type | Default Priority | Examples |
|---|---|---|
| Node test by type | -0.50 | * |
| @* | ||
| node | ||
| comment | ||
| text | ||
| processing-instruction | ||
| Namespace wildcard | -0.25 | ns:* |
| QName | 0.00 | foo |
| ns:foo | ||
| @bar | ||
| @ns:bar | ||
| Processing instruction tests by literal | 0.00 | processing-instruction('foo') |
| Everything else | 0.50 | ns:foo/bar |
| ns:foo[@bar] | ||
| foo[contains(.,'Aaron')] | ||
| //foo | ||
| Multiple patterns (pattern1 | pattern2) | Treated as distinct templates, whose priorities are calculated independently |
详细列出了如何为现有的不同类型的模式指定默认优先级。只包含按类型的节点测试的模式(例如 *、节点、注释、文本等)是最一般的,因此它们的默认优先级为 -0.5。只包含命名空间通配符 (ns:*) 的模式比较具体,所以它们的默认优先级为 -0.25。只包含限定名测试或常量处理指令测试(例如 foo、ns:foo、@bar、处理指令 ('foo') 等等)的模式分配的默认优先级为 0。而比这些具体的其他模式所分配的默认优先级为 0.5。这意味着具有多个定位步骤 (Location Step),或具有谓词的任何模式都会自动得到默认优先级 0.5。
仍然存在这样的情况:有多个具有相同优先级的模板匹配给定的节点。当出现这种情况时,处理器可能产生出错信号,也可能选择使用文档中的最后一个模板。这是通常的选择方式。例如,以下 XSLT 片段包含两个模板,它们可以匹配相同的 foo 元素(该元素具有一个 bar 子元素和一个 bar 父元素)。由于两个模板的默认优先级都为 0.5,因此 XSLT 处理器要么产生出错信号,要么选择文档中的最后一个模板 — 在本例中为匹配 bar/foo 元素的模板:
<xsl:template match='foo[bar]' > <!-- default priority = .5 --> </xsl:template> <xsl:template match='bar/foo'> <!-- default priority = .5 --> </xsl:template>
当然,您最好是避免出现这样的情况。如果您想让处理器在出现冲突时始终使用第一个模板,则只需要将它的优先级设为 1,如下面的代码所示:
<xsl:template match='foo[bar]' priority='1'> <!-- would have a default priority = .5 --> </xsl:template> <xsl:template match='foo/bar' > <!-- default priority = .5 --> </xsl:template>
正如您所看到的,当出现冲突时,XSLT 需要经过大量处理才能确定调用哪个模板。当大量使用 XSLT 的声明性编程模型时,理解这些规则是很有必要的。