申请模板是什么意思select =' *'?

时间:2015-10-10 02:50:32

标签: xml xslt cruisecontrol.net

原谅我,我刚刚学会了XSL,而且我很难理解apply-templates。在我的理解中。 apply-templates会发现节点与select匹配。并在当前xsl文档中搜索是否为指定的选择节点定义了template。然后将样式应用于这些节点。

例如:

<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>
</catalog>


<?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>
  • <xsl:template match="/">中的第一个apply-templates,表示根catalog下的所有节点都将应用指定的模板。因为已经为这些节点定义了模板。所以它会将模板应用于这些节点cd。其他apply-templates也是如此。他们使用相同的规则。

但是当我看到下面的xsl时。实际上有来自cruisecontrol.net MsTestReport2010.xsl

<xsl:variable name="runinfos"
                  select="*[local-name()='ResultSummary']/*[local-name()='RunInfos']/*[local-name()='RunInfo']" />
    <xsl:if test="count($runinfos) > 0">
      <h3>Errors and Warnings</h3>
      <table width="100%"
             border="1"
             cellSpacing="0"
             style="font-size:small;">
        <xsl:apply-templates select="$runinfos" />
      </table>
    </xsl:if>

在我的理解中,xsl:apply-templates select="$runinfos"将搜索xsl文档,找到为其定义的模板。这是下面的。

<xsl:template match="*[local-name()='RunInfo']">
    <tr>
      <td>
          <xsl:apply-templates select="*" />
      </td>
    </tr>
  </xsl:template>

但令我困惑的是它是什么意思select="*"。因为我在xsl文档中搜索.found没有为它定义这样的*模板..

我还想知道一个问题:如果xsl:apply-templates中的所选节点没有模板匹配怎么办?

我如何在某些工具中测试和调试xsl。如果你有一些好的,请与我分享。谢谢。

这是xml

<cruisecontrol project="KMIMProject">
    <build date="2015-10-09 19:01:32" buildtime="00:00:35" error="true" buildcondition="ForceBuild">
        <TestRun id="1bd2dff0-7418-4a1e-8ffd-189b27d1b118" name="Administrator@JOE-WANGPC 2015-10-09 19:01:26" runUser="JOE-WANGPC\Administrator" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
            <TestSettings name="Default Test Settings" id="6e1f3ea2-9cf0-4beb-8305-1a4b5db1fa55">
                <Deployment userDeploymentRoot="E:\study\cc.net\Test\KMIH\WorkingFolder" useDefaultDeploymentRoot="false" runDeploymentRoot="Administrator_JOE-WANGPC 2015-10-09 19_01_26"/>
                <Execution>
                    <TestTypeSpecific/>
                    <AgentRule name="Execution Agents">
      </AgentRule>
                </Execution>
                <Properties/>
            </TestSettings>
            <Times creation="2015-10-09T19:01:26.3934012+08:00" queuing="2015-10-09T19:01:26.6424154+08:00" start="2015-10-09T19:01:26.7014188+08:00" finish="2015-10-09T19:01:27.1244430+08:00"/>
            <ResultSummary outcome="Failed">
                <Counters total="106" executed="59" error="0" failed="59" timeout="0" aborted="0" inconclusive="0" passedButRunAborted="0" notRunnable="47" notExecuted="0" disconnected="0" warning="0" passed="0" completed="0" inProgress="0" pending="0"/>
                <RunInfos>
                    <RunInfo computerName="JOE-WANGPC" outcome="Warning" timestamp="2015-10-09T19:01:26.4934069+08:00">
                        <Text>Warning: Test Run deployment issue: The assembly or module 'KMIH.Persistence' directly or indirectly referenced by the test container 'E:\study\cc.net\Test\KMIH\SourceCheckOutFolder\kmih.unittests\obj\release\kmih.unittests.dll' was not found.</Text>
                    </RunInfo>
                    <RunInfo computerName="JOE-WANGPC" outcome="Warning" timestamp="2015-10-09T19:01:26.4934069+08:00">
                        <Text>Warning: Test Run deployment issue: The assembly or module 'KMIH.WebUI' directly or indirectly referenced by the test container 'E:\study\cc.net\Test\KMIH\SourceCheckOutFolder\kmih.unittests\obj\release\kmih.unittests.dll' was not found.</Text>
                    </RunInfo>
                </RunInfos>
            </ResultSummary>
        </TestRun>
    </build>
</cruisecontrol>

2 个答案:

答案 0 :(得分:2)

这意味着“选择所有元素子元素并应用与它们匹配的模板”。因此,如果存在子标记meh以及匹配meh的任何元素(或更一般的匹配)的模板,则它将该模板应用于该子元素。 apply-templates不会选择模板,它只会告诉引擎模板应该应用于选择(在select属性中),引擎应该为它们找到合适的模板。

如果没有匹配元素的模板,XSLT有内置规则,基本上说“将模板应用于所有子元素,如果有文本节点,则将它们复制到输出”。所以在这种情况下,由于RunInfo中的元素没有匹配的模板,XSLT只会将其中的文本复制到输出中。

答案 1 :(得分:1)

来自XPath Specification

  

*选择上下文节点的所有子元素

相关问题