2011-10-17 118 views
0

我有這個XML數據。在xml sqlserver中解析html標籤

 set @PROGRAM_XML ='<PROGRAM> 
     <ORG_GUID>FA9DC609-B8C0-4943-84E0-9D7BC0BC1027</ORG_GUID> 
    <LONG_DESCRIPTION><p> 
    <u><em>hi</em></u></p></LONG_DESCRIPTION> 
    <SHORT_DESCRIPTION><p> 
    <strong>hi</strong></p></SHORT_DESCRIPTION> 
    </PROGRAM>' 

選擇我得到的數據是,當我使用

 EXEC SP_XML_PREPAREDOCUMENT @XMLHANDLER OUTPUT, @PROGRAM_XML 

    SELECT 
     ORG_GUID 
     ,LONG_DESCRIPTION 
     ,SHORT_DESCRIPTION 
    FROM 
     OPENXML(@XMLHANDLER,'/PROGRAM',2) 
    WITH 
    (
     ORG_GUID UNIQUEIDENTIFIER 
     ,LONG_DESCRIPTION VARCHAR(2000) 
     ,SHORT_DESCRIPTION VARCHAR(2000) 
    ) 

ORG_GUID               LONG_DESCRIPTION    SHORT_DESCRIPTION      
     FA9DC609-B8C0-4943-84E0-9D7BC0BC1027      hi             hi 

但我想它作爲

 <ORG_GUID>               LONG_DESCRIPTION        SHORT_DESCRIPTION      
     FA9DC609-B8C0-4943-84E0-9D7BC0BC1027   <p><u><em>hi</em></u></p>  <p><strong>hi</strong></p> 

爲什麼它忽視了內在的HTML標籤。任何人都可以給我一個答案。

感謝

回答

0

如果您的SQL Server版本是2005及更高版本,可以使用這樣的代碼:

SELECT 
    b.value('ORG_GUID[1]','UNIQUEIDENTIFIER') AS ORG_GUID, 
    CAST(b.query('LONG_DESCRIPTION/*') AS NVARCHAR(2000)) AS LONG_DESCRIPTION, 
    CAST(b.query('SHORT_DESCRIPTION/*') AS NVARCHAR(2000)) AS SHORT_DESCRIPTION 
    FROM @PROGRAM_XML.nodes('//PROGRAM') a(b)