使用除黑色元素列表之外的所有元素复制Xml文档

时间:2011-03-21 13:56:15

标签: xml xslt xslt-1.0

我有一个如下的Xml文档:

<root>
    <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>
    <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>

我希望能够在没有element3repeatingelement2的黑色元素列表的情况下获取Xml的副本。

E.g。输出将是:

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

理想情况下,是否有人拥有此版本1.0的Xsl?我能想到的唯一方法是为所有元素写出XSLT,并为我不想要的元素省略XSL。

1 个答案:

答案 0 :(得分:6)

这简直容易;只需使用标识模板创建样式表,并为要删除的元素覆盖它,而不输出。像这样:

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