使用 CDATA 的 SQL Server XML 输出

发布于 2024-08-05 16:18:56 字数 434 浏览 9 评论 0原文

有没有办法让 SQL Server XML 返回使用 CDATA?我有 SQL Server 返回的 XML,如下所示:

<locations>
    <site id="124">
        <sitename>Texas A &amp; M</sitename>
    </site>
</locations>

当我需要这样时:

<locations>
    <site id="124">
        <sitename><![CDATA[Texas A & M]]></sitename>
    </site>
</locations>

Is there a way to have an SQL Server XML return use CDATA? I have XML being returned by SQL Server like this:

<locations>
    <site id="124">
        <sitename>Texas A & M</sitename>
    </site>
</locations>

When I am required to have this:

<locations>
    <site id="124">
        <sitename><![CDATA[Texas A & M]]></sitename>
    </site>
</locations>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悍妇囚夫 2024-08-12 16:18:56

查看 FOR XML EXPLICIT 的选项(参数Directive< /em>)。它提供了更大程度的控制,您还可以指定 CDATA。这是一个很好的教程

代码改编自该教程:

declare @agent table
(    
    AgentID int,
    Fname varchar(5),
    SSN varchar(11)
)

insert into @agent
select 1, 'Vimal', '123-23-4521' union all
select 2, 'Jacob', '321-52-4562' union all
select 3, 'Tom', '252-52-4563'

SELECT
    1 AS Tag,
    NULL AS Parent,
    NULL AS 'Agents!1!',
    NULL AS 'Agent!2!AgentID',
    NULL AS 'Agent!2!Fname!Element',
    NULL AS 'Agent!2!SSN!cdata'
UNION ALL
SELECT
    2 AS Tag,
    1 AS Parent,
    NULL, 
    AgentID,
    Fname,
    SSN
FROM @agent
FOR XML EXPLICIT

Look at the options of FOR XML EXPLICIT (parameter Directive). It gives the greater degree of control and you can also specify CDATA. Here is a good tutorial.

And the code addapted from that tutorial:

declare @agent table
(    
    AgentID int,
    Fname varchar(5),
    SSN varchar(11)
)

insert into @agent
select 1, 'Vimal', '123-23-4521' union all
select 2, 'Jacob', '321-52-4562' union all
select 3, 'Tom', '252-52-4563'

SELECT
    1 AS Tag,
    NULL AS Parent,
    NULL AS 'Agents!1!',
    NULL AS 'Agent!2!AgentID',
    NULL AS 'Agent!2!Fname!Element',
    NULL AS 'Agent!2!SSN!cdata'
UNION ALL
SELECT
    2 AS Tag,
    1 AS Parent,
    NULL, 
    AgentID,
    Fname,
    SSN
FROM @agent
FOR XML EXPLICIT
夜无邪 2024-08-12 16:18:56

以下是我获取 CDATA 的方法的示例:

DECLARE @GlobalDateFormat varchar(32)
SET @GlobalDateFormat = 'MM/dd/yyyy hh:mm tt'

DECLARE @xml XML

SET @xml = 
(
    SELECT
            SegmentId         = ISNULL(SegmentId,0)
        ,   DocumentId        = ISNULL(DocumentId,0)
        ,   Title             = CAST(Core.dbo.fCharFormat('xmlCDATA',Title,DEFAULT,'') AS xml)
        ,   DocumentShortName = CAST(Core.dbo.fCharFormat('xmlCDATA',DocumentShortName,DEFAULT,'') AS xml)
        ,   [FileName]        = CAST(Core.dbo.fCharFormat('xmlCDATA',[FileName],DEFAULT,'') AS xml)
        ,   [Path]            = CAST(Core.dbo.fCharFormat('xmlCDATA',[Path],DEFAULT,'') AS xml)
        ,   CreateDate        = ISNULL(Core.dbo.fDateFormat(@GlobalDateFormat,CreateDate),Core.dbo.fDateFormat(@GlobalDateFormat,GETDATE()))
        ,   ModificationDate  = ISNULL(Core.dbo.fDateFormat(@GlobalDateFormat,ModificationDate),Core.dbo.fDateFormat(@GlobalDateFormat,GETDATE()))
        ,   TemplateId        = ISNULL(CAST(TemplateId AS varchar(16)),'')
        ,   IsRoot            = ISNULL(IsRoot,0)
        ,   IsActive          = ISNULL(IsActive,0)
        ,   SortOrdinal       = ISNULL(CAST(SortOrdinal AS varchar(16)),'')
        ,   ClientId          = ISNULL(ClientId,'')
        ,   Tag               = CAST(Core.dbo.fCharFormat('xmlCDATA',Tag,DEFAULT,'') AS xml)
        FROM
            Document
        WHERE
            DocumentId = 9073
        FOR XML AUTO, ELEMENTS
)

