在select语句中解析存储在表记录中的xml - SQL Server

发布于 2024-11-26 20:54:07 字数 1949 浏览 0 评论 0原文

我在尝试解析存储在表记录中的 xml 时遇到了问题 xml 结构如下:

<?xml version="1.0" encoding="utf-16"?>  
<WidgetsLayout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <WidgetsList>      
            <WidgetConfiguration>        
                <WidgetId>4</WidgetId>        
                <DockOrder>0</DockOrder>        
                <DockZoneId>0</DockZoneId>        
                <Width />        
                <Height />        
                <IsRemovable>true</IsRemovable>      
            </WidgetConfiguration>      
            <WidgetConfiguration>        
                <WidgetId>3</WidgetId>        
                <DockOrder>0</DockOrder>        
                <DockZoneId>1</DockZoneId>        
                <Width />        
                <Height />        
                <IsRemovable>true</IsRemovable>      
            </WidgetConfiguration>    
        </WidgetsList>  
    </WidgetsLayout>

xml 以 varchar 形式存储在表记录中。

我需要获取临时表,其中包含 xml 结构中不同的 WidgetId 集。

更新:

我确实编写了以下批处理语句来检索 WidgetConfiguration xml 字符串上的集合,因此我能够检索 WidgetId 集,但我遇到了插入语句的问题:

GO

    declare @dashboard_layout table (
        id int,
        config_xml xml
    )

    INSERT INTO @dashboard_layout(id)
        SELECT 
            widget_config.value('(WidgetId)[1]', 'int')
        FROM
            dbo.dashboard_configuration c
        CROSS APPLY 
            CAST(RIGHT(c.configuration_xml_string, LEN(c.configuration_xml_string) - 41), XML).nodes('/WidgetsLayout/WidgetsList/WidgetConfiguration') AS list(widget_config)

    select * from @dashboard_layout

I'在“cast”结果上调用“nodes”时,最后一个插入语句行出现语法错误

提前致谢。

I've run into a problem while trying to parse the xml which is stored in the table records
The xml structure is following :

<?xml version="1.0" encoding="utf-16"?>  
<WidgetsLayout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <WidgetsList>      
            <WidgetConfiguration>        
                <WidgetId>4</WidgetId>        
                <DockOrder>0</DockOrder>        
                <DockZoneId>0</DockZoneId>        
                <Width />        
                <Height />        
                <IsRemovable>true</IsRemovable>      
            </WidgetConfiguration>      
            <WidgetConfiguration>        
                <WidgetId>3</WidgetId>        
                <DockOrder>0</DockOrder>        
                <DockZoneId>1</DockZoneId>        
                <Width />        
                <Height />        
                <IsRemovable>true</IsRemovable>      
            </WidgetConfiguration>    
        </WidgetsList>  
    </WidgetsLayout>

And the xml is stored as varchar in the table records.

I need to get the temporary table which will contain distinct set of WidgetId from the xml structure.

UPDATED :

I did write the following batch statement to retrieve the set on WidgetConfiguration xml strings, so I would be able to retrieve WidgetId set, but I've run into a problem with the insert statement:

GO

    declare @dashboard_layout table (
        id int,
        config_xml xml
    )

    INSERT INTO @dashboard_layout(id)
        SELECT 
            widget_config.value('(WidgetId)[1]', 'int')
        FROM
            dbo.dashboard_configuration c
        CROSS APPLY 
            CAST(RIGHT(c.configuration_xml_string, LEN(c.configuration_xml_string) - 41), XML).nodes('/WidgetsLayout/WidgetsList/WidgetConfiguration') AS list(widget_config)

    select * from @dashboard_layout

I've got an syntax error in last insert statement line when calling 'nodes' on 'cast' result

Thanks in advance.

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

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

发布评论

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

评论(1

南风起 2024-12-03 20:54:07

尝试一下 - 这会起作用:

DECLARE @input XML = '<WidgetsLayout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <WidgetsList>      
            <WidgetConfiguration>        
                <WidgetId>4</WidgetId>        
                <DockOrder>0</DockOrder>        
                <DockZoneId>0</DockZoneId>        
                <Width />        
                <Height />        
                <IsRemovable>true</IsRemovable>      
            </WidgetConfiguration>      
            <WidgetConfiguration>        
                <WidgetId>3</WidgetId>        
                <DockOrder>0</DockOrder>        
                <DockZoneId>1</DockZoneId>        
                <Width />        
                <Height />        
                <IsRemovable>true</IsRemovable>      
            </WidgetConfiguration>    
        </WidgetsList>  
    </WidgetsLayout>'

SELECT
    WList.value('(WidgetId)[1]', 'int')
FROM
    @input.nodes('/WidgetsLayout/WidgetsList/WidgetConfiguration') AS Widget(WList)

所以基本上:

  • 将 XML 存储为数据类型 XML - 或者如果不能,则必须将 VARCHAR 列转换为XML 进行处理

  • 抓取// 节点列表作为“伪表”

  • 提取该伪表的每个成员中的 元素作为 INT

更新: 好的,要从表中执行此操作,请使用以下命令:

INSERT INTO @dashboard_layout(ID)
    SELECT 
        WList.value('(WidgetId)[1]', 'int')
    FROM
        dbo.dashboard_configuration c
    CROSS APPLY 
        CAST(c.YourColumn AS XML).nodes('/WidgetsLayout/WidgetsList/WidgetConfiguration') AS Widget(WList) 

Try this - this would work:

DECLARE @input XML = '<WidgetsLayout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <WidgetsList>      
            <WidgetConfiguration>        
                <WidgetId>4</WidgetId>        
                <DockOrder>0</DockOrder>        
                <DockZoneId>0</DockZoneId>        
                <Width />        
                <Height />        
                <IsRemovable>true</IsRemovable>      
            </WidgetConfiguration>      
            <WidgetConfiguration>        
                <WidgetId>3</WidgetId>        
                <DockOrder>0</DockOrder>        
                <DockZoneId>1</DockZoneId>        
                <Width />        
                <Height />        
                <IsRemovable>true</IsRemovable>      
            </WidgetConfiguration>    
        </WidgetsList>  
    </WidgetsLayout>'

SELECT
    WList.value('(WidgetId)[1]', 'int')
FROM
    @input.nodes('/WidgetsLayout/WidgetsList/WidgetConfiguration') AS Widget(WList)

So basically:

  • store your XML as datatype XML - or if you can't, you will have to convert your VARCHAR column to XML do to the processing

  • grab the list of <WidgetsLayout>/<WidgetsList>/<WidgetConfiguration> nodes as a "pseudo-table"

  • extract the <WidgetId> element from each of the members of that pseudo table as an INT

Update: OK, to do this from a table, use this:

INSERT INTO @dashboard_layout(ID)
    SELECT 
        WList.value('(WidgetId)[1]', 'int')
    FROM
        dbo.dashboard_configuration c
    CROSS APPLY 
        CAST(c.YourColumn AS XML).nodes('/WidgetsLayout/WidgetsList/WidgetConfiguration') AS Widget(WList) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文