Solved

xml query

Posted on 2014-09-15
3
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 34

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

623 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