除非1(或2)个子项清空XSLT,否则排除XML节点

时间:2015-09-23 23:25:50

标签: xml xslt

如果除了一个元素()之外的所有元素都是空的,我试图排除记录节点。为了简单起见,我已将记录元素下面的元素数量减少到四个,但我可能最多只有50个。(因此,检查是否每个{{ 1}}会很可怕。)

输入

field = ''

我最接近的是以下

<root>
<record>
    <date>1/1/2015</date>
    <text>not empty</text>
    <text1/>
    <text2>not empty</text2>
</record>
<record>
    <date>1/3/2015</date>
    <text/>
    <text1/>
    <text2/>
</record>
<record>
    <date>1/5/2015</date>
    <text/>
    <text1/>
    <text2>more not empty</text2>
</record>

输出

<!--Identity template to copy all content by default-->
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="record/*[not(node())]"/>
<xsl:template match="date[not(following-sibling::* != '')]"/>

我想排除空<root> <record> <date>1/1/2015</date> <text>not empty</text> <text2>not empty</text2> </record> <record/> <record> <date>1/5/2015</date> <text2>more not empty</text2> </record> </root> 。如果我有第二个已知的&#39;这也是行不通的。元件。例如,如果每条记录上的第一个日期元素下面都有<record/>。有人有什么想法吗?

1 个答案:

答案 0 :(得分:1)

  

如果除了一个元素()之外的所有元素都是空的,我试图排除记录节点。

如何计算有多少不空?

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="record[count(*[node()]) &lt; 2]"/>
<xsl:template match="record/*[not(node())]"/>

</xsl:stylesheet>

或者,如果你只想看看&#34;未知&#34;节点:

<xsl:template match="record[not(*[not(self::date or self::date1)][node()])]"/>

这会禁止所有非datedate1字段为空的记录。

相关问题