xml XSLT 复制除 1 个元素之外的所有节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14985139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XSLT Copy all Nodes except 1 element
提问by user2092096
<events>
<main>
<action>modification</action>
<subAction>weights</subAction>
</main>
</events>
<SeriesSet>
<Series id="Price_0">
<seriesBodies>
<SeriesBody>
<DataSeriesBodyType>Do Not Copy</DataSeriesBodyType>
</SeriesBody>
</SeriesBodies>
</Series>
</SeriesSet>
How do i copy all xml and exclude the DataSeriesBodyType element
我如何复制所有 xml 并排除 DataSeriesBodyType 元素
回答by Pablo Pozo
You just have to use the identity template (as you were using) and then use a template matching DataSeriesBodyType which does nothing.
您只需要使用身份模板(就像您使用的那样),然后使用与 DataSeriesBodyType 匹配的模板,它什么都不做。
The code would be:
代码将是:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<!-- Identity template : copy all text nodes, elements and attributes -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- When matching DataSeriesBodyType: do nothing -->
<xsl:template match="DataSeriesBodyType" />
</xsl:stylesheet>
If you want to normalize the output to remove empty data text nodes, then add to the previous stylesheet the following template:
如果要规范化输出以删除空数据文本节点,请将以下模板添加到之前的样式表中:
<xsl:template match="text()">
<xsl:value-of select="normalize-space()" />
</xsl:template>

