1

我在解析 SQL Server 中的 XML 元素和属性时遇到问题。

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root>
  <dataModel>
    <repo name="SKU" profile="SKU"/>
  </dataModel>
  <SKU repo="SKU">
    <SKU_SKU_Number>ABCDEFG</SKU_SKU_Number>
    <SKU_Advanced_Feature_1 mLang="1">
      <value lang="en">SKU_Advanced_Feature1</value>
      <value lang="da-DK">SKU_Advanced_Feature2</value>
      <value lang="mal">SKU_Advanced_Feature3</value>
    </SKU_Advanced_Feature_1>
    <SKU_Advanced_Feature_2 mLang="1">
      <value lang="en"> </value>
      <value lang="da-DK"> </value>
    </SKU_Advanced_Feature_2>
    <SKU_Advanced_Feature_3 mLang="1">
      <value lang="en"> </value>
      <value lang="da-DK"> </value>
    </SKU_Advanced_Feature_3>
    <SKU_Advanced_Feature_4 mLang="1">
      <value lang="en"> </value>
      <value lang="da-DK"> </value>
    </SKU_Advanced_Feature_4>
    <SKU_Advanced_Feature_5 mLang="1">
      <value lang="en"> </value>
      <value lang="da-DK"> </value>
    </SKU_Advanced_Feature_5>
    <SKU_Application mLang="1">
      <value lang="en">Dehumidification and Internal Air Circulation</value>
      <value lang="da-DK">Testing SKU Application</value>
    </SKU_Application>
    <SKU_Approvals_and_Standards mLang="1">
      <value lang="en">ETL listed, UL474</value>
      <value lang="da-DK"/>
    </SKU_Approvals_and_Standards>
    <SKU_Approvals_and_Standards mLang="1">
      <value lang="en">CSA 22.2 No. 92</value>
      <value lang="da-DK"/>
    </SKU_Approvals_and_Standards>
    <SKU_Basic_Feature_1 mLang="1">
      <value lang="en">&lt;p&gt;Goodbye Humidity. Hello Comfort.&lt;/p&gt;&lt;p&gt;The Honeywell Home  TrueDRY™ DR90 Whole Home Dehumidifier  can remove up to 90 pints of moisture per day from your home's air, making you more comfortable. It's ideal for larger homes up to 3, 6000 square feet. It can remove moisture evenly throughout the entire home.&lt;/p&gt;&lt;p&gt;&lt;br&gt;&lt;/p&gt;</value>
      <value lang="da-DK"> </value>
    </SKU_Basic_Feature_1>
    <SKU_Basic_Feature_2 mLang="1">
      <value lang="en">&lt;p&gt;Help Lower Energy Costs&lt;/p&gt;&lt;p&gt;With up to 90 pints of moisture leaving your home, you can run your air conditioner  less frequently. This means you can potentially save money on your energy bill. Less moisture in the home also helps to reduce the strain on your air conditioner.&lt;br&gt;&lt;/p&gt;</value>
      <value lang="da-DK"> </value>
    </SKU_Basic_Feature_2>
    <SKU_Basic_Feature_3 mLang="1">
      <value lang="en">&lt;p&gt;Minimal Maintenance&lt;/p&gt;&lt;p&gt;The moisture the Honeywell Home TrueDRY DR90 Whole Home Dehumidifier pulls from your home automatically drains, so you don't have to empty the water reservoir by hand. And the filter only need replacing once per year. This  dehumidifier provides unobtrusive, quiet and efficient operation. G82&lt;/p&gt;</value>
      <value lang="da-DK"> </value>
    </SKU_Basic_Feature_3>
    <SKU_Brand>Honeywell Home</SKU_Brand>
    <SKU_Brand_description code="Honeywell Home">Honeywell Home</SKU_Brand_description>
    <SKU_Bundle>No</SKU_Bundle>
    <SKU_Bundle_description code="No" mLang="1">
      <desc lang="en">No</desc>
      <desc lang="da-DK"> </desc>
    </SKU_Bundle_description>
    <SKU_Channel_Display_Category>B</SKU_Channel_Display_Category>
    <SKU_Channel_Display_Category_description code="Standard Product">Standard Product</SKU_Channel_Display_Category_description>
    <SKU_Color_description code="Silver" mLang="1">
      <desc lang="en">Silver</desc>
      <desc lang="da-DK"> </desc>
    </SKU_Color_description>
    <SKU_Color>Silver</SKU_Color>
    <SKU_EAN_Number> </SKU_EAN_Number>
    <SKU_ECCN_Number> </SKU_ECCN_Number>
    <SKU_Features> </SKU_Features>
    <SKU_For_Use_With mLang="1">
      <value lang="en"/>
      <value lang="da-DK"/>
    </SKU_For_Use_With>
    <SKU_GTIN_Number> </SKU_GTIN_Number>
    <SKU_Getting_Started_Copy mLang="1">
      <value lang="en">Professional installation gets professional results. Find an authorized professional near you to complete your installation.</value>
      <value lang="da-DK"> </value>
    </SKU_Getting_Started_Copy>
    <SKU_Global_ERP_Status_Code>01</SKU_Global_ERP_Status_Code>
    <SKU_Global_ERP_Status_Code_description code="Active">Active</SKU_Global_ERP_Status_Code_description>
    <SKU_Hazardous_Material_Code> </SKU_Hazardous_Material_Code>
    <SKU_Hazardous_Material_Description mLang="1">
      <value lang="en"> </value>
      <value lang="da-DK"> </value>
    </SKU_Hazardous_Material_Description>
    <SKU_Includes mLang="1">
      <value lang="en">Four leveling feet with rubber isolation pads</value>
      <value lang="da-DK"/>
    </SKU_Includes>
    <SKU_Install_Type>Professional Install</SKU_Install_Type>
    <SKU_Install_Type_description code="Professional Install" mLang="1">
      <desc lang="en">Professional Install</desc>
      <desc lang="da-DK"> </desc>
    </SKU_Install_Type_description>
    <SKU_Is_Primary_SKU> </SKU_Is_Primary_SKU>
    <SKU_Keywords> </SKU_Keywords>
    <SKU_Literature_Language mLang="1">
      <value lang="en"/>
      <value lang="da-DK"/>
    </SKU_Literature_Language>
    <SKU_Long_Promotion_Description mLang="1">
      <value lang="en"> </value>
      <value lang="da-DK"> </value>
    </SKU_Long_Promotion_Description>
    <SKU_Meta_Description>The DR90A3000/U Honeywell Home Dehumidification from Resideo is built to meet the specific needs for your home. Find product info and resources here.</SKU_Meta_Description>
    <SKU_Meta_Title>DR90A3000/U Dehumidification - Honeywell Home From Resideo</SKU_Meta_Title>
    <SKU_Name> </SKU_Name>
    <SKU_New_SKU_Effective_End_Date> </SKU_New_SKU_Effective_End_Date>
    <SKU_New_SKU_Effective_Start_Date> </SKU_New_SKU_Effective_Start_Date>
    <SKU_New_SKU_Effective_Timezone_description code=""> </SKU_New_SKU_Effective_Timezone_description>
    <SKU_New_SKU_Effective_Timezone> </SKU_New_SKU_Effective_Timezone>
    <SKU_New_SKU_Indicator_description code="No">No</SKU_New_SKU_Indicator_description>
    <SKU_New_SKU_Indicator>No</SKU_New_SKU_Indicator>
    <SKU_OG_Description>The DR90A3000/U Honeywell Home Dehumidification from Resideo is built to meet the specific needs for your home. Find product info and resources here.</SKU_OG_Description>
    <SKU_OG_Title>DR90A3000/U Dehumidification - Honeywell Home From Resideo</SKU_OG_Title>
    <SKU_PIM_SKU_Status>Published</SKU_PIM_SKU_Status>
    <SKU_PIM_SKU_Status_description code="Published">Published</SKU_PIM_SKU_Status_description>
    <SKU_PIM_Workflow_Ownership> </SKU_PIM_Workflow_Ownership>
    <SKU_Packaging_Material> </SKU_Packaging_Material>
    <SKU_Packaging_Shape_description code=" " mLang="1">
      <desc lang="en"> </desc>
      <desc lang="da-DK"> </desc>
    </SKU_Packaging_Shape_description>
    <SKU_Packaging_Shape> </SKU_Packaging_Shape>
    <SKU_Power_Source> </SKU_Power_Source>
    <SKU_Pro_Install>Yes</SKU_Pro_Install>
    <SKU_Pro_Install_description code="Yes">Yes</SKU_Pro_Install_description>
    <SKU_Pro_Meta_Description>The DR90A3000/U Honeywell Home dehumidifier from Resideo helps you create a more comfortable home for your customers. Find product info and resources here.</SKU_Pro_Meta_Description>
    <SKU_Pro_Meta_Title>DR90A3000/U Dehumidifier - Honeywell Home From Resideo</SKU_Pro_Meta_Title>
    <SKU_Pro_OG_Description>The DR90A3000/U Honeywell Home dehumidifier from Resideo helps you create a more comfortable home for your customers. Find product info and resources here.</SKU_Pro_OG_Description>
    <SKU_Pro_OG_Desription> </SKU_Pro_OG_Desription>
    <SKU_Pro_OG_Title>DR90A3000/U Dehumidifier - Honeywell Home From Resideo</SKU_Pro_OG_Title>
    <SKU_Product_Features mLang="1">
      <value lang="en"/>
      <value lang="da-DK"/>
    </SKU_Product_Features>
    <SKU_Product_Manager_description code=""> </SKU_Product_Manager_description>
    <SKU_Product_Manager> </SKU_Product_Manager>
    <SKU_Product_Manager_owner> </SKU_Product_Manager_owner>
    <SKU_Product_Number>DR-Series3</SKU_Product_Number>
    <SKU_Product_Taxonomy_Node>RMH Root.Air Treatment.Dehumidifiers</SKU_Product_Taxonomy_Node>
    <SKU_Product_Taxonomy_Node_description code="Dehumidifiers">Dehumidifiers</SKU_Product_Taxonomy_Node_description>
    <SKU_Program_Type> </SKU_Program_Type>
    <SKU_Promotion_Banner> </SKU_Promotion_Banner>
    <SKU_Promotion_Effective_End_Time> </SKU_Promotion_Effective_End_Time>
    <SKU_Promotion_Effective_Start_Time> </SKU_Promotion_Effective_Start_Time>
    <SKU_Promotion_Timezone> </SKU_Promotion_Timezone>
    <SKU_Promotion_Timezone_description code=""> </SKU_Promotion_Timezone_description>
    <SKU_Release_Indicator>PPR1</SKU_Release_Indicator>
    <SKU_Release_Indicator>CR2</SKU_Release_Indicator>
    <SKU_Release_Indicator_description code="RDE Pro Public Release 1">PPR1</SKU_Release_Indicator_description>
    <SKU_Release_Indicator_description code="RDE Consumer Release 2">CR2</SKU_Release_Indicator_description>
    <SKU_Resideo_Workflow_Status> </SKU_Resideo_Workflow_Status>
    <SKU_SKU_Accessory_Part_Description mLang="1">
      <value lang="en"> </value>
      <value lang="da-DK"> </value>
    </SKU_SKU_Accessory_Part_Description>
    <SKU_SKU_Asset_Status>Completed</SKU_SKU_Asset_Status>
    <SKU_SKU_Asset_Status_description code="Completed">Completed</SKU_SKU_Asset_Status_description>
    <SKU_SKU_Compatibility_Description mLang="1">
      <value lang="en"> </value>
      <value lang="da-DK"> </value>
    </SKU_SKU_Compatibility_Description>
    <SKU_SKU_Detail_Page_Title> </SKU_SKU_Detail_Page_Title>
    <SKU_SKU_ID>501</SKU_SKU_ID>
    <SKU_SKU_Long_Description> </SKU_SKU_Long_Description>
    <SKU_SKU_Replacement_Part_Description mLang="1">
      <value lang="en"> </value>
      <value lang="da-DK"> </value>
    </SKU_SKU_Replacement_Part_Description>
    <SKU_SKU_Sequence_Order>1</SKU_SKU_Sequence_Order>
    <SKU_SKU_Short_Description>DR90 Whole House Dehumidifier</SKU_SKU_Short_Description>
    <SKU_SKU_Title mLang="1">
      <value lang="en">90-Pt. TrueDRY Whole Home Dehumidification System</value>
      <value lang="da-DK"> </value>
    </SKU_SKU_Title>
    <SKU_Sequence> </SKU_Sequence>
    <SKU_Short_Promotion_Description mLang="1">
      <value lang="en"> </value>
      <value lang="da-DK"> </value>
    </SKU_Short_Promotion_Description>
    <SKU_Sold_By_Retailer>No</SKU_Sold_By_Retailer>
    <SKU_Sold_By_Retailer_description code="No">No</SKU_Sold_By_Retailer_description>
    <SKU_Sold_Direct_To_Consumer_description code="No">No</SKU_Sold_Direct_To_Consumer_description>
    <SKU_Sold_Direct_To_Consumer>No</SKU_Sold_Direct_To_Consumer>
    <SKU_Syndication_Channel>RPH</SKU_Syndication_Channel>
    <SKU_Syndication_Channel>RCH</SKU_Syndication_Channel>
    <SKU_Syndication_Channel>HWH</SKU_Syndication_Channel>
    <SKU_Syndication_Channel>PED</SKU_Syndication_Channel>
    <SKU_Syndication_Channel_description code="Pro North America">RPH</SKU_Syndication_Channel_description>
    <SKU_Syndication_Channel_description code="Consumer United States">RCH</SKU_Syndication_Channel_description>
    <SKU_Syndication_Channel_description code="Honeywell Home">HWH</SKU_Syndication_Channel_description>
    <SKU_Syndication_Channel_description code="Power Equipment Direct">PED</SKU_Syndication_Channel_description>
    <SKU_Type_description code="Dehumidifiers" mLang="1">
      <desc lang="en">Dehumidifiers</desc>
      <desc lang="da-DK"> </desc>
    </SKU_Type_description>
    <SKU_Type>Dehumidifiers</SKU_Type>
    <SKU_UPC_Number>85267421515</SKU_UPC_Number>
    <SKU_Use> </SKU_Use>
    <pk1SKU>DR90A3000/U</pk1SKU>
    <Package_Height>
      <sequencePim>0</sequencePim>
      <SKU_Package_Height>25.2</SKU_Package_Height>
      <SKU_Package_Height_UoM>in</SKU_Package_Height_UoM>
      <SKU_Package_Height_UoM_descriptionDesc code="Inch">in</SKU_Package_Height_UoM_descriptionDesc>
    </Package_Height>
    <Product_Height>
      <sequencePim>0</sequencePim>
      <SKU_Product_Height>19.3</SKU_Product_Height>
      <SKU_Product_Height_UoM>in</SKU_Product_Height_UoM>
      <SKU_Product_Height_UoM_descriptionDesc code="Inch">in</SKU_Product_Height_UoM_descriptionDesc>
    </Product_Height>
    <Package_Length>
      <sequencePim>0</sequencePim>
      <SKU_Package_Length>35.8</SKU_Package_Length>
      <SKU_Package_Length_UoM_descriptionDesc code="Inch">in</SKU_Package_Length_UoM_descriptionDesc>
      <SKU_Package_Length_UoM>in</SKU_Package_Length_UoM>
    </Package_Length>
    <Package_Width>
      <sequencePim>0</sequencePim>
      <SKU_Package_Width>19.3</SKU_Package_Width>
      <SKU_Package_Width_UoM_descriptionDesc code="Inch">in</SKU_Package_Width_UoM_descriptionDesc>
      <SKU_Package_Width_UoM>in</SKU_Package_Width_UoM>
    </Package_Width>
    <Product_Length>
      <sequencePim>0</sequencePim>
      <SKU_Product_Length>19.3</SKU_Product_Length>
      <SKU_Product_Length_UoM_descriptionDesc code="Inch">in</SKU_Product_Length_UoM_descriptionDesc>
      <SKU_Product_Length_UoM>in</SKU_Product_Length_UoM>
    </Product_Length>
    <Product_Width>
      <sequencePim>0</sequencePim>
      <SKU_Product_Width>14.3</SKU_Product_Width>
      <SKU_Product_Width_UoM_descriptionDesc code="Inch">in</SKU_Product_Width_UoM_descriptionDesc>
      <SKU_Product_Width_UoM>in</SKU_Product_Width_UoM>
    </Product_Width>
    <Package_Weight>
      <sequencePim>0</sequencePim>
      <SKU_Package_Weight>108.6</SKU_Package_Weight>
      <SKU_Package_Weight_UoM>lb</SKU_Package_Weight_UoM>
      <SKU_Package_Weight_UoM_descriptionDesc code="Pound">lb</SKU_Package_Weight_UoM_descriptionDesc>
    </Package_Weight>
    <dynamicAttr>
      <attrName>Drain Connection Size</attrName>
      <attrValue> </attrValue>
      <desc code=""> </desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Drain Connection Type</attrName>
      <attrValue mLang="1">
        <value lang="en"> </value>
        <value lang="da-DK"> </value>
      </attrValue>
      <desc code=""> </desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Dehumidifying Capacity (Max)</attrName>
      <attrValue mLang="1">
        <value lang="en">90 pints per day</value>
        <value lang="da-DK"/>
      </attrValue>
      <attrValue mLang="1">
        <value lang="en"> 45 liters per day</value>
        <value lang="da-DK"/>
      </attrValue>
      <desc code="">90 pints per day| 45 liters per day</desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Power Supply Frequency</attrName>
      <attrValue mLang="1">
        <value lang="en"/>
        <value lang="da-DK"/>
      </attrValue>
      <desc code=""> </desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Building Size (Max)</attrName>
      <attrValue mLang="1">
        <value lang="en"/>
        <value lang="da-DK"/>
      </attrValue>
      <desc code=""> </desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Energy Performance</attrName>
      <attrValue mLang="1">
        <value lang="en">2.9 liters per KWH </value>
        <value lang="da-DK"> </value>
      </attrValue>
      <desc code="">2.9 liters per KWH </desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Dehumidification Collar Diameter</attrName>
      <attrValue> </attrValue>
      <desc code=""> </desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Operating Voltage</attrName>
      <attrValue mLang="1">
        <value lang="en"/>
        <value lang="da-DK"/>
      </attrValue>
      <desc code=""> </desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Current Draw</attrName>
      <attrValue mLang="1">
        <value lang="en">5.3 A</value>
        <value lang="da-DK"> </value>
      </attrValue>
      <desc code="">5.3 A</desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Recommended Service Life</attrName>
      <attrValue mLang="1">
        <value lang="en"> </value>
        <value lang="da-DK"> </value>
      </attrValue>
      <desc code=""> </desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Ventilation Collar Diameter</attrName>
      <attrValue> </attrValue>
      <desc code=""> </desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Thermal Charge</attrName>
      <attrValue mLang="1">
        <value lang="en"/>
        <value lang="da-DK"/>
      </attrValue>
      <desc code=""> </desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Power Consumption</attrName>
      <attrValue mLang="1">
        <value lang="en">120 VAC</value>
        <value lang="da-DK"/>
      </attrValue>
      <desc code="">120 VAC</desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Building Square Footage (Max)</attrName>
      <attrValue mLang="1">
        <value lang="en">Up to 2800 sq. ft.</value>
        <value lang="da-DK"> </value>
      </attrValue>
      <desc code="">Up to 2800 sq. ft.</desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Energy Star Certified</attrName>
      <attrValue>Yes</attrValue>
      <desc lang="en"> </desc>
      <desc lang="da-DK"> </desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Auxiliary Outputs</attrName>
      <attrValue> </attrValue>
      <desc lang="en"> </desc>
      <desc lang="da-DK"> </desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Ambient Operating Temperature (Max)</attrName>
      <attrValue mLang="1">
        <value lang="en"/>
        <value lang="da-DK"/>
      </attrValue>
      <desc code=""> </desc>
    </dynamicAttr>
    <dynamicAttr>
      <attrName>Ambient Operating Temperature (Min)</attrName>
      <attrValue mLang="1">
        <value lang="en"/>
        <value lang="da-DK"/>
      </attrValue>
      <desc code=""> </desc>
    </dynamicAttr>
  </SKU>
