XSL:如何复制一棵树,但删除一些节点? [英] XSL: how to copy a tree, but removing some nodes?

查看:30
本文介绍了XSL:如何复制一棵树,但删除一些节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 XSL 从树中删除一些元素.

I want to use XSL to remove some elements from a tree.

假设我有以下 XML 树:

Suppose I have the following XML tree:

<?xml version="1.0" ?>
<mydoc>
    <file>
        <colors>
            <blue />
            <red />
            <green />
        </colors>
        <secret>
            <username />
            <password />
        </secret>
    </file>
</mydoc>

我想从中删除用户名和密码节点.我将如何继续使用 XSL?

I want to remove the username and password nodes from it. How would I proceed with XSL ?

推荐答案

您想要一个身份转换.XSLT 中的一个常见设计模式是将复制所有内容的转换.然后添加模板以移除或转换源和目标之间的差异.

You want an identity transform. A common design pattern in XSLT is a transform that will copy everything. Then you add templates to remove or transform what is different between the source and the target.

<?xml version="1.0" encoding="UTF-8"?>
<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="username|password"/> <!-- this empty template will remove them -->
</xsl:stylesheet>

这篇关于XSL:如何复制一棵树,但删除一些节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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