user1151923
user1151923

Reputation: 1872

remove node from XML with XSLT

<Document>
    <NodeA>1</NodeA>
    <NodeB>2</NodeB>
    <ServiceNode>3</ServiceNode>
    <NodeX>4</NodeX>
</Document>

I need to remove ServiceNode from the XML above using XSLT transformation. The output of the transformation should be:

<Document>
    <NodeA>1</NodeA>
    <NodeB>2</NodeB>
    <NodeX>4</NodeX>
</Document>

I have tried this solution and this solution and did not get neither of those to work. The output value always still included the "excluded" nodes. What should I do to get this to work?

Upvotes: 1

Views: 3402

Answers (2)

DbxD
DbxD

Reputation: 550

If Anyone is still looking at this, use <xsl:strip-space elements="*"/> to make sure there are no empty lines in XML once the tags are removed and use <xsl:output method="xml" indent="yes"/> to preserve the indentation. Below is a possible solution.

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

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

    <xsl:template match="ServiceNode"/>
</xsl:stylesheet>

Upvotes: 0

leu
leu

Reputation: 2081

You didn't tell what your XSL looks like. Therefore, I guess that there is another error in it?!

Using the following code, you can eliminate <ServiceNode> by applying an empty template.

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

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

    <xsl:template match="ServiceNode"/>
</xsl:stylesheet>

Upvotes: 3

Related Questions