删除 XML 标签之间的某些文本 [英] Remove certain text between XML tags
问题描述
我需要一些帮助来转换这个 XML 文档:
I need some help to transform this XML document:
<root>
<tree>
<leaf>Hello</leaf>
ignore me
<pear>World</pear>
</tree>
</root>
为此:
<root>
<tree>
<leaf>Hello</leaf>
<pear>World</pear>
</tree>
</root>
这个例子是简化的,但基本上,我可以删除所有忽略我"的实例,或者不在叶子或梨子里的所有东西.
The example is simplified, but basically, I could either remove all instances of "ignore me" or everything that's not inside a leaf or a pear.
我只是想出了这个几乎可以复制所有内容的 XSLT:
I've only come up with this XSLT that copies pretty much everything:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" standalone="yes"/>
<xsl:template match="root|tree">
<xsl:element name="{name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="leaf|pear">
<xsl:element name="{name()}">
<xsl:copy-of select="child::node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我发现如何使用 xsl:call-template 删除 叶子或梨元素内的文本,但这不适用于 treeem> 元素.
What I have found out is how to use xsl:call-template to remove text inside a leaf or pear element, but that didn't work for things inside a tree element.
提前致谢.
推荐答案
看来您正在寻找身份转换.因为应该忽略作为根或树的直接子级的文本,为此添加空模板.因此尝试:
Looks like a identity transform is what you are looking for. Because the text as direct child of root or tree should be ignored add empty templates for that. Therefore try:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tree/text()" />
<xsl:template match="root/text()" />
</xsl:stylesheet>
这将生成以下输出:
<root>
<tree>
<leaf>Hello</leaf>
<pear>World</pear>
</tree>
</root>
这篇关于删除 XML 标签之间的某些文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!