问题
Forgive me I just learned the XSL
, And I have trouble to understand the apply-templates well. In my understanding. apply-templates
will find the nodes match the select
. and search in the current xsl document if there is a template
defined for specified select nodes. then apply the style to these nodes.
For Example:
<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>
- The first apply-templates in the
<xsl:template match="/">
, It means all the nodes under the rootcatalog
will apply the specified template. since there is a template already defined for these nodes. so it will apply the template to these nodescd
. And So does the other apply-templates. They works with the same rule.
But when I see the xsl like below. actually there are from 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>
in my understanding the xsl:apply-templates select="$runinfos"
will search the xsl document find the template defined for it. which is the below.
<xsl:template match="*[local-name()='RunInfo']">
<tr>
<td>
<xsl:apply-templates select="*" />
</td>
</tr>
</xsl:template>
But What make me confused is what does it mean select="*"
. Because I seach in the xsl document .found there is not such *
template defined for it ..
And I also wonder a question : What if there is no template match the selected nodes in the xsl:apply-templates
?
And How I can test and debug a xsl in some tools . Please share it with me if you have some good ones.Thanks.
Here is the 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>
回答1:
It means "select all element children and apply the templates that match them." So if there is, for example, a child tag meh
and a template for any element matching meh
(or a more general match), it would apply that template for that child element. The apply-templates
does not choose the template, it only tells the engine that templates should be applied to the selection (in the select
attribute) and the engine should find the appropriate templates for them.
If there is no template matching an element, XSLT has built-in rules that basically say "apply templates to all child elements also, and if there are text nodes, copy them to output". So in this case since the elements inside RunInfo have no matching template, XSLT will just copy the text inside it to the output.
回答2:
From the XPath Specification:
*
selects all element children of the context node
来源:https://stackoverflow.com/questions/33049741/what-does-it-mean-apply-templates-select