</Root>

我需要输出像

SKU_SKU_Number SKU_Advanced_Feature_1 SKU_Advanced_Feature_1_Lang SKU_Application SKU_Application_Lang dynamicAttr dynamicAttr_Lang Value DR90A3000/U SKU_Advanced_Feature1 en Dehumidification and Internal en Drain Connection Size en DR90A3000/U SKU_Advanced_Feature2 da-DK Testing SKU Application da-DK da-DK DR90A3000/U SKU_Advanced_Feature3 mal Drain Connection Type en DR90A3000/U Drain连接类型 da-DK DR90A3000/U 除湿能力(最大) en 90 品脱每天 DR90A3000/U da-DK DR90A3000/U 除湿能力(最大) en 45 升每天 DR90A3000/U da-DK DR90A3000/U 电源频率 en DR90A3000/U da-DKDR90A3000/U 建筑尺寸(最大) en DR90A3000/U da-DK DR90A3000/U 能源性能 en 2.9 升每千瓦时 DR90A3000/U da-DK DR90A3000/U 除湿环直径 en DR90A3000/U da-DK

我使用下面的查询来解析 XML。但这只给了我一个记录行和多个 NULL 值

DECLARE @docHandle INT = 121
SELECT *   --INTO #tmp1
FROM OPENXML(@docHandle, N'//SKU/',3) 
WITH 
(
SKU_SKU_Number VARCHAR(255) './SKU_SKU_Number',
SKU_Advanced_Feature_1 VARCHAR(255) './SKU_Advanced_Feature_1/value',
SKU_Advanced_Feature_1_lang1 VARCHAR(255) './SKU_Advanced_Feature_1/value/@lang'
)

