0

我有以下 xml:

<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person>
    <phone type='home'>203-555-1212</phone>
    <phone type='fax'>203-555-1212</phone>
    <address type='home'>
        <street>12 Main Street</street>
        <city>Southbury</city>
        <state>CT</state>
        <zip>06488</zip>
    </address>
    <firstName>Charles</firstName>
    <lastName>Smithington</lastName>
</person>
<person1>
        <phone type='home'>58-99-44-999</phone>
        <phone type='fax'>5788-9987-3365</phone>
        <address type='home'>
        <street>12 Main Street</street>
        <city>Park Avenue</city>
        <state>NY</state>
        <zip>10025</zip>
    </address>
        <firstName>Mike</firstName>
        <lastName>Shinoda</lastName>
    </person1>
</persons>

在这里,我必须排除州为 CT 的人的地址字段,而包括州为 NY 的人的地址。

4

1 回答 1

0

<xsl:template match="persons">

    <persons>

        <xsl:apply-templates select="person"/>

        <xsl:apply-templates select="person1"/>

    </persons>

</xsl:template>

<xsl:template match="person">

    <person>
        <xsl:copy-of select="*[name() != 'address']"/>

        <xsl:apply-templates select="address"/>

    </person>

</xsl:template><xsl:template match="person1">

    <person1>

        <xsl:copy-of select="*[name() != 'address']"/>

        <xsl:apply-templates select="address"/>

    </person1>

</xsl:template>

<xsl:template match="address">

<xsl:if test="not(state='CT')">

    <xsl:copy-of select="../address"/>

    </xsl:if>

</xsl:template>

当您在提供的 xml 上应用上述转换时,它将产生以下结果 xml。

<person>
    <phone type="home">203-555-1212</phone>

    <phone type="fax">203-555-1212</phone>

    <firstName>Charles</firstName>

    <lastName>Smithington</lastName>

</person>

<person1>

    <phone type="home">58-99-44-999</phone>

    <phone type="fax">5788-9987-3365</phone>

    <firstName>Mike</firstName>

    <lastName>Shinoda</lastName>

    <address type="home">

        <street>12 Main Street</street>

        <city>Park Avenue</city>

        <state>NY</state>

        <zip>10025</zip>

    </address>

</person1>

于 2013-04-06T09:41:14.483 回答