2

假设我有 XML: <Customer><CustomerType>Business</CustomerType><CreditRating>Good</CreditRating></Customer> <Customer><CustomerType>Residential</CustomerType><CreditRating>Good</CreditRating></Customer>

我想将住宅消费者的所有信用评级从好更改为坏。

仅当发生在具有住宅客户类型的客户记录中时,我可以使用什么正则表达式搜索词来查找(并因此替换)CreditRating 标签?

我知道我可以使用: <Customer>.*?<CustomerType>Residential.*?</Customer>匹配整个记录住宅客户记录,但我只想匹配客户记录中的 CreditRating 标签,以便进行搜索和替换。

非常感谢,

:-)

4

1 回答 1

0

这在 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>
于 2012-08-30T04:39:07.437 回答