替换 xml 的属性值中的特殊字符

replacing special characters in attribute values for xml

我需要更新 table 中的 xml 列以删除特定节点 RatingType="unknown" 的特殊字符 (%)。除上述以外的节点中存在的特殊字符不应更改。以下是我的输入和期望的结果。

            Input xml:
            <Ratings>
              <Rating Ratingtype="unknown">
                <AppliedKnowledge value="Yes%">3.0</AppliedKnowledge>
                <ToolSkills>3.5</ToolSkills>
              </Rating>
              <Rating Ratingtype="known">
              <AppliedKnowledge value="%Yes%">3.0</AppliedKnowledge>
                <Experience>9.5</Experience>
                <Education>16.0</Education>
                <DbDevelopment>4.5</DbDevelopment>
              </Rating>
              <Rating Ratingtype="unknown">
                <AppliedKnowledge value="%No%">4.0</AppliedKnowledge>
                <ToolSkills>4.5</ToolSkills>
              </Rating>
            </Ratings>

            Expected output xml:
            <Ratings>
              <Rating Ratingtype="unknown">
                <AppliedKnowledge value="Yes">3.0</AppliedKnowledge>
                <ToolSkills>3.5</ToolSkills>
              </Rating>
              <Rating Ratingtype="known">
              <AppliedKnowledge value="%Yes%">3.0</AppliedKnowledge>
                <Experience>9.5</Experience>
                <Education>16.0</Education>
                <DbDevelopment>4.5</DbDevelopment>
              </Rating>
              <Rating Ratingtype="unknown">
                <AppliedKnowledge value="No">4.0</AppliedKnowledge>
                <ToolSkills>4.5</ToolSkills>
              </Rating>
            </Ratings>

和你上一个问题一样,也不容易。

.modify() 方法允许每次调用一个更改。这意味着您必须使用某种循环。

还有办法:

如果 % 没有在其他任何地方使用,您可以简单地这样做:

将 XML 转换为 VARCHAR,进行简单的文本替换并将其转换回

SELECT CAST(REPLACE(CAST(@YourXml AS VARCHAR(MAX)),'%','')  AS XML)

比循环更好的是这个

将 XML 粉碎并重新构建。仅在目标值上使用 REPLACE

DECLARE @x XML=
'<Ratings>
  <Rating Ratingtype="unknown">
    <AppliedKnowledge value="Yes%">3.0</AppliedKnowledge>
    <ToolSkills>3.5</ToolSkills>
  </Rating>
  <Rating Ratingtype="known">
    <AppliedKnowledge value="%Yes%">3.0</AppliedKnowledge>
    <Experience>9.5</Experience>
    <Education>16.0</Education>
    <DbDevelopment>4.5</DbDevelopment>
  </Rating>
  <Rating Ratingtype="unknown">
    <AppliedKnowledge value="%No%">4.0</AppliedKnowledge>
    <ToolSkills>4.5</ToolSkills>
  </Rating>
</Ratings>';

SELECT CAST(REPLACE(CAST(@x AS VARCHAR(MAX)),'%','')  AS XML)

SELECT R.value('@Ratingtype','varchar(max)') AS [@Ratingtype]
      ,CASE WHEN R.value('@Ratingtype','varchar(max)')='unknown'
            THEN REPLACE(R.value('(AppliedKnowledge/@value)[1]','varchar(max)'),'%','')
            ELSE R.value('(AppliedKnowledge/@value)[1]','varchar(max)') END AS [AppliedKnowledge/@Value]
      ,R.value('AppliedKnowledge[1]','varchar(max)') AS [AppliedKnowledge]
      ,R.value('ToolSkills[1]','varchar(max)') AS [ToolSkills]
      ,R.value('Experience[1]','varchar(max)') AS [Experience]
      ,R.value('Education[1]','varchar(max)') AS [Education]
      ,R.value('DbDevelopment[1]','varchar(max)') AS [DbDevelopment]
FROM @x.nodes('/Ratings/Rating') AS A(R)
FOR XML PATH('Rating'),ROOT('Ratings')

结果

<Ratings>
  <Rating Ratingtype="unknown">
    <AppliedKnowledge Value="Yes">3.0</AppliedKnowledge>
    <ToolSkills>3.5</ToolSkills>
  </Rating>
  <Rating Ratingtype="known">
    <AppliedKnowledge Value="%Yes%">3.0</AppliedKnowledge>
    <Experience>9.5</Experience>
    <Education>16.0</Education>
    <DbDevelopment>4.5</DbDevelopment>
  </Rating>
  <Rating Ratingtype="unknown">
    <AppliedKnowledge Value="No">4.0</AppliedKnowledge>
    <ToolSkills>4.5</ToolSkills>
  </Rating>
</Ratings>