有没有一种可能的方法可以让我获得我想要的输出?任何帮助都感激不尽。

4

2 回答 2

1

从 SQL Server 2005 开始,最好使用基于 w3c 标准的 XQuery 语言,同时处理 XML 数据类型。

Microsoft 专有OPENXML及其伙伴sp_xml_preparedocumentsp_xml_removedocument保留只是为了与过时的 SQL Server 2000 向后兼容。

SQL

DECLARE @xml XML =
'<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <dataModel>
        <repo profile="SKU" name="SKU"/>
    </dataModel>
    <SKU repo="SKU">
        <SKU_SKU_Number>ABCD/U</SKU_SKU_Number>
        <SKU_Advanced_Feature_1 mLang="1">
            <value lang="en">SKU_Advanced_Feature1</value>
            <value lang="da-DK">SKU_Advanced_Feature2</value>
        </SKU_Advanced_Feature_1>
    </SKU>
</Root>';

SELECT c.value('(SKU_SKU_Number/text())[1]','VARCHAR(20)') AS SKU_SKU_Number
    , y.value('(./text())[1]','VARCHAR(30)') AS SKU_Advanced_Feature_1
    , y.value('@lang','VARCHAR(30)') AS Lang
FROM @xml.nodes('/Root/SKU') AS t(c)
    CROSS APPLY t.c.nodes('SKU_Advanced_Feature_1/value') AS x(y);

