Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT Copy all Nodes except 1 element

Tags:

xml

xslt

<events>
    <main>
        <action>modification</action>
        <subAction>weights</subAction>
    </main>
</events>
<SeriesSet>
    <Series id="Price_0">
        <seriesBodies>
            <SeriesBody>
                <DataSeriesBodyType>Do Not Copy</DataSeriesBodyType>
            </SeriesBody>
        </SeriesBodies>
    </Series>
</SeriesSet>

How do i copy all xml and exclude the DataSeriesBodyType element

like image 224
user2092096 Avatar asked Feb 20 '13 16:02

user2092096


People also ask

What is exclude result prefixes in XSLT?

Optional Attributes exclude-result-prefixes. Specifies any namespace used in this document that should not be sent to the output document. The list is whitespace separated. extension-element-prefixes. Specifies a space-separated list of any namespace prefixes for extension elements in this document.

What is current group () in XSLT?

Returns the contents of the current group selected by xsl:for-each-group. Available in XSLT 2.0 and later versions. Available in all Saxon editions. current-group() ➔ item()*

What is Number () in XSLT?

Specifies the format pattern. Here are some of the characters used in the formatting pattern: 0 (Digit)

What is following sibling in XSLT?

The following-sibling axis indicates all the nodes that have the same parent as the context node and appear after the context node in the source document.


1 Answers

You just have to use the identity template (as you were using) and then use a template matching DataSeriesBodyType which does nothing.

The code would be:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


    <xsl:output method="xml" encoding="utf-8" indent="yes"/>

    <!-- Identity template : copy all text nodes, elements and attributes -->   
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <!-- When matching DataSeriesBodyType: do nothing -->
    <xsl:template match="DataSeriesBodyType" />

</xsl:stylesheet>

If you want to normalize the output to remove empty data text nodes, then add to the previous stylesheet the following template:

<xsl:template match="text()">
    <xsl:value-of select="normalize-space()" />
</xsl:template>
like image 153
Pablo Pozo Avatar answered Sep 19 '22 08:09

Pablo Pozo