1
<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?

1
  • Is this a faithful copy of your document? Are there any namespaces in your actual document? How did you try the solutions you linked to? Please show the code you used. Commented Apr 20, 2015 at 13:06

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Please show a proper identity transform, matching @*|node(). You wouldn't want to exclude comment() and processing-instruction().
You are right: it's not necessary on the actual input but the better way to do it
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.