Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

xml query

Posted on 2014-09-15
3
Medium Priority
?
292 Views
Last Modified: 2014-09-16
Trying to query the following xml file
Declare @hDoc AS INT

declare @xml xml
        set @xml =
        '  <sdnList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/sdnList.xsd">
  <publshInformation>
    <Publish_Date>09/12/2014</Publish_Date>
    <Record_Count>5795</Record_Count>
  </publshInformation>
    <sdnEntry>
    <uid>2674</uid>
    <firstName>Abu</firstName>
    <lastName>ABBAS</lastName>
    <title>Director</title>
    <sdnType>Individual</sdnType>
    <programList>
      <program>SDT</program>
    </programList>
    <akaList>
      <aka>
        <uid>1795</uid>
        <type>a.k.a.</type>
        <category>strong</category>
        <lastName>ZAYDAN</lastName>
        <firstName>Muhammad</firstName>
      </aka>
    </akaList>
    <dateOfBirthList>
      <dateOfBirthItem>
        <uid>1000</uid>
        <dateOfBirth>10 Dec 1948</dateOfBirth>
        <mainEntry>true</mainEntry>
      </dateOfBirthItem>
    </dateOfBirthList>
  </sdnEntry>
  
</sdnList>

'



EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML
SELECT
        tab.col.value('.', 'nvarchar(100)') 
 FROM   @xml.nodes('/sdnList/sdnEntry') AS tab (col)
EXEC sp_xml_removedocument @hDoc

Open in new window

Need some help with the select


Would like result to include the publish date and record count along with all data under sdn entry

Here is sample of results
Publish_Date    Record_Count    SDNuid       firstName   lastName   title           sdnType      program  AKSuid dateOfBirthItemUID 
09/12/2014       5795                     2674           Abu              ABBAS        Director    Individual   SDT           1795 1000

Open in new window


