Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQLSERVER-导入和解析来自wordpress的特殊XML_Sql Server_Xml_Tsql_Xml Parsing - Fatal编程技术网

Sql server SQLSERVER-导入和解析来自wordpress的特殊XML

Sql server SQLSERVER-导入和解析来自wordpress的特殊XML,sql-server,xml,tsql,xml-parsing,Sql Server,Xml,Tsql,Xml Parsing,我编写了一个脚本,用于加载xml文件并对其进行解析 以下是脚本和部分xml文件示例: SQL脚本 XML文件(部分) 俄罗斯方块 ... 结构同上 我面临的问题 我需要列出所有产品以及所有相关的类别,此时脚本只返回第一个类别的所有产品 我不知道如何返回每个产品的所有PosteTa(键、值)列表 希望它清楚, 谢谢大家的支持 我对OPENXML很生疏,但你不需要它。让我们从一些更新的示例数据开始: INSERT INTO #XMLwithOpenXML(XMLData, LoadedDateT

我编写了一个脚本,用于加载xml文件并对其进行解析

以下是脚本和部分xml文件示例:

SQL脚本 XML文件(部分)

俄罗斯方块
... 结构同上
我面临的问题

  • 我需要列出所有产品以及所有相关的类别,此时脚本只返回第一个类别的所有产品
  • 我不知道如何返回每个产品的所有PosteTa(键、值)列表
  • 希望它清楚,
    谢谢大家的支持

    我对OPENXML很生疏,但你不需要它。让我们从一些更新的示例数据开始:

    INSERT INTO #XMLwithOpenXML(XMLData, LoadedDateTime)
    SELECT 
    '<?xml version="1.0" encoding="UTF-8" ?>
    <rss version="2.0"
        xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
        xmlns:content="http://purl.org/rss/1.0/modules/content/"
        xmlns:wfw="http://wellformedweb.org/CommentAPI/"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:wp="http://wordpress.org/export/1.2/"
    >
    <channel>
        <item>
            <title>Bancone Tetris</title>
            <category domain="categorie-prodotti" nicename="arredi-light"><![CDATA[Arredi light]]></category>
            <category domain="categorie-outlet" nicename="arredi-light"><![CDATA[arredi light]]></category>
            <category domain="categorie-prodotti" nicename="banconi"><![CDATA[Banconi]]></category>
            <category domain="categorie-outlet" nicename="banconi"><![CDATA[Banconi]]></category>
            <category domain="categorie-outlet" nicename="luci"><![CDATA[luci]]></category>
            <wp:postmeta>
                <wp:meta_key><![CDATA[wpcf-codice]]></wp:meta_key>
                <wp:meta_value><![CDATA[cod.05-008]]></wp:meta_value>
            </wp:postmeta>
            <wp:postmeta>
                <wp:meta_key><![CDATA[wpcf-size]]></wp:meta_key>
                <wp:meta_value><![CDATA[145x80xh110 cm]]></wp:meta_value>
            </wp:postmeta>
        </item>
        <item>
            <title>Bancone Tetris Part2</title>
            <category domain="categorie-outlet" nicename="banconi"><![CDATA[Banconi]]></category>
            <category domain="categorie-outlet" nicename="luci"><![CDATA[luci]]></category>
            <wp:postmeta>
                <wp:meta_key><![CDATA[wpcf-codice]]></wp:meta_key>
                <wp:meta_value><![CDATA[cod.05-008]]></wp:meta_value>
            </wp:postmeta>
            <wp:postmeta>
                <wp:meta_key><![CDATA[wpcf-size]]></wp:meta_key>
                <wp:meta_value><![CDATA[145x80xh110 cm]]></wp:meta_value>
            </wp:postmeta>
        </item>
    </channel>
    </rss>' AS BulkColumn, GETDATE();
    
    结果

    product                    category
    -------------------------- --------------
    Bancone Tetris             Arredi light
    Bancone Tetris             arredi light
    Bancone Tetris             Banconi
    Bancone Tetris             Banconi
    Bancone Tetris             luci
    Bancone Tetris Part2       Banconi
    Bancone Tetris Part2       luci
    
    对于wp:posteta的内容,您需要参考wp名称空间。这里有三种方法(请注意我的评论):

    所有三种解决方案都返回:

    product              category             meta_key             meta_val
    -------------------- -------------------- -------------------- --------------------
    Bancone Tetris       Arredi light         wpcf-codice          cod.05-008
    Bancone Tetris       Arredi light         wpcf-size            145x80xh110 cm
    Bancone Tetris       arredi light         wpcf-codice          cod.05-008
    Bancone Tetris       arredi light         wpcf-size            145x80xh110 cm
    Bancone Tetris       Banconi              wpcf-codice          cod.05-008
    Bancone Tetris       Banconi              wpcf-size            145x80xh110 cm
    Bancone Tetris       Banconi              wpcf-codice          cod.05-008
    Bancone Tetris       Banconi              wpcf-size            145x80xh110 cm
    Bancone Tetris       luci                 wpcf-codice          cod.05-008
    Bancone Tetris       luci                 wpcf-size            145x80xh110 cm
    Bancone Tetris Part2 Banconi              wpcf-codice          cod.05-008
    Bancone Tetris Part2 Banconi              wpcf-size            145x80xh110 cm
    Bancone Tetris Part2 luci                 wpcf-codice          cod.05-008
    Bancone Tetris Part2 luci                 wpcf-size            145x80xh110 cm
    
    SELECT 
      product  = item.value('(title/text())[1]', 'varchar(1000)'),
      category = category.value('(text())[1]', 'varchar(1000)')
    FROM #XMLwithOpenXML x
    CROSS APPLY x.XMLData.nodes('rss/channel/item') n1(item)
    CROSS APPLY item.nodes('category')   n2(category);
    
    product                    category
    -------------------------- --------------
    Bancone Tetris             Arredi light
    Bancone Tetris             arredi light
    Bancone Tetris             Banconi
    Bancone Tetris             Banconi
    Bancone Tetris             luci
    Bancone Tetris Part2       Banconi
    Bancone Tetris Part2       luci
    
    -- Option #1: USE "*:" for "all namespaces (MY FAVORITE)
    SELECT 
      product  = item.value('(title/text())[1]', 'varchar(1000)'),
      category = category.value('(text())[1]', 'varchar(1000)'),
      meta_key = postmeta.value('(*:meta_key/text())[1]', 'varchar(1000)'),
      meta_val = postmeta.value('(*:meta_value/text())[1]', 'varchar(1000)')
    FROM #XMLwithOpenXML x
    CROSS APPLY x.XMLData.nodes('rss/channel/item') n1(item)
    CROSS APPLY item.nodes('category')   n2(category)
    CROSS APPLY item.nodes('*:postmeta') n3(postmeta);
    
    -- Option #2: USE "WITH XMLNAMESPACES"
    WITH XMLNAMESPACES ('http://wordpress.org/export/1.2/' as wp) 
    SELECT 
      product  = item.value('(title/text())[1]', 'varchar(1000)'),
      category = category.value('(text())[1]', 'varchar(1000)'),
      meta_key = postmeta.value('(wp:meta_key/text())[1]', 'varchar(1000)'),
      meta_val = postmeta.value('(wp:meta_value/text())[1]', 'varchar(1000)')
    FROM #XMLwithOpenXML x
    CROSS APPLY x.XMLData.nodes('rss/channel/item') n1(item)
    CROSS APPLY item.nodes('category')   n2(category)
    CROSS APPLY item.nodes('wp:postmeta') n3(postmeta);
    
    -- Option #3: Inline Namespace declaration
    SELECT 
      product  = item.value('(title/text())[1]', 'varchar(1000)'),
      category = category.value('(text())[1]', 'varchar(1000)'),
      meta_key = postmeta.value('declare namespace wp="http://wordpress.org/export/1.2/"; (wp:meta_key/text())[1]', 'varchar(1000)'),
      meta_val = postmeta.value('declare namespace wp="http://wordpress.org/export/1.2/"; (wp:meta_value/text())[1]', 'varchar(1000)')
    FROM #XMLwithOpenXML x
    CROSS APPLY x.XMLData.nodes('rss/channel/item') n1(item)
    CROSS APPLY item.nodes('category')   n2(category)
    CROSS APPLY item.nodes('declare namespace wp="http://wordpress.org/export/1.2/"; wp:postmeta') n3(postmeta);
    
    product              category             meta_key             meta_val
    -------------------- -------------------- -------------------- --------------------
    Bancone Tetris       Arredi light         wpcf-codice          cod.05-008
    Bancone Tetris       Arredi light         wpcf-size            145x80xh110 cm
    Bancone Tetris       arredi light         wpcf-codice          cod.05-008
    Bancone Tetris       arredi light         wpcf-size            145x80xh110 cm
    Bancone Tetris       Banconi              wpcf-codice          cod.05-008
    Bancone Tetris       Banconi              wpcf-size            145x80xh110 cm
    Bancone Tetris       Banconi              wpcf-codice          cod.05-008
    Bancone Tetris       Banconi              wpcf-size            145x80xh110 cm
    Bancone Tetris       luci                 wpcf-codice          cod.05-008
    Bancone Tetris       luci                 wpcf-size            145x80xh110 cm
    Bancone Tetris Part2 Banconi              wpcf-codice          cod.05-008
    Bancone Tetris Part2 Banconi              wpcf-size            145x80xh110 cm
    Bancone Tetris Part2 luci                 wpcf-codice          cod.05-008
    Bancone Tetris Part2 luci                 wpcf-size            145x80xh110 cm