XSLT 删除空节点和带有 -1 的节点 [英] XSLT To remove empty nodes and nodes with -1

查看:26
本文介绍了XSLT 删除空节点和带有 -1 的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是 XSLT 向导.

I'm not an XSLT wizard.

我有当前用于删除空节点的 XSLT:

I have the current XSLT i'm using to remove empty nodes:

 string strippingStylesheet = "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" +
                "<xsl:template match=\"@*|node()\">" +
                "<xsl:if test=\". != ''\">" +
                "<xsl:copy>" + 
                "<xsl:apply-templates select=\"@*|node()\"/>" +
                "</xsl:copy>" + 
                "</xsl:if></xsl:template></xsl:stylesheet>";

我需要找到一种方法来删除其中包含 -1 的节点.以前的开发人员认为将系统中的每个 int 都默认为 -1 是个好主意,是的,这意味着所有 DB 字段都包含 -1 而不是 null.

I need to find a way to also remove nodes with -1 in them. A previous developer thought it would be a good idea to make every int in the system be defaulted to -1, and yes that means all DB fields have -1 in them instead of null.

尽管我想打败死马(用棍子、蝙蝠、火箭筒),但我需要重新开始工作并完成这项工作.

So as much as I want to beat the dead horse (with a stick, bat, bazooka), I need to get back to work and get this done.

任何帮助都会很棒.

推荐答案

我有我正在使用的当前 XSLT删除空节点:

I have the current XSLT i'm using to remove empty nodes:

.........

我需要找到一种方法来删除其中包含 -1 的节点

I need to find a way to also remove nodes with -1 in them

我猜需要删除所有空节点".

I guess that it is required to remove all "empty nodes".

处理取决于空节点"的定义.在您的情况下,一个合理的定义是:任何没有属性和子元素的元素,或者没有属性并且只有一个子元素是值为 -1 的文本节点.

The processing depends on the definition of "empty node". One reasonable definition in your case is: Any element that doesn't have attributes and children or doesn't have attributes and has only one child that is a text node with value -1.

对于这个定义,这里有一个简单的解决方案.

For this definition here is a simple solution.

这种转变:

<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="*[not(@*) and not(*) and (not(text()) or .=-1)]"/>
</xsl:stylesheet>

应用于此示例 XML 文档时:

<t>
 <a>-1</a>
 <a>2</a>
 <b><c/></b>
 <d>-1</d>
 <d>15</d>
 <e x="1"/>
 <f>foo</f>
</t>

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

<t>
   <a>2</a>
   <b/>
   <d>15</d>
   <e x="1"/>
   <f>foo</f>
</t>

这篇关于XSLT 删除空节点和带有 -1 的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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