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

Rya*_*ier 7 xml xslt

我不是XSLT向导.

我有当前的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>";
Run Code Online (Sandbox Code Playgroud)

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

因此,尽管我想击败死马(用棍棒,蝙蝠,火箭筒),但我需要重新开始工作并完成任务.

任何帮助都会很棒.

Dim*_*hev 12

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

.........

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

我想要删除所有 "空节点".

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

对于这个定义,这是一个简单的解决方案

这种转变:

<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>
Run Code Online (Sandbox Code Playgroud)

应用于此示例XML文档时:

<t>
 <a>-1</a>
 <a>2</a>
 <b><c/></b>
 <d>-1</d>
 <d>15</d>
 <e x="1"/>
 <f>foo</f>
</t>
Run Code Online (Sandbox Code Playgroud)

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

<t>
   <a>2</a>
   <b/>
   <d>15</d>
   <e x="1"/>
   <f>foo</f>
</t>
Run Code Online (Sandbox Code Playgroud)


Fla*_*ack 5

在简单的情况下,这应该工作:

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

<xsl:template match="*[. = '' or . = '-1']"/>
Run Code Online (Sandbox Code Playgroud)

通过这个简单的输入:

<root>
    <node></node>
    <node>-1</node>
    <node>2</node>
    <node>8</node>
    <node>abc</node>
    <node>-1</node>
    <node></node>
    <node>99</node>
</root>
Run Code Online (Sandbox Code Playgroud)

结果将是:

<root>
    <node>2</node>
    <node>8</node>
    <node>abc</node>
    <node>99</node>
</root>
Run Code Online (Sandbox Code Playgroud)