使用 XSLT 从 xml 中删除所有节点,不包括特定节点 [英] Remove all nodes from xml excluding specific nodes using XSLT
本文介绍了使用 XSLT 从 xml 中删除所有节点,不包括特定节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一堆 xml 文件,其中包含不同数量的数据节点,我想使用 XSLT 更改文件以仅包含特定节点.示例:
I have a bunch of xml files with a varying amount of data nodes in them and I want to change the files using XSLT to include only specific nodes. Example:
<?xml version="1.0" encoding="UTF-8"?>
<SomeName>
<identifier>
<UID> 1234 </UID>
</identifier>
<MainNode1>
<SubNode1>
<Subnode1a>DATA1a0</Subnode1a>
</SubNode1>
<SubNode1>
<Subnode1a>DATA1a1</Subnode1a>
</SubNode1>
<SubNode1>
<Subnode1a>DATA1a2</Subnode1a>
</SubNode1>
</MainNode1>
<MainNode2>
<SubNode2>
<Subnode2a>DATA2a0</Subnode2a>
</SubNode2>
</MainNode2>
<MainNodeIDONTCARE>
<SubnodeWhatever>
</SubnodeWhatever>
</MainNodeIDONTCARE>
<MainNodeuseless>
<SubnodeWhatever>
</SubnodeWhatever>
</MainNodeuseless>
<MainNodewhatever>
<SubnodeWhatever>
</SubnodeWhatever>
</MainNodewhatever>
</SomeName>
现在我的最终 XML 文件应该如下所示:
Now my final XML file should look like:
<?xml version="1.0" encoding="UTF-8"?>
<SomeName>
<identifier>
<UID> 1234 </UID>
</identifier>
<MainNode1>
<SubNode1>
<Subnode1a>DATA1a0</Subnode1a>
</SubNode1>
<SubNode1>
<Subnode1a>DATA1a1</Subnode1a>
</SubNode1>
<SubNode1>
<Subnode1a>DATA1a2</Subnode1a>
</SubNode1>
</MainNode1>
<MainNode2>
<SubNode2>
<Subnode2a>DATA2a0</Subnode2a>
</SubNode2>
</MainNode2>
</SomeName>
我一直在尝试使用 XSLT 完成它,但似乎无法完成.
I've been trying to get it done with XSLT, but I can't seem to get it done.
感谢您的帮助.
推荐答案
这应该有效:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="SomeName">
<xsl:copy>
<xsl:for-each select="identifier|MainNode1|MainNode2">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这篇关于使用 XSLT 从 xml 中删除所有节点,不包括特定节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文