Here is xsd
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="sdnList" targetNamespace="http://tempuri.org/sdnList.xsd" elementFormDefault="qualified"
   xmlns="http://tempuri.org/sdnList.xsd" xmlns:mstns="http://tempuri.org/sdnList.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="sdnList">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="publshInformation" maxOccurs="1">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element name="Publish_Date" type="xs:string" minOccurs="0" maxOccurs="1"></xs:element>
                     <xs:element name="Record_Count" type="xs:int" minOccurs="0" maxOccurs="1"></xs:element>
                  </xs:sequence>
               </xs:complexType>
            </xs:element>
            <xs:element name="sdnEntry" maxOccurs="unbounded">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element name="uid" type="xs:int" />
                     <xs:element name="firstName" type="xs:string" minOccurs="0" />
                     <xs:element name="lastName" type="xs:string" />
                     <xs:element name="title" type="xs:string" minOccurs="0" />
                     <xs:element name="sdnType" type="xs:string" />
                     <xs:element name="remarks" type="xs:string" minOccurs="0" />
                     <xs:element name="programList" minOccurs="1" maxOccurs="1">
                        <xs:complexType>
                           <xs:sequence>
                              <xs:element name="program" type="xs:string" minOccurs="0" maxOccurs="unbounded" />
                           </xs:sequence>
                        </xs:complexType>
                     </xs:element>
                     <xs:element name="idList" maxOccurs="1" minOccurs="0">
                        <xs:complexType>
                           <xs:sequence>
                              <xs:element name="id" minOccurs="0" maxOccurs="unbounded">
                                 <xs:complexType>
                                    <xs:sequence>
                                       <xs:element name="uid" type="xs:int" />
                                       <xs:element name="idType" type="xs:string" minOccurs="0" />
                                       <xs:element name="idNumber" type="xs:string" minOccurs="0" />
                                       <xs:element name="idCountry" type="xs:string" minOccurs="0" />
                                       <xs:element name="issueDate" type="xs:string" minOccurs="0" />
                                       <xs:element name="expirationDate" type="xs:string" minOccurs="0" />
                                    </xs:sequence>
                                 </xs:complexType>
                              </xs:element>
                           </xs:sequence>
                        </xs:complexType>
                     </xs:element>
                     <xs:element name="akaList" maxOccurs="1" minOccurs="0">
                        <xs:complexType>
                           <xs:sequence>
                              <xs:element name="aka" minOccurs="0" maxOccurs="unbounded">
                                 <xs:complexType>
                                    <xs:sequence>
                                       <xs:element name="uid" type="xs:int" />
                                       <xs:element name="type" type="xs:string" />
                                       <xs:element name="category" type="xs:string" />
                                       <xs:element name="lastName" type="xs:string" minOccurs="0" />
                                       <xs:element name="firstName" type="xs:string" minOccurs="0" />
                                    </xs:sequence>
                                 </xs:complexType>
                              </xs:element>
                           </xs:sequence>
                        </xs:complexType>
                     </xs:element>
                     <xs:element name="addressList" maxOccurs="1" minOccurs="0">
                        <xs:complexType>
                           <xs:sequence>
                              <xs:element name="address" minOccurs="0" maxOccurs="unbounded">
                                 <xs:complexType>
                                    <xs:sequence>
                                       <xs:element name="uid" type="xs:int" />
                                       <xs:element name="address1" type="xs:string" minOccurs="0" />
                                       <xs:element name="address2" type="xs:string" minOccurs="0" />
                                       <xs:element name="address3" type="xs:string" minOccurs="0" />
                                       <xs:element name="city" type="xs:string" minOccurs="0" />
                                       <xs:element name="stateOrProvince" type="xs:string" minOccurs="0" />
                                       <xs:element name="postalCode" type="xs:string" minOccurs="0" />
                                       <xs:element name="country" type="xs:string" minOccurs="0" />
                                    </xs:sequence>
                                 </xs:complexType>
                              </xs:element>
                           </xs:sequence>
                        </xs:complexType>
                     </xs:element>
                     <xs:element name="nationalityList" minOccurs="0" maxOccurs="1">
                        <xs:complexType>
                           <xs:sequence>
                              <xs:element name="nationality" minOccurs="0" maxOccurs="unbounded">
                                 <xs:complexType>
                                    <xs:sequence>
                                       <xs:element name="uid" type="xs:int" />
                                       <xs:element name="country" type="xs:string" />
                                       <xs:element name="mainEntry" type="xs:boolean" />
                                    </xs:sequence>
                                 </xs:complexType>
                              </xs:element>
                           </xs:sequence>
                        </xs:complexType>
                     </xs:element>
                     <xs:element name="citizenshipList" minOccurs="0" maxOccurs="1">
                        <xs:complexType>
                           <xs:sequence>
                              <xs:element name="citizenship" minOccurs="0" maxOccurs="unbounded">
                                 <xs:complexType>
                                    <xs:sequence>
                                       <xs:element name="uid" type="xs:int" />
                                       <xs:element name="country" type="xs:string" />
                                       <xs:element name="mainEntry" type="xs:boolean" />
                                    </xs:sequence>
                                 </xs:complexType>
                              </xs:element>
                           </xs:sequence>
                        </xs:complexType>
                     </xs:element>
                     <xs:element name="dateOfBirthList" minOccurs="0" maxOccurs="1">
                        <xs:complexType>
                           <xs:sequence>
                              <xs:element name="dateOfBirthItem" minOccurs="0" maxOccurs="unbounded">
                                 <xs:complexType>
                                    <xs:sequence>
                                       <xs:element name="uid" type="xs:int" />
                                       <xs:element name="dateOfBirth" type="xs:string" />
                                       <xs:element name="mainEntry" type="xs:boolean" />
                                    </xs:sequence>
                                 </xs:complexType>
                              </xs:element>
                           </xs:sequence>
                        </xs:complexType>
                     </xs:element>
                     <xs:element name="placeOfBirthList" minOccurs="0" maxOccurs="1">
                        <xs:complexType>
                           <xs:sequence>
                              <xs:element name="placeOfBirthItem" minOccurs="0" maxOccurs="unbounded">
                                 <xs:complexType>
                                    <xs:sequence>
                                       <xs:element name="uid" type="xs:int" />
                                       <xs:element name="placeOfBirth" type="xs:string" />
                                       <xs:element name="mainEntry" type="xs:boolean" />
                                    </xs:sequence>
                                 </xs:complexType>
                              </xs:element>
                           </xs:sequence>
                        </xs:complexType>
                     </xs:element>
                     <xs:element name="vesselInfo" minOccurs="0" maxOccurs="1">
                        <xs:complexType>
                           <xs:sequence>
                              <xs:element name="callSign" type="xs:string" minOccurs="0" />
                              <xs:element name="vesselType" type="xs:string" minOccurs="0" />
                              <xs:element name="vesselFlag" type="xs:string" minOccurs="0" />
                              <xs:element name="vesselOwner" type="xs:string" minOccurs="0" />
                              <xs:element name="tonnage" type="xs:int" minOccurs="0" />
                              <xs:element name="grossRegisteredTonnage" type="xs:int" minOccurs="0" />
                           </xs:sequence>
                        </xs:complexType>
                     </xs:element>
                  </xs:sequence>
               </xs:complexType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>

Open in new window

0
Comment
Question by:johnnyg123
2 Comments
 
LVL 36

Accepted Solution

by:
ste5an earned 2000 total points
ID: 40324968
You're mixing XML parsing techniques. You don't need the prepare statements. The basic problem is the use of a default XML namespace. E.g.

DECLARE @Xml XML = N'
<sdnList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/sdnList.xsd">
	<publshInformation>
		<Publish_Date>09/12/2014</Publish_Date>
		<Record_Count>5795</Record_Count>
	</publshInformation>
	<sdnEntry>
		<uid>2674</uid>
		<firstName>Abu</firstName>
		<lastName>ABBAS</lastName>
		<title>Director</title>
		<sdnType>Individual</sdnType>
		<programList>
			<program>SDT</program>
		</programList>
		<akaList>
			<aka>
				<uid>1795</uid>
				<type>a.k.a.</type>
				<category>strong</category>
				<lastName>ZAYDAN</lastName>
				<firstName>Muhammad</firstName>
			</aka>
		</akaList>
		<dateOfBirthList>
			<dateOfBirthItem>
				<uid>1000</uid>
				<dateOfBirth>10 Dec 1948</dateOfBirth>
				<mainEntry>true</mainEntry>
			</dateOfBirthItem>
		</dateOfBirthList>
	</sdnEntry>
</sdnList>
 ';

WITH XMLNAMESPACES ( DEFAULT 'http://tempuri.org/sdnList.xsd' ) 
	SELECT  publshInformation.value('Publish_Date[1]', 'NVARCHAR(255)') AS PublishDate, 
		publshInformation.value('Record_Count[1]', 'NVARCHAR(255)') AS RecordCount
	FROM    @Xml.nodes('/sdnList/publshInformation') A ( publshInformation );

WITH XMLNAMESPACES ( DEFAULT 'http://tempuri.org/sdnList.xsd' ) 
	SELECT  sdnEntry.value('uid[1]', 'NVARCHAR(255)') AS [Uid], 
		sdnEntry.value('firstName[1]', 'NVARCHAR(255)') AS FirstName, 
		sdnEntry.value('lastName[1]', 'NVARCHAR(255)') AS LastName, 
		sdnEntry.value('title[1]', 'NVARCHAR(255)') AS Title, 
		sdnEntry.value('sdnType[1]', 'NVARCHAR(255)') AS SdnType
	FROM    @Xml.nodes('/sdnList/sdnEntry') A ( sdnEntry );

WITH XMLNAMESPACES ( DEFAULT 'http://tempuri.org/sdnList.xsd' ) 
	SELECT  sdnEntry.value('uid[1]', 'NVARCHAR(255)') AS [Uid], 
		program.value('.', 'NVARCHAR(255)') AS Program
	FROM    @Xml.nodes('/sdnList/sdnEntry') A ( sdnEntry )
	CROSS APPLY sdnEntry.nodes('programList/program') B ( program );

WITH XMLNAMESPACES ( DEFAULT 'http://tempuri.org/sdnList.xsd' ) 
	SELECT  sdnEntry.value('uid[1]', 'NVARCHAR(255)') AS [Uid], 
		aka.value('uid[1]', 'NVARCHAR(255)') AS AkaUid
	FROM    @Xml.nodes('/sdnList/sdnEntry') A ( sdnEntry )
	CROSS APPLY sdnEntry.nodes('akaList/aka') B ( aka );

WITH XMLNAMESPACES ( DEFAULT 'http://tempuri.org/sdnList.xsd' ) 
	SELECT  sdnEntry.value('uid[1]', 'NVARCHAR(255)') AS [Uid], 
		dateOfBirthItem.value('uid[1]', 'NVARCHAR(255)') AS DateOfBirthUid,
		dateOfBirthItem.value('dateOfBirth[1]', 'NVARCHAR(255)') AS DateOfBirthDate
	FROM    @Xml.nodes('/sdnList/sdnEntry') A ( sdnEntry )
	CROSS APPLY sdnEntry.nodes('dateOfBirthList/dateOfBirthItem') B ( dateOfBirthItem );

Open in new window

0
 

Author Closing Comment

by:johnnyg123
ID: 40325881
Excellent !  ... Exactly what I was looking for!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question