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?

Community
  • 1
  • 1
user1151923
  • 1,853
  • 6
  • 28
  • 44
  • 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. – Mathias Müller Apr 20 '15 at 13:06

2 Answers2

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>
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
leu
  • 2,051
  • 2
  • 12
  • 25
  • Please show a proper _identity transform_, matching `@*|node()`. You wouldn't want to exclude `comment()` and `processing-instruction()`. – Mathias Müller Apr 20 '15 at 13:21
  • You are right: it's not necessary on the actual input but the better way to do it – leu Apr 20 '15 at 13:27
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>
DbxD
  • 540
  • 2
  • 15
  • 29