apply-templates select ='*'是什么意思? [英] What does it mean apply-templates select='*'?

查看:121
本文介绍了apply-templates select ='*'是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我刚刚学习了 XSL ,而且我很难很好地理解应用模板。以我的理解。 应用模板将找到与 select 匹配的节点。并在当前xsl文档中搜索是否为指定的选择节点定义了模板。然后将样式应用于这些节点。

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.

例如:

<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 = /> ,这表示根目录下的所有节点都将应用指定的模板。因为已经为这些节点定义了模板。因此它将模板应用于这些节点 cd 。其他适用模板也是如此。它们使用相同的规则。

    • The first apply-templates in the <xsl:template match="/">, It means all the nodes under the root catalog will apply the specified template. since there is a template already defined for these nodes. so it will apply the template to these nodes cd. And So does the other apply-templates. They works with the same rule.
    • 但是当我看到如下所示的xsl时。实际上来自cruisecontrol.net MsTestReport2010.xsl

      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>
      

      以我的理解 xsl:apply-templates select = $ runinfos 将搜索xsl文档,找到为其定义的模板。

      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>
      

      但是让我感到困惑的是 select = * 。因为我在xsl文档中搜寻.found,所以没有为它定义这样的 * 模板..

      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 ..

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

      And I also wonder a question : What if there is no template match the selected nodes in the xsl:apply-templates ?

      以及如何在某些工具中测试和调试xsl。

      And How I can test and debug a xsl in some tools . Please share it with me if you have some good 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>
      


      推荐答案

      表示选择所有子元素并应用模板与他们匹配。因此,例如,如果有一个子标记 meh 和一个匹配 meh 的元素的模板(或更多)一般匹配),它将对该子元素应用该模板。 apply-templates 不会选择模板,它只是告诉引擎应将模板应用于选择(在 select 属性),然后引擎应为它们找到合适的模板。

      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.

      如果没有与元素匹配的模板,则XSLT内置了一些规则,这些规则基本上说也将模板应用于所有子元素,如果有文本节点,请将其复制到输出中。因此,在这种情况下,由于RunInfo中的元素没有匹配的模板,因此XSLT只会将其中的文本复制到输出中。

      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.

      这篇关于apply-templates select ='*'是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