输出

+----------------+------------------------+-------+
| SKU_SKU_Number | SKU_Advanced_Feature_1 | Lang  |
+----------------+------------------------+-------+
| ABCD/U         | SKU_Advanced_Feature1  | en    |
| ABCD/U         | SKU_Advanced_Feature2  | da-DK |
+----------------+------------------------+-------+

SQL #2

SELECT c.value('(SKU_SKU_Number/text())[1]','VARCHAR(20)') AS SKU_SKU_Number
    , y.value('(./text())[1]','VARCHAR(30)') AS SKU_Advanced_Feature_1
    , y.value('@lang','VARCHAR(30)') AS Lang
    , y.value('local-name(..)', 'VARCHAR(50)') AS PARENT
FROM @xml.nodes('/Root/SKU') AS t(c)
    CROSS APPLY t.c.nodes('*[local-name(.)=("SKU_Advanced_Feature_1","SKU_Approvals_and_Standards")]/value') AS x(y);

最新 XML 的输出

+----------------+------------------------+-------+-----------------------------+
| SKU_SKU_Number | SKU_Advanced_Feature_1 | Lang  |           PARENT            |
+----------------+------------------------+-------+-----------------------------+
| ABCDEFG        | SKU_Advanced_Feature1  | en    | SKU_Advanced_Feature_1      |
| ABCDEFG        | SKU_Advanced_Feature2  | da-DK | SKU_Advanced_Feature_1      |
| ABCDEFG        | SKU_Advanced_Feature3  | mal   | SKU_Advanced_Feature_1      |
| ABCDEFG        | ETL listed, UL474      | en    | SKU_Approvals_and_Standards |
| ABCDEFG        | NULL                   | da-DK | SKU_Approvals_and_Standards |
| ABCDEFG        | CSA 22.2 No. 92        | en    | SKU_Approvals_and_Standards |
| ABCDEFG        | NULL                   | da-DK | SKU_Approvals_and_Standards |
+----------------+------------------------+-------+-----------------------------+

