Solved

xml query

Posted on 2014-09-15
3
283 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 33

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LTrim & Double Space Correction 5 40
SQL Pivot with row total 5 26
Check ALL SP in database make sure there are no errors 17 43
Download ms sql express. 2 25
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

808 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