对apply-templates上的select属性进行xslt处理

Sak*_*vel 0 xslt

我有一个如下的XSLT并将此xslt应用到输入xml [粘贴在下面],它正常工作,除了需要澄清的一件事.

这是输入xml

<Test>
  <Experiment id='1'>
    <Dish1>
      <Conditions pressure='x' temp='y'/>
      <Measurement timeStamp='8am' reading='y'/>
    </Dish1>
    <Dish2>
      <Conditions pressure='x' temp='y'/>
      <Measurement timeStamp='8am' reading='y'/>
    </Dish2>
    <Dish1>
      <Conditions pressure='x' temp='y'/>
      <Measurement timeStamp='2pm' reading='y'/>
    </Dish1>
    <Dish2>
      <Conditions pressure='x' temp='y'/>
      <Measurement timeStamp='2pm' reading='y'/>
    </Dish2>
   </Experiment>
   <Experiment id='2'>
    <Dish1>
      <Conditions pressure='x' temp='y'/>
      <Measurement timeStamp='9am' reading='y'/>
    </Dish1>
    </Experiment>
</Test>
Run Code Online (Sandbox Code Playgroud)

这是xslt

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="Experiment">
      <xsl:copy>
         <xsl:apply-templates select="@*" />
         <xsl:for-each-group select="*" group-by="local-name()">
            <xsl:copy>
               <xsl:apply-templates select="current-group()" />

            </xsl:copy>
         </xsl:for-each-group>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="Experiment/*">
      <Observation>
         <xsl:apply-templates select="*/@*" />
      </Observation>
   </xsl:template>

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

这是转型工作正常.但是,如果我在xslt上更改如下,则会出现错误.任何的想法?

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="."/>
</xsl:copy>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

也就是说,在最后一场比赛中,我已经改变 <xsl:apply-templates select="@*|node()"/><xsl:apply-templates select="."/>

Dan*_*ley 5

.表示当前节点.当您应用模板时.,您将它们应用于当前上下文.

在您的情况下,模板将匹配Test,输出它的副本,并将模板应用于自身.它将再次匹配,基本上让你进入无限循环.

通过执行apply-templates node()|@*,您可以将模板应用于所有子节点和属性.