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 に答える