SQL #3

SELECT c.value('(SKU_SKU_Number/text())[1]','VARCHAR(20)') AS SKU_SKU_Number
    , y.value('(./text())[1]','VARCHAR(30)') AS SKU_Advanced_Feature_1
    , y.value('@lang','VARCHAR(30)') AS Lang
    , b.value('(attrName/text())[1]','VARCHAR(30)') AS attrName
    , y.value('local-name(..)', 'VARCHAR(50)') AS PARENT
FROM @xml.nodes('/Root/SKU') AS t(c)
    CROSS APPLY t.c.nodes('*[local-name(.)=("SKU_Advanced_Feature_1","SKU_Approvals_and_Standards")]/value') AS x(y)
    CROSS APPLY t.c.nodes('*[local-name(.)=("dynamicAttr")]') AS a(b);
于 2020-08-25T21:30:44.033 回答
0

从您的 XML 看来,您需要多个查询。请尝试以下。

SELECT c.value('(SKU_SKU_Number/text())[1]','VARCHAR(255)') AS SKU_SKU_Number,
        c.value('(pk1SKU/text())[1]','VARCHAR(255)') AS pk1SKU,
        c.value('(SKU_Brand/text())[1]','VARCHAR(255)') AS SKU_Brand,
        c.value('(SKU_Brand_description/text())[1]','VARCHAR(255)') AS SKU_Brand_description,
        c.value('(SKU_Bundle/text())[1]','VARCHAR(255)') AS SKU_Bundle,
        c.value('(SKU_Bundle_description/desc/@lang)[1]','VARCHAR(255)') AS SKU_Bundle_description_1_Lang,
        c.value('(SKU_Bundle_description/desc/text())[1]','VARCHAR(255)') AS SKU_Bundle_description_1,
        c.value('(SKU_Bundle_description/desc/@lang)[2]','VARCHAR(255)') AS SKU_Bundle_description_2_Lang,
        c.value('(SKU_Bundle_description/desc/text())[2]','VARCHAR(255)') AS SKU_Bundle_description_2,
        c.value('(SKU_Channel_Display_Category/text())[1]','VARCHAR(255)') AS SKU_Channel_Display_Category,
        c.value('(SKU_Channel_Display_Category_description/text())[1]','VARCHAR(255)') AS SKU_Channel_Display_Category_description,
        c.value('(SKU_Color_description/desc/@lang)[1]','VARCHAR(255)') AS SKU_Color_description_1_Lang,
        c.value('(SKU_Color_description/desc/text())[1]','VARCHAR(255)') AS SKU_Color_description_1,
        c.value('(SKU_Color_description/desc/@lang)[2]','VARCHAR(255)') AS SKU_Color_description_2_Lang,
        c.value('(SKU_Color_description/desc/text())[2]','VARCHAR(255)') AS SKU_Color_description_2,
        c.value('(SKU_Color/text())[1]','VARCHAR(255)') AS SKU_Color,
        c.value('(SKU_EAN_Number/text())[1]','VARCHAR(255)') AS SKU_EAN_Number,
        c.value('(SKU_ECCN_Number/text())[1]','VARCHAR(255)') AS SKU_ECCN_Number,
        c.value('(SKU_Features/text())[1]','VARCHAR(255)') AS SKU_Features,
        c.value('(SKU_GTIN_Number/text())[1]','VARCHAR(255)') AS SKU_GTIN_Number,
        c.value('(SKU_Global_ERP_Status_Code/text())[1]','VARCHAR(255)') AS SKU_Global_ERP_Status_Code,
        c.value('(SKU_Global_ERP_Status_Code_description/desc/text())[1]','VARCHAR(255)') AS SKU_Global_ERP_Status_Code_description,
        c.value('(SKU_Hazardous_Material_Code/text())[1]','VARCHAR(255)') AS SKU_Hazardous_Material_Code,
        c.value('(SKU_Install_Type/text())[1]','VARCHAR(255)') AS SKU_Install_Type, 
        c.value('(SKU_Is_Primary_SKU/text())[1]','VARCHAR(255)') AS SKU_Is_Primary_SKU,
        c.value('(SKU_Keywords/text())[1]','VARCHAR(255)') AS SKU_Keywords,
        c.value('(SKU_Meta_Title/text())[1]','VARCHAR(255)') AS SKU_Meta_Title,
        c.value('(SKU_Meta_Description/text())[1]','VARCHAR(255)') AS SKU_Meta_Description,
        c.value('(SKU_Name/text())[1]','VARCHAR(255)') AS SKU_Name,
        c.value('(SKU_New_SKU_Effective_End_Date/text())[1]','VARCHAR(255)') AS SKU_New_SKU_Effective_End_Date,
        c.value('(SKU_New_SKU_Effective_Start_Date/text())[1]','VARCHAR(255)') AS SKU_New_SKU_Effective_Start_Date,
        c.value('(SKU_New_SKU_Effective_Timezone_description/text())[1]','VARCHAR(255)') AS SKU_New_SKU_Effective_Timezone_description,
        c.value('(SKU_New_SKU_Effective_Timezone/text())[1]','VARCHAR(255)') AS SKU_New_SKU_Effective_Timezone,
        c.value('(SKU_New_SKU_Indicator_description/text())[1]','VARCHAR(255)') AS SKU_New_SKU_Indicator_description,
        c.value('(SKU_New_SKU_Indicator/text())[1]','VARCHAR(255)') AS SKU_New_SKU_Indicator,
        c.value('(SKU_OG_Description/text())[1]','VARCHAR(255)') AS SKU_OG_Description,
        c.value('(SKU_OG_Title/text())[1]','VARCHAR(255)') AS SKU_OG_Title,
        c.value('(SKU_PIM_SKU_Status/text())[1]','VARCHAR(255)') AS SKU_PIM_SKU_Status,
        c.value('(SKU_PIM_SKU_Status_description/text())[1]','VARCHAR(255)') AS SKU_PIM_SKU_Status_description,
        c.value('(SKU_PIM_Workflow_Ownership/text())[1]','VARCHAR(255)') AS SKU_PIM_Workflow_Ownership,
        c.value('(SKU_Packaging_Material/text())[1]','VARCHAR(255)') AS SKU_Packaging_Material,
        c.value('(SKU_Packaging_Shape_description/text())[1]','VARCHAR(255)') AS SKU_Packaging_Shape_description,
        c.value('(SKU_Packaging_Shape/text())[1]','VARCHAR(255)') AS SKU_Packaging_Shape,
        c.value('(SKU_Power_Source/text())[1]','VARCHAR(255)') AS SKU_Power_Source,
        c.value('(SKU_Pro_Install/text())[1]','VARCHAR(255)') AS SKU_Pro_Install,
        c.value('(SKU_Pro_Install_description/text())[1]','VARCHAR(255)') AS SKU_Pro_Install_description,
        c.value('(SKU_Pro_Meta_Description/text())[1]','VARCHAR(255)') AS SKU_Pro_Meta_Description,
        c.value('(SKU_Pro_Meta_Title/text())[1]','VARCHAR(255)') AS SKU_Pro_Meta_Title,
        c.value('(SKU_Pro_OG_Description/text())[1]','VARCHAR(255)') AS SKU_Pro_OG_Description,
        c.value('(SKU_Pro_OG_Desription/text())[1]','VARCHAR(255)') AS SKU_Pro_OG_Desription,
        c.value('(SKU_Pro_OG_Title/text())[1]','VARCHAR(255)') AS SKU_Pro_OG_Title,
        c.value('(SKU_Product_Manager_description/text())[1]','VARCHAR(255)') AS SKU_Product_Manager_description,
        c.value('(SKU_Product_Manager/text())[1]','VARCHAR(255)') AS SKU_Product_Manager,
        c.value('(SKU_Product_Manager_owner/text())[1]','VARCHAR(255)') AS SKU_Product_Manager_owner,
        c.value('(SKU_Product_Number/text())[1]','VARCHAR(255)') AS SKU_Product_Number,
        c.value('(SKU_Product_Taxonomy_Node/text())[1]','VARCHAR(255)') AS SKU_Product_Taxonomy_Node,
        c.value('(SKU_Product_Taxonomy_Node_description/text())[1]','VARCHAR(255)') AS SKU_Product_Taxonomy_Node_description,
        c.value('(SKU_Program_Type/text())[1]','VARCHAR(255)') AS SKU_Program_Type,
        c.value('(SKU_Promotion_Banner/text())[1]','VARCHAR(255)') AS SKU_Promotion_Banner,
        c.value('(SKU_Promotion_Effective_End_Time/text())[1]','VARCHAR(255)') AS SKU_Promotion_Effective_End_Time,
        c.value('(SKU_Promotion_Effective_Start_Time/text())[1]','VARCHAR(255)') AS SKU_Promotion_Effective_Start_Time,
        c.value('(SKU_Promotion_Timezone/text())[1]','VARCHAR(255)') AS SKU_Promotion_Timezone,
        c.value('(SKU_Promotion_Timezone_description/text())[1]','VARCHAR(255)') AS SKU_Promotion_Timezone_description,
        c.value('(SKU_Release_Indicator/text())[1]','VARCHAR(255)') AS SKU_Release_Indicator,
        c.value('(SKU_Release_Indicator_description/text())[1]','VARCHAR(255)') AS SKU_Release_Indicator_description,
        c.value('(SKU_Release_Indicator/text())[2]','VARCHAR(255)') AS SKU_Release_Indicator2,
        c.value('(SKU_Release_Indicator_description/text())[2]','VARCHAR(255)') AS SKU_Release_Indicator_description2,
        c.value('(SKU_Release_Indicator/text())[3]','VARCHAR(255)') AS SKU_Release_Indicator3,
        c.value('(SKU_Release_Indicator_description/text())[3]','VARCHAR(255)') AS SKU_Release_Indicator_description3,
        c.value('(SKU_Release_Indicator/text())[4]','VARCHAR(255)') AS SKU_Release_Indicator4,
        c.value('(SKU_Release_Indicator_description/text())[4]','VARCHAR(255)') AS SKU_Release_Indicator_description4,
        c.value('(SKU_Release_Indicator/text())[5]','VARCHAR(255)') AS SKU_Release_Indicator5,
        c.value('(SKU_Release_Indicator_description/text())[5]','VARCHAR(255)') AS SKU_Release_Indicator_description5,
        c.value('(SKU_Resideo_Workflow_Status/text())[1]','VARCHAR(255)') AS SKU_Resideo_Workflow_Status,
        c.value('(SKU_SKU_Asset_Status/text())[1]','VARCHAR(255)') AS SKU_SKU_Asset_Status,
        c.value('(SKU_SKU_Asset_Status_description/text())[1]','VARCHAR(255)') AS SKU_SKU_Asset_Status_description,
        c.value('(SKU_SKU_Detail_Page_Title/text())[1]','VARCHAR(255)') AS SKU_SKU_Detail_Page_Title,
        c.value('(SKU_SKU_ID/text())[1]','VARCHAR(255)') AS SKU_SKU_ID,
        c.value('(SKU_SKU_Long_Description/text())[1]','VARCHAR(255)') AS SKU_SKU_Long_Description,
        c.value('(SKU_SKU_Sequence_Order/text())[1]','VARCHAR(255)') AS SKU_SKU_Sequence_Order,
        c.value('(SKU_SKU_Short_Description/text())[1]','VARCHAR(255)') AS SKU_SKU_Short_Description,
        c.value('(SKU_Sequence/text())[1]','VARCHAR(255)') AS SKU_Sequence,
        c.value('(SKU_Sold_By_Retailer/text())[1]','VARCHAR(255)') AS SKU_Sold_By_Retailer,
        c.value('(SKU_Sold_By_Retailer_description/text())[1]','VARCHAR(255)') AS SKU_Sold_By_Retailer_description,
        c.value('(SKU_Sold_Direct_To_Consumer_description/text())[1]','VARCHAR(255)') AS SKU_Sold_Direct_To_Consumer_description,
        c.value('(SKU_Sold_Direct_To_Consumer/text())[1]','VARCHAR(255)') AS SKU_Sold_Direct_To_Consumer,
        c.value('(SKU_Syndication_Channel/text())[1]','VARCHAR(255)') AS SKU_Syndication_Channel,
        c.value('(SKU_Syndication_Channel_description/text())[1]','VARCHAR(255)') AS SKU_Syndication_Channel_description,
        c.value('(SKU_Syndication_Channel/text())[2]','VARCHAR(255)') AS SKU_Syndication_Channel2,
        c.value('(SKU_Syndication_Channel_description/text())[2]','VARCHAR(255)') AS SKU_Syndication_Channel_description2,
        c.value('(SKU_Syndication_Channel/text())[3]','VARCHAR(255)') AS SKU_Syndication_Channel3,
        c.value('(SKU_Syndication_Channel_description/text())[3]','VARCHAR(255)') AS SKU_Syndication_Channel_description3,
        c.value('(SKU_Syndication_Channel/text())[4]','VARCHAR(255)') AS SKU_Syndication_Channel4,
        c.value('(SKU_Syndication_Channel_description/text())[4]','VARCHAR(255)') AS SKU_Syndication_Channel_description4,
        c.value('(SKU_Syndication_Channel/text())[5]','VARCHAR(255)') AS SKU_Syndication_Channel5,
        c.value('(SKU_Syndication_Channel_description/text())[5]','VARCHAR(255)') AS SKU_Syndication_Channel_description5,
        c.value('(SKU_Type_description/text())[1]','VARCHAR(255)') AS SKU_Type_description,
        c.value('(SKU_Type/text())[1]','VARCHAR(255)') AS SKU_Type,
        c.value('(SKU_UPC_Number/text())[1]','VARCHAR(255)') AS SKU_UPC_Number,
        c.value('(SKU_Use/text())[1]','VARCHAR(255)') AS SKU_Use
    FROM @XMLData.nodes('/Root/SKU') AS t(c)
    OUTER APPLY t.c.nodes('*[local-name(.)=(".")]/value') AS x(y)


SELECT c.value('(SKU_SKU_Number/text())[1]','VARCHAR(255)') AS SKU_SKU_Number,
        c.value('(Package_Height/SKU_Package_Height/text())[1]','VARCHAR(255)') AS SKU_Package_Height,
        c.value('(Package_Height/SKU_Package_Height_UoM/text())[1]','VARCHAR(255)') AS SKU_Package_Height_UoM,
        c.value('(Package_Height/SKU_Package_Height_UoM_descriptionDesc/text())[1]','VARCHAR(255)') AS SKU_Package_Height_UoM_descriptionDesc,

        c.value('(Product_Height/SKU_Product_Height/text())[1]','VARCHAR(255)') AS SKU_Product_Height,
        c.value('(Product_Height/SKU_Product_Height_UoM/text())[1]','VARCHAR(255)') AS SKU_Product_Height_UoM,
        c.value('(Product_Height/SKU_Product_Height_UoM_descriptionDesc/text())[1]','VARCHAR(255)') AS SKU_Product_Height_UoM_descriptionDesc,
        
        c.value('(Package_Length/SKU_Package_Length/text())[1]','VARCHAR(255)') AS SKU_Package_Length,
        c.value('(Package_Length/SKU_Package_Length_UoM/text())[1]','VARCHAR(255)') AS SKU_Package_Length_UoM,
        c.value('(Package_Length/SKU_Package_Length_UoM_descriptionDesc/text())[1]','VARCHAR(255)') AS SKU_Package_Length_UoM_descriptionDesc,
        
        c.value('(Package_Width/SKU_Package_Width/text())[1]','VARCHAR(255)') AS SKU_Package_Width,
        c.value('(Package_Width/SKU_Package_Width_UoM_descriptionDesc/text())[1]','VARCHAR(255)') AS SKU_Package_Width_UoM_descriptionDesc,
        c.value('(Package_Width/SKU_Package_Width_UoM/text())[1]','VARCHAR(255)') AS SKU_Package_Width_UoM,

        c.value('(Product_Length/SKU_Product_Length/text())[1]','VARCHAR(255)') AS SKU_Product_Length,
        c.value('(Product_Length/SKU_Product_Length_UoM_descriptionDesc/text())[1]','VARCHAR(255)') AS SKU_Product_Length_UoM_descriptionDesc,
        c.value('(Product_Length/SKU_Product_Length_UoM/text())[1]','VARCHAR(255)') AS SKU_Product_Length_UoM,

        c.value('(Product_Width/SKU_Product_Width/text())[1]','VARCHAR(255)') AS SKU_Product_Width,
        c.value('(Product_Width/SKU_Product_Width_UoM_descriptionDesc/text())[1]','VARCHAR(255)') AS SKU_Product_Width_UoM_descriptionDesc,
        c.value('(Product_Width/SKU_Product_Width_UoM/text())[1]','VARCHAR(255)') AS SKU_Product_Width_UoM,

        c.value('(Package_Weight/SKU_Package_Weight/text())[1]','VARCHAR(255)') AS SKU_Package_Weight,
        c.value('(Package_Weight/SKU_Package_Weight_UoM/text())[1]','VARCHAR(255)') AS SKU_Package_Weight_UoM,
        c.value('(Package_Weight/SKU_Package_Weight_UoM_descriptionDesc/text())[1]','VARCHAR(255)') AS SKU_Package_Weight_UoM_descriptionDesc

FROM @XMLData.nodes('/Root/SKU') AS t(c)
    OUTER APPLY t.c.nodes('*[local-name(.)=(".")]/value') AS x(y)

SELECT c.value('(SKU_SKU_Number/text())[1]','VARCHAR(255)') AS SKU_SKU_Number
    , x.y.value('(attrName/text())[1]','VARCHAR(255)') AS attrName
    , x.y.value('(attrValue/value/@lang)[1]','VARCHAR(255)') AS Lang1
    , x.y.value('(attrValue/value/text())[1]','VARCHAR(255)') AS attrValue1
    , x.y.value('(attrValue/value/@lang)[2]','VARCHAR(255)') AS Lang2
    , x.y.value('(attrValue/value/text())[2]','VARCHAR(255)') AS attrValue2

FROM @XMLData.nodes('/Root/SKU') AS t(c)
    CROSS APPLY t.c.nodes('*[local-name(.)=("dynamicAttr")]') AS x(y)       
于 2020-08-28T04:23:38.950 回答