简体   繁体   English

如何使用Java或XSLT删除空的XML节点

[英]How to remove empty XML nodes using Java or XSLT

I have the following XML document: 我有以下XML文档:

<?xml version="1.0" encoding="utf-8"?>
<greatGrandParent>
    <grandParent>
        <parent>
            <sibling>Fred</sibling>
            <sibling>Max</sibling>
            <sibling>Katie</sibling>
        </parent>
        <parent>
            <sibling>Lindy</sibling>
            <sibling>Richard</sibling>
        </parent>
        <parent>
            <sibling />
        </parent>
    </grandParent>
    <grandParent>
        <parent>
            <sibling>Steve</sibling>
            <sibling>Abbas</sibling>
        </parent>
        <parent>
            <sibling>Kate</sibling>
            <sibling>James</sibling>
            <sibling>Ian</sibling>
        </parent>
    </grandParent>
    <grandParent>
        <parent>
            <sibling />
        </parent>
    </grandParent>
</greatGrandParent>

My requirement is - to remove an XML node if all of its child elements are empty. 我的要求是-如果所有XML子元素都为空,则删除它。 For instance, in the above shown XML, 例如,在上面显示的XML中,

  • the 3rd parent of the 1st grandParent has no siblings. 1st grandParent的第3个父级没有兄弟姐妹。 Hence, the sibling should be removed. 因此,兄弟姐妹应该被删除。 And since the parent has no siblings, the parent should be removed as well from the XML. 并且由于父级没有兄弟姐妹,因此也应从XML中删除父级。 But, the grandParent would still exist since it has other parents with siblings. 但是,grandParent仍然存在,因为它还有其他带有兄弟姐妹的父母。

  • The only parent within the 3rd grandparent has no siblings. 第三代祖父母中唯一的父母没有兄弟姐妹。 Hence, the sibling needs to be removed. 因此,需要删除兄弟姐妹。 And since the parent has no sibling, the parent should be removed as well. 并且由于父级没有兄弟姐妹,因此也应删除父级。 Since, the grandParent has no child elements(parent), the grandParent must also be removed from the XML. 由于grandParent没有子元素(父元素),因此必须从XML中删除grandParent。

Hence, the resulting XML must look like: 因此,生成的XML必须看起来像:

<?xml version="1.0" encoding="utf-8"?>
<greatGrandParent>
    <grandParent>
        <parent>
            <sibling>Fred</sibling>
            <sibling>Max</sibling>
            <sibling>Katie</sibling>
        </parent>
        <parent>
            <sibling>Lindy</sibling>
            <sibling>Richard</sibling>
        </parent>
    </grandParent>
    <grandParent>
        <parent>
            <sibling>Steve</sibling>
            <sibling>Abbas</sibling>
        </parent>
        <parent>
            <sibling>Kate</sibling>
            <sibling>James</sibling>
            <sibling>Ian</sibling>
        </parent>
    </grandParent>
</greatGrandParent>

I'd be glad if somebody can suggest me a solution to do this using any Java API or XSLT. 如果有人可以建议我使用任何Java API或XSLT进行此操作的解决方案,我将感到非常高兴。

You could use the following XSLT for this: 您可以为此使用以下XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
        <xsl:template match="node()|@*">
                <xsl:if test="normalize-space(string(.)) != ''">
                        <xsl:copy>
                                <xsl:apply-templates select="node()|@*"/>
                        </xsl:copy>
                </xsl:if>
        </xsl:template>
</xsl:stylesheet>

To get an idea of how it works, execute the next XSLT that gives you an extra element on grandParent node, also read this little tutorial: http://www.xmlplease.com/xsltidentity 要了解其工作原理,请执行下一个XSLT,该XSLT在grandParent节点上为您提供额外的元素,还请阅读此小教程: http : grandParent

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

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

    <xsl:template match="grandParent">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <childValues><xsl:value-of select="normalize-space(.)" /></childValues>
            <xsl:apply-templates select="node()" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

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

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