繁体   English   中英

使用XSLT替换XML中所有匹配的节点

[英]Replacing all matching nodes in XML using XSLT

我想替换xml文件中的所有匹配节点。

到原始xml:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <StackPanel>
    <Button/>
  </StackPanel>
</Window>

我应用了以下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="Button">
        <AnotherButton><xsl:apply-templates select="@*|node()" /></AnotherButton>
    </xsl:template>    
</xsl:stylesheet>

但是它产生相同的xml。 我做错了什么?

Sean的意思是,如果您从XML文档中删除名称空间,那么XSLT将可以正常工作

<Window>
  <StackPanel>
    <Button/>
  </StackPanel>
</Window>

生产...

<Window>
    <StackPanel>
        <AnotherButton />
    </StackPanel>
</Window>

或者 ,您询问是否可以保留名称空间

x:命名空间添加到Button中...

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <StackPanel>
    <x:Button/>
  </StackPanel>
</Window>

更新您的XSL以也使用此x:Button命名空间

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

    <xsl:template match="x:Button">
        <x:AnotherButton><xsl:apply-templates select="@*|node()" /></x:AnotherButton>
    </xsl:template>    
</xsl:stylesheet>

生产...

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <x:AnotherButton/>
    </StackPanel>
</Window>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM