【问题标题】:Parsing xml stored in table records in the select statement - SQL Server解析select语句中存储在表记录中的xml - SQL Server
【发布时间】:2011-07-26 19:37:32
【问题描述】:

我在尝试解析存储在表记录中的 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

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

提前致谢。

【问题讨论】:

    标签: sql-server-2008 xml-parsing


    【解决方案1】:

    试试这个 - 这会奏效:

    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 以进行处理

    • 获取&lt;WidgetsLayout&gt;/&lt;WidgetsList&gt;/&lt;WidgetConfiguration&gt; 节点列表作为“伪表”

    • 从该伪表的每个成员中提取&lt;WidgetId&gt; 元素作为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) 
    

    【讨论】:

    • 问题是我在dashboard_configuration表中有一组varchar类型的行包含这个xml结构。因此,我将dashboard_configuration 中的所有xml 字符串插入到@dashboard_layout 表中,然后我解析了它们中的每一个并检索了所有找到的Widget_id。
    • 我已经尝试过您的示例,但在第二个 '.' 处出现语法错误。在最后一行
    • 在 xml 类型的数据字段上调用“节点”时有效。必须编写内部选择语句。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 2020-12-12
    • 1970-01-01
    相关资源
    最近更新 更多