Trouble with XSD, XML to SQL Import - Text node is not allowed in this location

ViarneUniomia

New Member
I am creating my first XSD, as I have a 4MB XML file I need to parse into SQL, and processing something that big takes far too long using untyped XML (I gave up and cancelled the query after an hour).The XML file I have is in the following format (there are more elements for each product, but I shortened it and created a test XML file until I get it right):\[code\] <ITEMS> <CREATED value="http://stackoverflow.com/questions/10762044/Wed May 2 9:40:38 BST 2012"> <PRODUCT ITEM="0001"> <MODEL>MODELNO1</MODEL> <BARCODE>5550204425</BARCODE> <TITLE>Item 1 Title</TITLE> </PRODUCT> <PRODUCT ITEM="0002"> <MODEL>MODELNO2</MODEL> <BARCODE>52614343433</BARCODE> <TITLE>Item 2 Title</TITLE> </PRODUCT> <PRODUCT ITEM="0003"> <MODEL>MODELNO3</MODEL> <BARCODE>32563533</BARCODE> <TITLE>Item 3 Title</TITLE> </PRODUCT> <PRODUCT ITEM="0004"> <MODEL>MODELNO4</MODEL> <BARCODE>65135647582</BARCODE> <TITLE>Item 4 Title</TITLE> </PRODUCT> <PRODUCT ITEM="0005"> <MODEL>MODELNO5</MODEL> <BARCODE>65874112</BARCODE> <TITLE>Item 4 Title</TITLE> </PRODUCT> </CREATED> </ITEMS>\[/code\]This XML file is auto-generated by an external supplier system, and I have no choice but to work with it in its current format. I created this schema for it:\[code\]<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="ITEMS"> <xs:complexType> <xs:sequence> <xs:element name="CREATED"> <xs:complexType> <xs:sequence> <xs:element name="PRODUCT" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="MODEL" type="xs:string" maxOccurs="unbounded" /> <xs:element name="BARCODE" type="xs:string" maxOccurs="unbounded" /> <xs:element name="TITLE" type="xs:string" maxOccurs="unbounded" /> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element></xs:schema>\[/code\]In SQL, I first created a schema collection, like so:\[code\]IF EXISTS ( SELECT * FROM sys.xml_schema_collections where [name] = 'MyXmlSchema')DROP XML SCHEMA COLLECTION [MyXmlSchema]GODECLARE @MySchema XMLSET @MySchema = ( SELECT * FROM OPENROWSET ( BULK 'C:\test\schema2.xsd', SINGLE_CLOB ) AS xmlData)CREATE XML SCHEMA COLLECTION [MyXmlSchema] AS @MySchema GO\[/code\]I then created a table based on the schema:\[code\]CREATE TABLE [dbo].[XMLProds] ( [MODEL] xml(CONTENT dbo.[MyXmlSchema]) NOT NULL, [EAN] xml(CONTENT dbo.[MyXmlSchema]) NOT NULL, [NAME] xml(CONTENT dbo.[MyXmlSchema]) NOT NULL)\[/code\]And finally, validated the XML:\[code\]DECLARE @x2 XML ([MyXmlSchema])SELECT @x2 = '<copied the code from the test XML file and pasted here>'\[/code\]The validation didn't like the date value in the 'CREATED' field, which I can live without as it's only declared once and can be easily deleted. But it also didn't like the 'ITEM' value in each of the product fields, which is problem no. 2. This can't be ignored as it appears in every single item (all 2-3 thousand of them). Is there a way to get around this?Just to crack on, I deleted the unwanted values from the test XML and the validation passed. I then executed this statement in an attempt to populate the table:\[code\]INSERT INTO XMLProds (MODEL, BARCODE, TITLE) SELECT X.product.query('MODEL').value('.', 'VARCHAR(20)'), X.product.query('BARCODE').value('.', 'VARCHAR(50)'), X.product.query('TITLE').value('.', 'VARCHAR(150)')FROM ( SELECT CAST(x AS XML) FROM OPENROWSET(BULK 'C:\test\Products2test.xml', SINGLE_BLOB) AS T(x)) AS T(x) CROSS APPLY x.nodes('/ITEMS/CREATED/PRODUCT') AS X(product);\[/code\]..but was met with the following error:\[quote\] Msg 6909, Level 16, State 1, Line 21 XML Validation: Text node is not allowed at this location, the type was defined with element only content or with simple content. Location: /\[/quote\]Any help with where I'm going wrong here would be greatly appreciated! Thanks in advance.
 
Back
Top