2013-03-18 63 views
0

我在样式表的attribut上有一个全局匹配,但是我想排除f - 元素。我怎样才能做到这一点?xsl按属性匹配模板,除了一个

示例XML:

<a> 
<b formatter="std">...</b> 
<c formatter="abc">...</c> 
<d formatter="xxx"> 
    <e formatter="uuu">...</e> 
    <f formatter="iii"> 
     <g formatter="ooo">...</g> 
     <h formatter="uuu">...</h> 
    </f> 
</d> 
</a> 

目前的解决方案:

<xsl:template match="//*[@formatter]"> 
    ... 
</xsl:template> 

我已经试过这样的事情,但没有奏效。

<xsl:template match="f//*[@formatter]"> 
... 
</xsl:template> 

<xsl:template match="//f*[@formatter]"> 
... 
</xsl:template> 
+0

您想要排除** f **元素及其所有子元素,还是想要保留子节点(将它们移动到某个级别)? – 2013-03-18 08:46:52

+0

我只想排除** f **元素。应该“正常”处理子节点。 – sbo 2013-03-18 08:56:12

+0

元素'f'中'formatter'的属性总是'iii'?如果是的话,你可以用它作为标准。 – Peter 2013-03-18 09:19:37

回答

3

无论//f[@formatter]f[@formatter]会工作(该//是没有必要的)。当这个XSLT是在你的榜样输入运行:

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

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

    <xsl:template match="*[@formatter]"> 
    <xsl:element name="transformed-{local-name()}"> 
     <xsl:apply-templates select="@* | node()" /> 
    </xsl:element> 
    </xsl:template> 

    <xsl:template match="f[@formatter]"> 
    <xsl:apply-templates select="node()" /> 
    </xsl:template> 
</xsl:stylesheet> 

结果是:

<a> 
    <transformed-b formatter="std">...</transformed-b> 
    <transformed-c formatter="abc">...</transformed-c> 
    <transformed-d formatter="xxx"> 
    <transformed-e formatter="uuu">...</transformed-e> 

     <transformed-g formatter="ooo">...</transformed-g> 
     <transformed-h formatter="uuu">...</transformed-h> 

    </transformed-d> 
</a> 

正如你所看到的,f被排除在外。这是否回答你的问题,或者我误解了你想要做的事情?

+0

不错的解决方案,我认为这就是sbo想要的。 +1 – Peter 2013-03-18 10:49:14

+0

就是这样。谢谢。 – sbo 2013-03-26 19:44:32