简体   繁体   English

XSLT使用-1删除空节点和节点

[英]XSLT To remove empty nodes and nodes with -1

I'm not an XSLT wizard. 我不是XSLT向导。

I have the current XSLT i'm using to remove empty nodes: 我有当前的XSLT我用来删除空节点:

 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>";

I need to find a way to also remove nodes with -1 in them. 我需要找到一种方法来删除其中包含-1的节点。 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. 以前的开发人员认为将系统中的每个int默认为-1是一个好主意,是的,这意味着所有DB字段都有-1而不是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. 因此,尽管我想击败死马(用棍棒,蝙蝠,火箭筒),但我需要重新开始工作并完成任务。

Any help would be great. 任何帮助都会很棒。

I have the current XSLT i'm using to remove empty nodes: 我有当前的XSLT我用来删除空节点:

. . . . . . . . .

I need to find a way to also remove nodes with -1 in them 我需要找到一种方法来删除其中包含-1的节点

I guess that it is required to remove all "empty nodes". 我想要删除所有 “空节点”。

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 . 在您的情况下,一个合理的定义是: 任何没有属性和子元素或没有属性的元素,并且只有一个子元素是值为-1的文本节点

For this definition here is a simple solution. 对于这个定义,这是一个简单的解决方案

This transformation : 这种转变

<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>

when applied on this sample XML document : 应用于此示例XML文档时

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

produces the wanted, correct result : 产生想要的,正确的结果

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

In simple case this should work: 在简单的情况下,这应该工作:

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>     

<xsl:template match="*[. = '' or . = '-1']"/>

With this simple input: 通过这个简单的输入:

<root>
    <node></node>
    <node>-1</node>
    <node>2</node>
    <node>8</node>
    <node>abc</node>
    <node>-1</node>
    <node></node>
    <node>99</node>
</root>

Result will be: 结果将是:

<root>
    <node>2</node>
    <node>8</node>
    <node>abc</node>
    <node>99</node>
</root>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM