根据其他元素的值删除元素——XSLT

编程入门 行业动态 更新时间:2024-10-26 19:38:23
本文介绍了根据其他元素的值删除元素——XSLT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个样式表,用于根据其他元素的值删除某些元素.但是,它不起作用......

I have a style-sheet that I am using to remove certain elements based on the value of an other element. However, it is not working ...

示例输入 XML

<Model> <Year>1999</Year> <Operation>ABC</Operation> <Text>Testing</Text> <Status>Ok</Status> </Model>

如果操作值为ABC",则从 XML 中删除文本和状态节点.并给出以下输出.

If Operation value is 'ABC' then remove Text and Status nodes from XML. And gives the following output.

<Model> <Year>1999</Year> <Operation>ABC</Operation> </Model>

这是我正在使用的样式表,但即使操作不是ABC",它也会从所有 XML 中删除文本和状态节点.

Here is my style sheet that I am using but it is removing Text and Status nodes from all XMLs even when operation is not 'ABC'.

<xsl:stylesheet version="1.0" xmlns:xsl="www.w3/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:variable name="ID" select="//Operation"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="Text | Status"> <xsl:if test ="$ID ='ABC'"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:if> </xsl:template> </xsl:stylesheet>

提前致谢

当命名空间存在时我将如何做同样的事情

How would I do the same when namespace is present like

<ns0:next type="Sale" xmlns:ns0="Test.Schemas.Inside_Sales">

推荐答案

这是一个完整的 XSLT 转换——简短而简单(没有变量,没有 xsl:if, xsl:choose, xsl:when, xsl:otherwise):

Here is a complete XSLT transformation -- short and simple (no variables, no xsl:if, xsl:choose, xsl:when, xsl:otherwise):

<xsl:stylesheet version="1.0" xmlns:xsl="www.w3/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match= "*[Operation='ABC']/Text | *[Operation='ABC']/Status"/> </xsl:stylesheet>

当此转换应用于提供的 XML 文档时:

<Model> <Year>1999</Year> <Operation>ABC</Operation> <Text>Testing</Text> <Status>Ok</Status> </Model>

产生了想要的、正确的结果:

<Model> <Year>1999</Year> <Operation>ABC</Operation> </Model>

更多推荐

根据其他元素的值删除元素——XSLT

本文发布于:2023-11-30 11:09:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649760.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   XSLT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!