regex-查找与另一个标记值一起出现的XML标记

regex-查找与另一个标记值一起出现的XML标记,regex,Regex,假设我有XML: BusinessGood ResidentialGood 我想把住宅消费者的信用评级从好改为坏 仅当CreditRating标记出现在具有住宅客户类型的客户记录中时,我可以使用哪个正则表达式搜索词来查找(并因此替换)CreditRating标记 我知道我可以使用: *?住宅。*?匹配整个记录住宅客户记录,但我只想匹配客户记录中的CreditRating标记,以便我可以进行搜索和替换 非常感谢 :-)这在XSLT中很简单(而且安全) XML输入(包装在中,格式良好) 商业 好的

假设我有XML:
BusinessGood
ResidentialGood

我想把住宅消费者的信用评级从好改为坏

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

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

非常感谢

:-)

这在XSLT中很简单(而且安全)

XML输入(包装在
中,格式良好)


商业
好的
住宅的
好的
XSLT1.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>

商业
好的
住宅的
令人不快的
这在XSLT中很简单(而且安全)

XML输入(包装在
中,格式良好)


商业
好的
住宅的
好的
XSLT1.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>

商业
好的
住宅的
令人不快的

添加您尝试过的精确编程标记并发布您尝试过的内容?尝试使用正则表达式解析XML通常是个坏主意。。。XSLT或XQuery是更好的选择。致命错误-在实际的XML中,客户类型是一个子节点,因此不确定如何在XQuery中匹配此子节点..:-(添加您尝试过的确切编程标记,并发布您尝试过的内容?尝试使用正则表达式解析XML通常是个坏主意…XSLT或XQuery是更好的选择。致命错误-在实际XML中,客户类型是一个子节点,因此不确定如何在XQuery中匹配此项。)-(