2014-05-21 113 views
-2
<Instance xsi:type="ButtonConfig"> 
    <Name>ExitButton</Name> 
    <Height>89</Height> 
    <Width>120</Width> 
    <Margin> 
     <All>-1</All> 
     <Bottom>0</Bottom> 
     <Left>400</Left> 
     <Right>0</Right> 
     <Top>11</Top> 
    </Margin>   
</Instance> 

在上述xml中,我需要将左边距更改为420.如何使用XSLT执行此操作?使用XSLT更新XML节点

回答

3

这几乎是“identify transform”,它简单地复制输入文档。

下面是一个简单样式表,它主要执行恒等变换,而覆盖的<Instance/>具有包含ExitButton一个<Name/>内的<Margin/>内的输出为一个<Left/>。请注意,我必须为您的输入XML添加一个名称空间定义xsi,我认为它是文档中的其他位置。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Margin/Left[ancestor::Instance/Name[text()='ExitButton']]"> 
    <Left>420</Left> 
    </xsl:template> 
</xsl:stylesheet> 
+0

实例节点多次重新分配。该名称在每个实例节点中都是唯一的。 –

+1

@WPFLearner:那就是你应该在你的问题中提供的那种细节,然后!我更新了样式表以匹配特定的名称。 –

+0

它的工作原理。谢谢。 –

3

因为任何XSLT教程都会告诉你:对于像这样简单的事情,从标识样式表开始,它基本上不变地复制文档...然后添加一个实现该异常的模板。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

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

    <xsl:template match="Margin/Left"> 
    <xsl:copy> 
     <xsl:text>420</xsl:text> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
+0

实例节点被多次重新分段。该名称在每个实例节点中都是唯一的。 –

+3

然后,改善你的问题,所以我们有足够的细节来回答这个问题。或者从我已经勾画并扩展它的开始。 – keshlam