我有一种特殊的XML格式。使用xquery后,我得到了奇怪的结果。结果不是XML字符串。如何解析这种XML格式?
declare @xml xml
set @xml='<Data>
<![CDATA[ <DATA>
<NVPAIRS>
<PAIR><NAME>status</NAME><VALUE>100</VALUE></PAIR>
<PAIR><NAME>date</NAME><VALUE>20001224</VALUE></PAIR>
<PAIR><NAME>name</NAME><VALUE>Tom</VALUE></PAIR>
</NVPAIRS>
</DATA>
]]>
</Data>
'
select x.query('.')
from @xml.nodes('//Data') as T(X)
我得到的结果:
<Data>
<DATA>
<NVPAIRS>
<PAIR><NAME>status</NAME><VALUE>a</VALUE></ PAIR>
<PAIR><NAME>date</NAME><VALUE>20001224</VALUE> </PAIR>
<PAIR><NAME></NAME><VALUE>12%3A30</VALUE></ PAIR>
</NVPAIRS>
</DATA>
</Data>
答案 0 :(得分:2)
As has been mentioned, you have to strip the non-xml from the beginning and end of your string before you can begin parsing. Here is an example of how you can do that with the sample you provided. You may need to tweak accordingly based on your actual XML data.
DECLARE @String VARCHAR(MAX)
DECLARE @XML XML
SET @String='<Data>
<![CDATA[ <DATA>
<NVPAIRS>
<PAIR><NAME>status</NAME><VALUE>100</VALUE></PAIR>
<PAIR><NAME>date</NAME><VALUE>20001224</VALUE></PAIR>
<PAIR><NAME>name</NAME><VALUE>Tom</VALUE></PAIR>
</NVPAIRS>
</DATA>
]]>
</Data>
'
SET @XML = LEFT(RIGHT(@String, LEN(@String) - 17), CHARINDEX(']]>', RIGHT(@String, LEN(@String) - 17)) - 1)
SELECT @XML.query('/DATA')
You'll notice that you now have a valid XML object against which to query.