Paula DiTallo
asked on
Need to Convert Relational Data (SQL Server) to XML using XSD
SQL/XML Brainiacs and Wizards--
I am trying to convert existing non-XML formatted string data from a view. I generated an XSD file for the XML format I need to output. The view's columns/elements are an exact match to the xsd (see select view script below) -- so First is First, Last is Last, Email is Email, etc. I was able to create the schema collection (see code snippet) --however I do not know how to route a select statement from the view to use this --
Please advise...
--------- select view script ------------
SELECT [First]
,[Last]
,[Email]
,[Mobile]
,[Phone]
,[Fax]
,[Address1]
,[Address2]
,[City]
,[State]
,[Country]
,[CountryName]
,[Postal]
,[Territory]
,[TerritoryState]
FROM [MyMetroServer].[dbo].[VW_ SalesForce InterNtl]
I am trying to convert existing non-XML formatted string data from a view. I generated an XSD file for the XML format I need to output. The view's columns/elements are an exact match to the xsd (see select view script below) -- so First is First, Last is Last, Email is Email, etc. I was able to create the schema collection (see code snippet) --however I do not know how to route a select statement from the view to use this --
Please advise...
--------- select view script ------------
SELECT [First]
,[Last]
,[Email]
,[Mobile]
,[Phone]
,[Fax]
,[Address1]
,[Address2]
,[City]
,[State]
,[Country]
,[CountryName]
,[Postal]
,[Territory]
,[TerritoryState]
FROM [MyMetroServer].[dbo].[VW_
CREATE XML SCHEMA COLLECTION SalesRep
AS N'<?xml version="1.0" encoding="utf-16"?>
<xsd:schema id="NewDataSet" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="SalesRep">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="First" type="xsd:string" minOccurs="0" />
<xsd:element name="Last" type="xsd:string" minOccurs="0" />
<xsd:element name="Email" type="xsd:string" minOccurs="0" />
<xsd:element name="Mobile" type="xsd:string" minOccurs="0" />
<xsd:element name="Phone" type="xsd:string" minOccurs="0" />
<xsd:element name="Fax" type="xsd:string" minOccurs="0" />
<xsd:element name="Address1" type="xsd:string" minOccurs="0" />
<xsd:element name="Address2" type="xsd:string" minOccurs="0" />
<xsd:element name="City" type="xsd:string" minOccurs="0" />
<xsd:element name="State" type="xsd:string" minOccurs="0" />
<xsd:element name="Country" type="xsd:string" minOccurs="0" />
<xsd:element name="CountryName" type="xsd:string" minOccurs="0" />
<xsd:element name="Postal" type="xsd:string" minOccurs="0" />
<xsd:element name="Territory" type="xsd:string" minOccurs="0" />
<xsd:element name="TerritoryState" type="xsd:string" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xsd:complexType>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="SalesRep" />
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>';
declare @x XML(CONTENT SalesRep);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Techies--
While waiting on some feedback, I almost solved the issue by writing attached SQL--but-- the line is truncated. Anyone know how to extend the line size?
Here's an example of the output:
<SalesRep><First>Angelica< /First><La st>Souto</ Last><Emai l>angelica .souto@sha winc.com</ Email><Mob ile>1- 305 796 1999</Mobile><Phone>1-305 827 5950</Phone><Fax>1-305 827 5910</Fax><Address1>5881 Northwest 151 Street</Address1><Address2 >Suite 100A</Address2><
While waiting on some feedback, I almost solved the issue by writing attached SQL--but-- the line is truncated. Anyone know how to extend the line size?
Here's an example of the output:
<SalesRep><First>Angelica<
SELECT
'<SalesRep>' +
'<First>' + first + '</First>' +
'<Last>' + last + '</Last>' +
'<Email>' + email + '</Email>' +
case
when Mobile is null then '<Mobile></Mobile>'
when Mobile = '' then '<Mobile></Mobile>'
else '<Mobile>' + PhoneCode + '-' + Mobile + '</Mobile>'
end
+
case
when Phone is null then '<Phone></Phone>'
when Phone = '' then '<Phone></Phone>'
else '<Phone>' + PhoneCode + '-' + Phone + '</Phone>'
end
+
case
when Fax is null then '<Fax></Fax>'
when Fax = '' then '<Fax></Fax>'
else '<Fax>' + PhoneCode + '-' + Fax + '</Fax>'
end
+
'<Address1>' + Address1 + '</Address1>' +
case
when Address2 is null then '<Address2></Address2>'
when Address2 = '' then '<Address2></Address2>'
else '<Address2>' + Address2 + '</Address2>'
end
+
case
when City is null then '<City></City>'
when City = '' then '<City></City>'
else '<City>' + City + '</City>'
end
+
case
when State is null then '<State></State>'
when State = '' then '<State></State>'
else '<State>' + State + '</State>'
end
+
'<Country>' + Country + '</Country>' +
'<CountryName>' + CountryName + '</CountryName>' +
'<Postal>' + Postal + '</Postal>' +
'<Territory>' + Territory + '</Territory>' +
case
when TerritoryState is null then '<TerritoryState></TerritoryState>'
when TerritoryState = '' then '<TerritoryState></TerritoryState>'
else '<TerritoryState>' + TerritoryState + '</TerritoryState>'
end
+
'</SalesRep>'
FROM VW_SalesForceInterNtl
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
As the old adage goes...any port in a storm will do... especially when pressed for time!;-) the task at hand is completed; the escape characters found and managed. Since this was a one-time change over, I wasn't worried about performance. To deal with the rows/escape chars/buffer issue I just output the cvs data from the grid to excel -- did a search and replace on the escape characters.... the process worked fine--even though this method for conversion is far from the preferred way.
Now that the task is complete and I have a little more time to exam the best practices approach, let's reexamine my first approach for a moment. After posting, Hwkranger suggested that I use XML Explicit rather than the Schema Collection I first had in mind to do. I'll look at XML Explicit today--but I am interested in knowing why I can't select from a table/view using the XSD schema collection--I would think it would be more efficient than XML Explicit even--since its all spelled out. Any thoughts?
Now that the task is complete and I have a little more time to exam the best practices approach, let's reexamine my first approach for a moment. After posting, Hwkranger suggested that I use XML Explicit rather than the Schema Collection I first had in mind to do. I'll look at XML Explicit today--but I am interested in knowing why I can't select from a table/view using the XSD schema collection--I would think it would be more efficient than XML Explicit even--since its all spelled out. Any thoughts?
>>Any thoughts?<<
I am afraid the "B" grade deflated any enthusiasm I may have had with this question. Try posting a new question and hopefully someone will step up to the plate.
Good luck.
I am afraid the "B" grade deflated any enthusiasm I may have had with this question. Try posting a new question and hopefully someone will step up to the plate.
Good luck.
ASKER
Fair enough!;-) Thanks for taking the time you did to answer the part of the question you could.
Open in new window