Как исключить дочерние элементы в xml?

У меня есть следующий 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, а адрес человека, для которого указан штат Нью-Йорк.

1 ответ

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

Другие вопросы по тегам