开发者

Copy Xml Document with all elements except a black list of elements

开发者 https://www.devze.com 2023-02-18 14:15 出处:网络
I have an Xml document like the following: <root> <record> <element1>value1</element1>

I have an Xml document like the following:

<root>
    <record>
        <element1>value1</element1>
        <element2>value2</element2>
        <element3>value3</element3>
        <element4>value4</element4>
   开发者_如何学JAVA     <element5>value5</element5>
        <repeatingelement1>value 1.1</repeatingelement1>
        <repeatingelement1>value 1.2</repeatingelement1>
        <repeatingelement1>value 1.3</repeatingelement1>
        <repeatingelement2>value 2.1</repeatingelement2>
        <repeatingelement2>value 2.2</repeatingelement2>
        <repeatingelement2>value 2.3</repeatingelement2>
    </record>
    <record>
        <element1>a</element1>
        <element2>b</element2>
        <element3>c</element3>
        <element4>d</element4>
        <element5>e</element5>
        <repeatingelement1>a 1.1</repeatingelement1>
        <repeatingelement1>a 1.2</repeatingelement1>
        <repeatingelement1>a 1.3</repeatingelement1>
        <repeatingelement2>b 2.1</repeatingelement2>
        <repeatingelement2>b 2.2</repeatingelement2>
        <repeatingelement2>b 2.3</repeatingelement2>
    </record>
    <record>
        <element1>value1</element1>
        <element2>value2</element2>
        <element3>value3</element3>
        <element4>value4</element4>
        <element5>value5</element5>
        <repeatingelement1>value 1.1</repeatingelement1>
        <repeatingelement1>value 1.2</repeatingelement1>
        <repeatingelement1>value 1.3</repeatingelement1>
        <repeatingelement2>value 2.1</repeatingelement2>
        <repeatingelement2>value 2.2</repeatingelement2>
        <repeatingelement2>value 2.3</repeatingelement2>
    </record>
</root>

I want to be able to get a copy of the Xml without a black list of elements say without element3 and repeatingelement2.

E.g. Output would be:

<root>
    <record>
        <element1>value1</element1>
        <element2>value2</element2>
        <element4>value4</element4>
        <element5>value5</element5>
        <repeatingelement1>value 1.1</repeatingelement1>
        <repeatingelement1>value 1.2</repeatingelement1>
        <repeatingelement1>value 1.3</repeatingelement1>
    </record>
    <record>
        <element1>a</element1>
        <element2>b</element2>
        <element4>d</element4>
        <element5>e</element5>
        <repeatingelement1>a 1.1</repeatingelement1>
        <repeatingelement1>a 1.2</repeatingelement1>
        <repeatingelement1>a 1.3</repeatingelement1>
    </record>
    <record>
        <element1>value1</element1>
        <element2>value2</element2>
        <element4>value4</element4>
        <element5>value5</element5>
        <repeatingelement1>value 1.1</repeatingelement1>
        <repeatingelement1>value 1.2</repeatingelement1>
        <repeatingelement1>value 1.3</repeatingelement1>
    </record>
</root>

Does anyone have the Xsl for this version 1.0 ideally? The only way i can think of doing it is by writing out XSLT for all the elements and leaving out XSL for the elements that I don't want.


That's surprisingly easy; just create a stylesheet with the identity template, and override it for the elements you want to remove, with no output. Like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="element3|repeatingelement2" />

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

精彩评论

暂无评论...
验证码 换一张
取 消