[英]XSLT To remove empty nodes and nodes with -1
我不是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>";
我需要找到一種方法來刪除其中包含-1的節點。 以前的開發人員認為將系統中的每個int默認為-1是一個好主意,是的,這意味着所有DB字段都有-1而不是null。
因此,盡管我想擊敗死馬(用棍棒,蝙蝠,火箭筒),但我需要重新開始工作並完成任務。
任何幫助都會很棒。
我有當前的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>
應用於此示例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>
在簡單的情況下,這應該工作:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[. = '' or . = '-1']"/>
通過這個簡單的輸入:
<root>
<node></node>
<node>-1</node>
<node>2</node>
<node>8</node>
<node>abc</node>
<node>-1</node>
<node></node>
<node>99</node>
</root>
結果將是:
<root>
<node>2</node>
<node>8</node>
<node>abc</node>
<node>99</node>
</root>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.