如何return元素属性值

how to return element attribute value

这是一个例子,我正在尝试 return value 属性值:

IF OBJECT_ID(N'tempdb.dbo.#XMLwithOpenXML') IS NOT NULL
    DROP TABLE #XMLwithOpenXML;

CREATE TABLE #XMLwithOpenXML
    (
      [XMLData] [XML] NULL ,
      [LoadedDateTime] [DATETIME] NULL,
    );

INSERT  INTO #XMLwithOpenXML
        ( XMLData
        )
VALUES  ( '<SupplyWeb_Data>
  <PurchaseOrder>
    <PO_Header>
      <status value="stat" />
      <po_number value="PO123" />
    </PO_Header>
    <PO_Detail>
      <cust_part_no value="A123" />
      <cust_part_desc value="A123 Desc" />
    </PO_Detail>
    <PO_Detail>
      <cust_part_no value="B123" />
      <cust_part_desc value="B123 Desc" />
    </PO_Detail>
  </PurchaseOrder>
</SupplyWeb_Data>'
        );

SELECT  T2.Loc.value('(/cust_part_no/@Value)[1]', 'varchar(255)') AS cust_part_no ,
        T2.Loc.value('(/cust_part_desc/@Value)[1]', 'varchar(255)') AS cust_part_desc
FROM    #XMLwithOpenXML
        CROSS APPLY XMLData.nodes('/SupplyWeb_Data/PurchaseOrder/PO_Detail')
        AS T2 ( Loc ); 

我遗漏了一些简单的东西,但我想不通。我想我需要使用 values 函数,但它是 returning null.

XQuery 区分大小写,因此 value 不是 Value 并在 /cust_part_... 之前添加 .

SELECT T2.Loc.value('(./cust_part_no/@value)[1]', 'varchar(255)')   AS cust_part_no 
      ,T2.Loc.value('(./cust_part_desc/@value)[1]', 'varchar(255)') AS cust_part_desc
FROM #XMLwithOpenXML x
CROSS APPLY XMLData.nodes('/SupplyWeb_Data/PurchaseOrder/PO_Detail')AS T2(Loc); 

LiveDemo