apply-templates select='*' 是什么意思?
What does it mean apply-templates select='*'?
原谅我刚学XSL
,apply-templates有点看不懂。在我的理解中。 apply-templates
将找到与 select
匹配的节点。并在当前 xsl 文档中搜索是否为指定的 select 节点定义了 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
。其他应用模板也是如此。他们使用相同的规则。
但是当我看到如下所示的 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 文档中搜索。发现没有为它定义的 *
模板..
而且我也想知道一个问题:如果xsl:apply-templates
中没有匹配selected节点的模板怎么办?
以及如何在某些工具中测试和调试 xsl。有好的请分享给我ones.Thanks.
这里是 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>
*
selects all element children of the context node
意思是"select all element children and apply the templates that match them." 所以如果有,例如,子标签meh
和任何元素匹配meh
(或更一般的匹配)的模板,它会将该模板应用于该子元素。 apply-templates
不选择模板,它只是告诉引擎应该将模板应用于选择(在 select
属性中)并且引擎应该为它们找到合适的模板。
如果没有匹配元素的模板,XSLT 有内置规则,基本上是 "apply templates to all child elements also, and if there are text nodes, copy them to output"。所以在这种情况下,由于 RunInfo 中的元素没有匹配的模板,XSLT 只会将其中的文本复制到输出中。
原谅我刚学XSL
,apply-templates有点看不懂。在我的理解中。 apply-templates
将找到与 select
匹配的节点。并在当前 xsl 文档中搜索是否为指定的 select 节点定义了 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
。其他应用模板也是如此。他们使用相同的规则。
但是当我看到如下所示的 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 文档中搜索。发现没有为它定义的 *
模板..
而且我也想知道一个问题:如果xsl:apply-templates
中没有匹配selected节点的模板怎么办?
以及如何在某些工具中测试和调试 xsl。有好的请分享给我ones.Thanks.
这里是 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>
*
selects all element children of the context node
意思是"select all element children and apply the templates that match them." 所以如果有,例如,子标签meh
和任何元素匹配meh
(或更一般的匹配)的模板,它会将该模板应用于该子元素。 apply-templates
不选择模板,它只是告诉引擎应该将模板应用于选择(在 select
属性中)并且引擎应该为它们找到合适的模板。
如果没有匹配元素的模板,XSLT 有内置规则,基本上是 "apply templates to all child elements also, and if there are text nodes, copy them to output"。所以在这种情况下,由于 RunInfo 中的元素没有匹配的模板,XSLT 只会将其中的文本复制到输出中。