我有一个样式表,用于根据其他元素的值删除某些元素.但是,它不起作用......
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
发布评论