Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

A Generic Process to Convert XML Data - Part 1 Expand / Collapse
Author
Message
Posted Saturday, August 23, 2008 11:20 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 12:43 PM
Points: 138, Visits: 269
Comments posted to this topic are about the item A Generic Process to Convert XML Data - Part 1


Post #557788
Posted Monday, August 25, 2008 11:11 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, February 2, 2013 8:21 AM
Points: 283, Visits: 268
So, basically you are talking about an Audit table that just stores the "Audits" in a different Xml format? I've been doing that for a while but never relied on the Schemas (I guess I should). Basically, a View can be created for each "type" or "xml schema" so that if you DID need to see what audits took place, it's pretty easy to report on.

I've recently done something similar with regards to importing multiple Products into our database but support an old Xml format as well. So, basically I created two Xml Schema Collections (called them ProductSchema_v01 and ProductSchema_v02). The Stored Procedure then ensures that the Xml data coming in conforms to one or the other schemas and then imports the data appropriately based on the version.

CREATE PROCEDURE [dbo].[usp_ImportProducts]
(
@Products Xml
)
AS
Declare @v1 Xml (ProductSchema_v01)
Declare @v2 Xml (ProductSchema_v02)

BEGIN TRY
Set @v1 = @Products
END TRY
BEGIN CATCH
BEGIN TRY
Set @v2 = @Products
END TRY
BEGIN CATCH
RAISERROR('Invalid Xml...', 16, 1)
END CATCH
END CATCH

If (@v1 Is Not Null)
....
Else If (@v2 Is Not Null)
....




Post #558307
Posted Tuesday, August 26, 2008 6:20 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, July 17, 2014 10:36 PM
Points: 5,303, Visits: 1,378
Good one...


Post #558789
Posted Tuesday, August 26, 2008 8:25 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, April 14, 2014 4:18 PM
Points: 1,276, Visits: 1,132
Good article! One question did come to mind as I read it: would it be possible to modify your method to use a single XML schema for validation against multiple namespaces:

DROP XML SCHEMA COLLECTION stat;
GO

CREATE XML SCHEMA COLLECTION stat
AS
'<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="leo:stat:a" xmlns="leo:stat:a">
<xs:element name="statroot">
<xs:complexType>
<xs:sequence>
<xs:element name="statrecord" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="companyid" type="xs:integer" />
<xs:element name="prodno" type="xs:string" />
<xs:element name="userid" type="xs:integer" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema> ';
GO

ALTER XML SCHEMA COLLECTION stat
ADD '<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="leo:stat:b" xmlns="leo:stat:b">
<xs:element name="statroot">
<xs:complexType>
<xs:sequence>
<xs:element name="statrecord" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="co" type="xs:integer" />
<xs:element name="prod" type="xs:string" />
<xs:element name="uid" type="xs:integer" />
<xs:element name="ext" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema> '
GO

declare @xmlvar1 xml (stat);

set @xmlvar1 = '<xs:statroot xmlns:xs="leo:stat:a">
<statrecord>
<companyid>6</companyid>
<prodno>prd12</prodno>
<userid>12345</userid>
</statrecord>
</xs:statroot>
<xs:statroot xmlns:xs="leo:stat:b">
<statrecord>
<co>6</co>
<prod>prd12</prod>
<uid>12345</uid>
<ext>Test</ext>
</statrecord>
</xs:statroot>'

This would require you to pass in the namespace qualifiers in your input XML, but might eliminate the need to pass the XML schema collection name.

Thanks!
Post #558924
Posted Tuesday, August 26, 2008 7:17 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Yesterday @ 12:43 PM
Points: 138, Visits: 269
You can do it. But I did not add it because I need to be able to get different XML schemas with different namespaces generically. You will see it in part 2. Sorry, but it has to be a drawing of the process but for some reason it didn't shows up. I did email to the publisher. They should correct it.


Post #559297
Posted Friday, July 3, 2009 4:31 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, January 25, 2012 8:14 AM
Points: 567, Visits: 512
Why is this a sql centric solution? Would performance be further improved with some C# code on an application server doing the xml heaving lifting?
Post #747129
Posted Friday, July 3, 2009 8:09 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, February 2, 2013 8:21 AM
Points: 283, Visits: 268
I don't see how performance could be improved by using an application solution. SQL is already set-based as can insert, update, and delete anything from the Xml you send in in one pass. CLR or any C# solution on the other hand will need to issue multiple statements to the database (and since you're talking about an application service - you also talking about multiple network passes back and forth between SQL Server). No, this would slow down performance drastically. You'd be surprised how efficient the Xml query engine is in SQL 2005 and up.


Post #747140
Posted Saturday, July 4, 2009 5:50 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, April 14, 2014 4:18 PM
Points: 1,276, Visits: 1,132
Eric Inman (7/3/2009)
Why is this a sql centric solution? Would performance be further improved with some C# code on an application server doing the xml heaving lifting?


If you're interested in guaranteeing data stored in the database is valid you have to use the database engine to check it, just like any other database-side constraints. You can always check data in client apps or in the middle tier before it gets stored in the database, but there's no guarantee that these methods of enforcing constraints on data can't be circumvented (by a DBA with direct table access, for instance).
Post #747272
Posted Saturday, July 4, 2009 10:47 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, January 25, 2012 8:14 AM
Points: 567, Visits: 512
tymberwyld (7/3/2009)
I don't see how performance could be improved by using an application solution. SQL is already set-based as can insert, update, and delete anything from the Xml you send in in one pass. CLR or any C# solution on the other hand will need to issue multiple statements to the database (and since you're talking about an application service - you also talking about multiple network passes back and forth between SQL Server). No, this would slow down performance drastically. You'd be surprised how efficient the Xml query engine is in SQL 2005 and up.


The Insert Update and Delete are of course set based, parsing the DOM is about as RBAR as you can get. Who cares if its not apparnt to the developer writing the code. In modern web applications, round trips are not as costly as they used to be in client server set ups. The data tier and the web tier are usuallu a few firewalls away on a very fast backplane. That is no argument to introduct xpath and its friends into the data tier.

Do not even get me started with what the TDS protocol will do to a 200k xml blob. Which is what you will start getting if your app group reliazes you are in bed with the XML mistress.

Have you taken into account what XML parsing will do to tempdb? All of this for what, to be able to play with xpath in your stored procs. No thanks. XML is a data strucutre for the application tier.


Post #747302
Posted Saturday, July 4, 2009 10:52 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, January 25, 2012 8:14 AM
Points: 567, Visits: 512
Mike C (7/4/2009)
Eric Inman (7/3/2009)
Why is this a sql centric solution? Would performance be further improved with some C# code on an application server doing the xml heaving lifting?


If you're interested in guaranteeing data stored in the database is valid you have to use the database engine to check it, just like any other database-side constraints. You can always check data in client apps or in the middle tier before it gets stored in the database, but there's no guarantee that these methods of enforcing constraints on data can't be circumvented (by a DBA with direct table access, for instance).


Understand, but I just dont think that passing around a string with over 95% waste in every packet gets that done. I enforce the data requirements in my domain, not the data interface. Over the last few years I have noticed that people are moving more to enforcing the domain data in the data interface layer and not in the domain iteslef. I dont know how this can hold up when said data becomes more than a trivial xml hierarchy.
Post #747303
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse