1
Declare @xml xml,
@y int
set @xml= '<ContactUpdates>
       <Contact VendorID="4"><LastName>McCrystle</LastName>
       <FirstName>Timothy</FirstName>
       </Contact>
       <Contact VendorID="10">
        <LastName>Flynn</LastName>
        <FirstName>Erin</FirstName>
        </Contact></ContactUpdates>'


Exec sp_xml_preparedocument @y output, @xml;
Select * from openxml(@y,'/ContactUpdates/Contact')
  With (VendorID Varchar(20),
        LastName Varchar(30),
        FirstName Varchar(30))`

The result is

I do not know where has the mistake been done. Please help me out with this.

1

3 Answers 3

4

You have a mix of both attribute-centric and element centric-projections. The reason why VendorId is mapped, but not the two elements, is because attribute centric is the default. In a mixed / complex hierarchy scenario, as per here, you will need to explicitly provide the xpath mappings:

Exec sp_xml_preparedocument @y output, @xml;
Select * from openxml(@y,'/ContactUpdates/Contact')
  With (VendorID Varchar(20) '@VendorID', -- Attribute
        LastName Varchar(30) 'LastName', -- Element
        FirstName Varchar(30) 'FirstName'); -- Element

Edit

Something of interest to note is that the flags attribute is, well, a bitwise style [flag]. This means you can OR the options together. 1 is attribute centric, and 2 element centric, so 1 | 2 = 3 will give you both:

Exec sp_xml_preparedocument @y output, @xml;
Select * from openxml(@y,'/ContactUpdates/Contact', 3)
  With (VendorID Varchar(20),
        LastName Varchar(30),
        FirstName Varchar(30));

-- Remember to release the handle with sp_xml_removedocument 

I however do not believe this is good practice - it doesn't convey much to the developer, and it possibly has negative performance implications given that it is less specific than the exact xpath.

3
  • Thanks for the answer Stuart, if possible can you provide me with a link or example of explicitly providing xpath mappings Nov 7, 2014 at 5:48
  • Here's a good bunch of xpath examples. TL;DR: /ContactUpdates/Contact has already navigated you to the Contact element. Attributes in xpath / xsl are prefixed by @ and elements are unprefixed. Also, have updated, although I would prefer the original answer as it more accurately conveys the intention to a reader and also does not require Sql to guess for you. Pradeep's answer uses XQuery, which is a different approach.
    – StuartLC
    Nov 7, 2014 at 5:54
  • 1
    Thanks Stuart I appreciate your support.. :) Nov 7, 2014 at 7:18
1

Use the proper, native XQuery support in SQL Server :

SELECT
    VendorID = xc.value('@VendorID', 'int'),
    FirstName = xc.value('(FirstName)[1]', 'varchar(50)'),
    LastName = xc.value('(LastName)[1]', 'varchar(50)')
FROM 
    @xml.nodes('/ContactUpdates/Contact') AS XT(XC)

Easily gives you this nice output:

enter image description here

1

This should help you

SELECT VendorID = Container.value('(@VendorID)[1]', 'varchar(50)'),
       LastName = Container.value('(LastName)[1]', 'varchar(50)'),
       FirstName = Container.value('(FirstName)[1]', 'varchar(50)')
FROM   (SELECT @xml Columndata) a
       CROSS APPLY Columndata.nodes('/ContactUpdates/Contact') AS T(Container) 

Edited as per Stuart comment.

SELECT VendorID = Container.value('(@VendorID)[1]', 'varchar(50)'),
       LastName = Container.value('(LastName)[1]', 'varchar(50)'),
       FirstName = Container.value('(FirstName)[1]', 'varchar(50)')
FROM   @xml.nodes('/ContactUpdates/Contact') AS T(Container) 
1
  • 2
    +1, just note that this changes the technology to XQuery. Also, since this is an XML variable, not a table column, you can avoid the CROSS APPLY altogether, viz: FROM @xml.nodes('/ContactUpdates/Contact') as T(Container)
    – StuartLC
    Nov 7, 2014 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.