如何通过删除空格来查找标记值

时间:2014-08-25 08:26:41

标签: sql sql-server xml xml-parsing xml-serialization

declare @x xml = '<XML> <UNSTRING STATEMENTNUMBER="48515">
          <STRING>CRD001M-OPTIONI</STRING>
          <DELIMITERS>
            <DELIMITER>aaa</DELIMITER>
          </DELIMITERS>
          <SUBSTRINGS>WS-MENU-STACK1 WS-MENU-STACK2 WS-MENU-STACK3 WS-MENU-STACK4 WS-MENU-STACK5 WS-MENU-STACK6 WS-MENU-STACK7 WS-MENU-STACK8 WS-MENU-STACK9 WS-MENU-STACK10 TALLYING IN WS-MENU-STACK-TALLY</SUBSTRINGS>
          <DELIMITERSFOUND>
            <DELIMITER>
            </DELIMITER>
          </DELIMITERSFOUND>
          <INDICES>
            <INDEX>
            </INDEX>
          </INDICES>
          <POINTER>
          </POINTER>
          <TALLYING>
          </TALLYING>
          <ONOVERFLOW>
          </ONOVERFLOW>
        </UNSTRING></XML>'

在上面的XML标签中有空间隔离的值 我想在下面的字符串下面做

declare @x xml = '<XML> <UNSTRING STATEMENTNUMBER="48515">
          <STRING>CRD001M-OPTIONI</STRING>
          <DELIMITERS>
            <DELIMITER>aaa</DELIMITER>
          </DELIMITERS>
            <SUBSTRINGS>WS-MENU-STACK1</SUBSTRINGS>
            <SUBSTRINGS>WS-MENU-STACK2</SUBSTRINGS>
            <SUBSTRINGS>WS-MENU-STACK3</SUBSTRINGS>
            <SUBSTRINGS>WS-MENU-STACK4</SUBSTRINGS>
            <SUBSTRINGS>WS-MENU-STACK5</SUBSTRINGS>
            <SUBSTRINGS>WS-MENU-STACK6</SUBSTRINGS>
            <SUBSTRINGS>WS-MENU-STACK7</SUBSTRINGS>
            <SUBSTRINGS>WS-MENU-STACK8</SUBSTRINGS>
            <SUBSTRINGS>WS-MENU-STACK9</SUBSTRINGS>
            <SUBSTRINGS>WS-MENU-STACK10</SUBSTRINGS>
            <SUBSTRINGS>TALLYING</SUBSTRINGS>
            <SUBSTRINGS>IN</SUBSTRINGS>
            <SUBSTRINGS>WS-MENU-STACK-TALLY</SUBSTRINGS>
          <DELIMITERSFOUND>
            <DELIMITER>
            </DELIMITER>
          </DELIMITERSFOUND>
          <INDICES>
            <INDEX>
            </INDEX>
          </INDICES>
          <POINTER>
          </POINTER>
          <TALLYING>
          </TALLYING>
          <ONOVERFLOW>
          </ONOVERFLOW>
        </UNSTRING></XML>'

请帮助我在这里如何通过替换或任何其他事情解决问题 我使用了替换功能,但无法找到值 此处<substrings>标记值按空格隔离,因此查找每个值都在<substrings> value </substrings>之间

1 个答案:

答案 0 :(得分:0)

使用以下查询并享受它的结果:

declare @x xml = '<XML> <UNSTRING STATEMENTNUMBER="48515">
          <STRING>CRD001M-OPTIONI</STRING>
          <DELIMITERS>
            <DELIMITER>aaa</DELIMITER>
          </DELIMITERS>
          <SUBSTRINGS>WS-MENU-STACK1 WS-MENU-STACK2 WS-MENU-STACK3 WS-MENU-STACK4 WS-MENU-STACK5 WS-MENU-STACK6 WS-MENU-STACK7 WS-MENU-STACK8 WS-MENU-STACK9 WS-MENU-STACK10 TALLYING IN WS-MENU-STACK-TALLY</SUBSTRINGS>
          <DELIMITERSFOUND>
            <DELIMITER>
            </DELIMITER>
          </DELIMITERSFOUND>
          <INDICES>
            <INDEX>
            </INDEX>
          </INDICES>
          <POINTER>
          </POINTER>
          <TALLYING>
          </TALLYING>
          <ONOVERFLOW>
          </ONOVERFLOW>
        </UNSTRING></XML>'

DECLARE @T NVARCHAR(MAX) = CAST(@X AS NVARCHAR(MAX))
SELECT CAST(
            REPLACE(@T,
                    '<SUBSTRINGS>'+D.value('.','VARCHAR(max)')+'</SUBSTRINGS>',
                    '<SUBSTRINGS>'+REPLACE(D.value('.','VARCHAR(max)'),' ','</SUBSTRINGS><SUBSTRINGS>')+'</SUBSTRINGS>'
                    )
        AS XML)
FROM @x.nodes('/XML/UNSTRING/SUBSTRINGS') X(D)