这在 XSLT 中很容易(而且安全)...
XML 输入(包装<Customers>
成格式良好)
<Customers>
<Customer>
<CustomerType>Business</CustomerType>
<CreditRating>Good</CreditRating>
</Customer>
<Customer>
<CustomerType>Residential</CustomerType>
<CreditRating>Good</CreditRating>
</Customer>
</Customers>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Customer[CustomerType='Residential']/CreditRating">
<CreditRating>Bad</CreditRating>
</xsl:template>
</xsl:stylesheet>
输出 XML
<Customers>
<Customer>
<CustomerType>Business</CustomerType>
<CreditRating>Good</CreditRating>
</Customer>
<Customer>
<CustomerType>Residential</CustomerType>
<CreditRating>Bad</CreditRating>
</Customer>
</Customers>