如何在sqlserver中使用OpenXML获取父元素名称

发布于 2024-08-05 06:32:50 字数 384 浏览 4 评论 0原文

如果我有一些 xml:

<Users>
    <User>
        <property1>sdfd</property1>
        ...
    <User>
    ... 
</Users>

而我的 sql 是:

SELECT 
    *
FROM
    OpenXML(@idoc, '/Users/User')
    WITH (  
          [property1] varchar(50) 'property1',
          ...
    )

如何获取父元素的名称并将其返回到数据集中?

If I have some xml:

<Users>
    <User>
        <property1>sdfd</property1>
        ...
    <User>
    ... 
</Users>

And my sql is:

SELECT 
    *
FROM
    OpenXML(@idoc, '/Users/User')
    WITH (  
          [property1] varchar(50) 'property1',
          ...
    )

How can I get the name of the parent element and return that in the dataset?

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

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

发布评论

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

评论(1

土豪 2024-08-12 06:32:50

此页面来自 OpenXML 上的 MSDN 文档 似乎表明您应该能够对父级使用“..”符号:

declare @idoc int
declare @doc varchar(1000)

set @doc ='<ROOT>
<Customer CustomerID="VINET" ContactName="Paul Henriot">
   <Order OrderID="10248" CustomerID="VINET" EmployeeID="5" 
           OrderDate="1996-07-04T00:00:00">
      <OrderDetail ProductID="11" Quantity="12"/>
      <OrderDetail ProductID="42" Quantity="10"/>
   </Order>
</Customer>
<Customer CustomerID="LILAS" ContactName="Carlos Gonzlez">
   <Order OrderID="10283" CustomerID="LILAS" EmployeeID="3" 
           OrderDate="1996-08-16T00:00:00">
      <OrderDetail ProductID="72" Quantity="3"/>
   </Order>
</Customer>
</ROOT>'

--Create an internal representation of the XML document.
exec sp_xml_preparedocument @idoc OUTPUT, @doc

-- SELECT stmt using OPENXML rowset provider
SELECT *
FROM   OPENXML (@idoc, '/ROOT/Customer/Order/OrderDetail',2)
         WITH (OrderID       int         '../@OrderID',
               CustomerID  varchar(10) '../@CustomerID',
               OrderDate   datetime    '../@OrderDate',
               ProdID      int         '@ProductID',
               Qty         int         '@Quantity')

这也适用于您的情况吗?

更新:
试试这个(“伪属性”@mp:parentlocalname):

SELECT *
FROM   OPENXML (@idoc, '/ROOT/Customer/Order/OrderDetail',2)
         WITH (OrderID       int         '../@OrderID',
               CustomerID  varchar(10) '../@CustomerID',
               OrderDate   datetime    '../@OrderDate',
               ProdID      int         '@ProductID',
               Qty         int         '@Quantity',
               ParentNodeName  varchar(50) '@mp:parentlocalname' )

这个现在可以满足您的要求吗? :-)

ExtremeExperts 的文章中查看这些“伪属性”的完整列表< /a>.

马克

This page from MSDN docs on OpenXML seems to indicate you should be able to use the ".." notation for the parent:

declare @idoc int
declare @doc varchar(1000)

set @doc ='<ROOT>
<Customer CustomerID="VINET" ContactName="Paul Henriot">
   <Order OrderID="10248" CustomerID="VINET" EmployeeID="5" 
           OrderDate="1996-07-04T00:00:00">
      <OrderDetail ProductID="11" Quantity="12"/>
      <OrderDetail ProductID="42" Quantity="10"/>
   </Order>
</Customer>
<Customer CustomerID="LILAS" ContactName="Carlos Gonzlez">
   <Order OrderID="10283" CustomerID="LILAS" EmployeeID="3" 
           OrderDate="1996-08-16T00:00:00">
      <OrderDetail ProductID="72" Quantity="3"/>
   </Order>
</Customer>
</ROOT>'

--Create an internal representation of the XML document.
exec sp_xml_preparedocument @idoc OUTPUT, @doc

-- SELECT stmt using OPENXML rowset provider
SELECT *
FROM   OPENXML (@idoc, '/ROOT/Customer/Order/OrderDetail',2)
         WITH (OrderID       int         '../@OrderID',
               CustomerID  varchar(10) '../@CustomerID',
               OrderDate   datetime    '../@OrderDate',
               ProdID      int         '@ProductID',
               Qty         int         '@Quantity')

Does that work in your case, too?

UPDATE:
Try this (the "pseudo-attribute" @mp:parentlocalname) :

SELECT *
FROM   OPENXML (@idoc, '/ROOT/Customer/Order/OrderDetail',2)
         WITH (OrderID       int         '../@OrderID',
               CustomerID  varchar(10) '../@CustomerID',
               OrderDate   datetime    '../@OrderDate',
               ProdID      int         '@ProductID',
               Qty         int         '@Quantity',
               ParentNodeName  varchar(50) '@mp:parentlocalname' )

Does this now do what you want? :-)

See a whole list of these "pseudo-attributes" in this article at ExtremeExperts.

Marc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文