Solved

xml query

Posted on 2014-09-15
3
275 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
3 Comments
 
LVL 32

Accepted Solution

by:
ste5an earned 500 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

757 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now