SELECT @xml

以下是来自我的类​​似黄鼠狼的 CDATA 处理函数的相关详细信息:

IF @cmdName = 'xmlCDATA'
BEGIN
    IF @chars IS NULL
    BEGIN
        SET @charsOut = @charsDefault
    END
    ELSE
    BEGIN
        SET @chars = REPLACE(@chars,'<![CDATA[','')
        SET @chars = REPLACE(@chars,']]>','')
        SET @charsOut = '<![CDATA[' + @chars + ']]>'
    END
END

Here is an example of my way of getting CDATA:

DECLARE @GlobalDateFormat varchar(32)
SET @GlobalDateFormat = 'MM/dd/yyyy hh:mm tt'

DECLARE @xml XML

SET @xml = 
(
    SELECT
            SegmentId         = ISNULL(SegmentId,0)
        ,   DocumentId        = ISNULL(DocumentId,0)
        ,   Title             = CAST(Core.dbo.fCharFormat('xmlCDATA',Title,DEFAULT,'') AS xml)
        ,   DocumentShortName = CAST(Core.dbo.fCharFormat('xmlCDATA',DocumentShortName,DEFAULT,'') AS xml)
        ,   [FileName]        = CAST(Core.dbo.fCharFormat('xmlCDATA',[FileName],DEFAULT,'') AS xml)
        ,   [Path]            = CAST(Core.dbo.fCharFormat('xmlCDATA',[Path],DEFAULT,'') AS xml)
        ,   CreateDate        = ISNULL(Core.dbo.fDateFormat(@GlobalDateFormat,CreateDate),Core.dbo.fDateFormat(@GlobalDateFormat,GETDATE()))
        ,   ModificationDate  = ISNULL(Core.dbo.fDateFormat(@GlobalDateFormat,ModificationDate),Core.dbo.fDateFormat(@GlobalDateFormat,GETDATE()))
        ,   TemplateId        = ISNULL(CAST(TemplateId AS varchar(16)),'')
        ,   IsRoot            = ISNULL(IsRoot,0)
        ,   IsActive          = ISNULL(IsActive,0)
        ,   SortOrdinal       = ISNULL(CAST(SortOrdinal AS varchar(16)),'')
        ,   ClientId          = ISNULL(ClientId,'')
        ,   Tag               = CAST(Core.dbo.fCharFormat('xmlCDATA',Tag,DEFAULT,'') AS xml)
        FROM
            Document
        WHERE
            DocumentId = 9073
        FOR XML AUTO, ELEMENTS
)

SELECT @xml

Here is the relevant detail from my weasel-like, CDATA-handling function:

IF @cmdName = 'xmlCDATA'
BEGIN
    IF @chars IS NULL
    BEGIN
        SET @charsOut = @charsDefault
    END
    ELSE
    BEGIN
        SET @chars = REPLACE(@chars,'<![CDATA[','')
        SET @chars = REPLACE(@chars,']]>','')
        SET @charsOut = '<![CDATA[' + @chars + ']]>'
    END
END
記柔刀 2024-08-12 16:18:56

正如乔尔在上面的评论中提到的,这两种形式应该意味着完全相同的事情。但是,如果确实需要 CDATA 形式,那么您可以编写一个后处理器,将第一个形式作为输入并使用 CDATA 输出第二个形式。

执行此操作时,后处理器将对第一种形式中的 XML 转义数据进行取消编码,并使用 CDATA 兼容方法对其进行重新编码。请参阅问题有没有办法转义 xml 中的 CDATA 结束标记? 有关 CDATA 转义的注意事项。

As Joel mentioned in the comment above, those two forms should mean exactly the same thing. However, if the CDATA form is really required, then you could write a post-processor that takes the first form as input and outputs the second form using CDATA.

When doing this, a post-processor would unencode the XML-escaped data in the first form, and re-encode it using a CDATA-compatible method. See the question Is there a way to escape a CDATA end token in xml? for considerations regarding CDATA escaping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文