Solved

xml query

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

679 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