根据其他元素的值删除元素——XSLT [英] Remove elements based on other element's value -- XSLT

查看:49
本文介绍了根据其他元素的值删除元素——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="http://www.w3.org/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="http://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="http://www.w3.org/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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