XSLT 复制除 1 个元素之外的所有节点 [英] XSLT Copy all Nodes except 1 element

查看:20
本文介绍了XSLT 复制除 1 个元素之外的所有节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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>

如何复制所有 xml 并排除 DataSeriesBodyType 元素

How do i copy all xml and exclude the DataSeriesBodyType element

推荐答案

您只需要使用身份模板(正如您以前使用的那样),然后使用与 DataSeriesBodyType 匹配的模板,它什么也不做.

You just have to use the identity template (as you were using) and then use a template matching DataSeriesBodyType which does nothing.

代码如下:

<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>

这篇关于XSLT 复制除 1 个元素之外的所有节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