sql xml中的感叹号

时间:2013-05-09 13:16:13

标签: sql xml

尝试添加带有非法字符的元素时遇到问题。我有以下SQL

SELECT  
'en-GB' AS '@xml:lang',
'20120217164611.1712._3FO0ZXYQ3@GBRAPPCHL1' AS '@payloadID',
'2012-02-17T16:46:11' AS '@timestamp',
'1.2.014' AS '@version',
  ( SELECT
     ( SELECT /* FROM */
        (SELECT 'TecSolExchangeID' AS 'Credential/@domain', 33333 AS 'Credential/Identity'
         FOR XML PATH(''), TYPE),
        (SELECT 'name' AS 'Credential/@domain', 'Test Limited' AS 'Credential/Identity'
         FOR XML PATH(''), TYPE)
      FOR XML PATH('From'), TYPE ),
     ( SELECT /* TO */
        (SELECT 'TecSolExchangeID' AS 'Credential/@domain', 66666 AS 'Credential/Identity'
         FOR XML PATH(''), TYPE),
        (SELECT 'name' AS 'Credential/@domain', 'Test Company' AS 'Credential/Identity'
         FOR XML PATH(''), TYPE)
      FOR XML PATH('To'), TYPE ),
     ( SELECT /* SENDER */
        (SELECT 'TecSolExchangeID' AS 'Credential/@domain', 13041 AS 'Credential/Identity', '' AS 'Credential/SharedSecret'
         FOR XML PATH(''), TYPE), ' cXML Link v1.3.0' AS 'UserAgent'
      FOR XML PATH('Sender'), TYPE )
   FROM TEST2.PORDER
   WHERE POHNUM_0 = '2011302POH00000002'
     FOR XML PATH('Header'),TYPE )
     ,   ( SELECT 'production' AS '@deploymentMode',
     ( SELECT
                   ( SELECT /* ShipNoticeHeader */
              (SELECT ''
               FOR XML PATH(''),
                   TYPE)
            FOR XML PATH('ShipNoticeHeader'),
                TYPE ),
           ( SELECT  '' AS '@domain',
              '' as 'CarrierIdentfier',
                      ''AS 'ShipmentIdentifier'
            FOR XML PATH('ShipControl'),
                TYPE ),
                                 (SELECT /* Ship Notice Portion */
                    (SELECT '1;29117/42' AS 'DocumentReference/@PayLoadID',
                            '' AS 'DocumentReference'
                     FOR XML PATH('OrderReference'),
                         TYPE),
                                             (SELECT '4' AS '@Quantity',
                                             '1' AS '@LineNo',
                            'Box 4' AS 'UnitOfMeasure'
                     FOR XML PATH('ShipNoticeItem'),
                         TYPE)
                  FOR XML PATH('ShipNoticePortion'),
                      TYPE)

      FROM TEST2.PORDER
      WHERE POHNUM_0 = '2011302POH00000002'
        FOR XML PATH('ShipNoticeRequest'),
            TYPE )
   FROM TEST2.PORDER
   WHERE POHNUM_0 = '2011302POH00000002'
     FOR XML PATH('Request'),
         TYPE)
FROM TEST2.PORDER
WHERE POHNUM_0 = '2011302POH00000002'
  FOR XML PATH('?XML')

正如您所看到的,我在根域中有一个问号。这失败了,错误。 '?XML'包含FOR XML所需的无效XML标识符; '?'(0x003F)是故障的第一个字符'。

我的问题是我需要能够创建包含非法字符的元素。例如,我还需要围绕消息的另一个元素“<!DOCTYPE>”。同样的情况,除了这个字段有感叹号。

这可能是先关闭,如果是这样,任何人都可以帮我完成它。

提前致谢

1 个答案:

答案 0 :(得分:0)

您无法在For XML中执行此操作,但是您可以通过结合FOR XMLNVARCHAR操作来实现此目的。

因此,如果您想添加DocType(因为您实际上正在生成HTML),您可以这样做:

select '<!DOCTYPE html>'
+ cast(
(
    -- XML bit here
    -- Replace this with your XML query.

    select 
        A 
    from (
        select 'a1' as A 
        union select 'a2'
        ) a
        for xml path(''), root('Z'), type
)
 as nvarchar(Max))

但请注意,结果是适合SQL Server重新导入的XML。

<强>讨论

XML和HTML是相关的(两者都是SGML的类型),但不一样。 HTML具有XML不具备的功能,而XML具有HTML不具备的功能。

您可以编写XML的子集,这也是HTML的一个子集,这通常很有用,但两者都不是另一个的子集。