xslt - xsl如何使用xsl删除父级下的大量xml元素
问题描述
我有一个如下的 xml 结构:
<Parent>
<child1></child1>
<child2></child2>
<child3></child3>
...
...
</Parent>
假设有大约 20 个子 xml 元素需要删除,并且元素下只保留 3 个 xml 元素<Parent>
。
我知道使用身份模板,例如下面的模板可以删除 xml 文件中的一个元素。
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="child1"/>
但事情是我必须删除 20 个 xml 子元素并且只保留其中的 3 个。如何使代码尽可能少地实现这一点?
解决方案
自己想出来的。我做了以下事情:
<xsl:template match="Parent">
<xsl:copy>
<xsl:apply-templates select="child1"/>
<xsl:apply-templates select="child2"/>
<xsl:apply-templates select="child3"/>
</xsl:copy>
</xsl:template>
并将模板应用到的父节点上<Patent>
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
推荐阅读
- python - 使用python删除冗余节点集
- clojure - 是否可以定义一个宏:在函数上调度其他任何东西?
- python - aiohttp中的自定义访问日志
- scala - 错误:未找到:值 SparkSession
- maven - JHipster Spring Boot 模块化拆分包
- haskell - Haskell 列出错误
- javascript - 绑定数组中不同元素的值 (JavaScript)
- r - Y 和 M 模型中样本量不均匀的 R 多级中介
- sql - 如何为我的 Reporting Services 目录中的所有报表添加内容管理员权限?
- java - 使用循环访问 Freemarker 根对象