应用模板上选择属性的 xslt 处理 [英] xslt processing on select attribute on apply-templates
问题描述
我有一个如下的 XSLT 并将这个 xslt 应用到输入 xml[粘贴在下面],除了需要澄清的一件事外,它工作正常.
I have a XSLT as below and apply this xslt into the input xml[pasted below], it is working fine except one thing that need to clarify.
这是输入的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>
这是 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>
这是转换工作正常.但是,如果我在 xslt 上进行如下更改,则会出错.有什么想法吗?
This is transformation is working fine. But, if i change as below on the xslt, then, it is getting error. Any idea?
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="."/>
</xsl:copy>
</xsl:template>
即,在最后一场比赛中,我已将 <xsl:apply-templates select="@*|node()"/>
更改为 <xsl:apply-templates select="."/>
I.e., On the last match, I have changed from <xsl:apply-templates select="@*|node()"/>
to <xsl:apply-templates select="."/>
推荐答案
.
表示当前节点.当您将模板应用于 .
时,您将它们应用于当前上下文.
.
means the current node. When you apply-templates to .
, you're applying them to the current context.
在您的情况下,模板将匹配 Test
,输出它的副本,并将模板应用于自身.然后它会再次匹配,基本上让你陷入无限循环.
In your case, the template will match Test
, output a copy of it, and apply-templates to itself. It will then match again basically putting you into an endless loop.
通过在 node()|@*
上执行应用模板,您将模板应用于所有子节点和属性.
By doing the apply-templates on node()|@*
, you're applying templates to all child nodes and attributes.
这篇关于应用模板上选择属性的 xslt 处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!