Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Type XML

 
   Database Help (Home) -> XML RSS
Next:  Update with Date format  
Author Message
kpb

External


Since: Nov 06, 2008
Posts: 1



(Msg. 1) Posted: Thu Nov 06, 2008 2:10 pm
Post subject: Type XML
Archived from groups: microsoft>public>sqlserver>xml (more info?)

I have the following tsql:

-- TSQL START
DECLARE @XSD varchar(MAX)
SET @XSD = '<?xml version="1.0" encoding="utf-8" ?>
<xs:schema elementFormDefault="qualified" targetNamespace="http://a"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Widget">
<xs:complexType>
<xs:all>
<xs:element name="var1" minOccurs="0" maxOccurs="1"
type="xs:short" />
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>'

CREATE XML SCHEMA COLLECTION [Test_XMLText_Schema] AS @XSD
GO

DECLARE @xmlUnTyped xml
DECLARE @xmlTyped xml (DOCUMENT Test_XMLText_Schema)

SET @xmlUnTyped = '<?xml version="1.0" encoding="utf-8"?>
<Widget xmlns="http://a">
<var1/>
</Widget>'


BEGIN TRY
SET @xmlTyped = @xmlUnTyped
SELECT @xmlTyped
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
END CATCH

DROP XML SCHEMA COLLECTION [dbo].[Test_XMLText_Schema]

-- TSQL END


It loads the XML fine into the untyped XML variable. Trying to load it
into the typed XML variable I receive the following error:
XML Validation: Invalid simple type value: ''. Location: /*:Widget[1]/
*:var1[1]

Adding nillable="true" to var1 XSD declaration does not help.
Changing the type of var1 element to xs:string allows it to work but I
don't want to do that if I don't have to.

Ithink I'm just missing something really simple.

Thanks in advance,

Kevin

 >> Stay informed about: Type XML 
Back to top
Login to vote
Bob Beauchemin

External


Since: Oct 10, 2008
Posts: 8



(Msg. 2) Posted: Fri Nov 07, 2008 5:25 am
Post subject: Re: Type XML [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

The way you've defined the XML schema you have two choices:
1. "var1" must be of type xs:short
2. "var1" can be missing

If add add nillable=true to the schema definition of var1, this also works:
SET @xmlTyped = '<?xml version="1.0" encoding="utf-8"?>
<Widget xmlns="http://a"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<var1 xsi:nil="true"/>
</Widget>'

By using <var1/> in a document, you are saying that var1's content is an
empty string. Which also works if you type var1 as a string (but not as an
xs:short).

Hope this helps,
Cheers,
Bob Beauchemin
SQLskills

"kpb" wrote in message

>I have the following tsql:
>
> -- TSQL START
> DECLARE @XSD varchar(MAX)
> SET @XSD = '<?xml version="1.0" encoding="utf-8" ?>
> <xs:schema elementFormDefault="qualified" targetNamespace="http://a"
> xmlns:xs="http://www.w3.org/2001/XMLSchema">
> <xs:element name="Widget">
> <xs:complexType>
> <xs:all>
> <xs:element name="var1" minOccurs="0" maxOccurs="1"
> type="xs:short" />
> </xs:all>
> </xs:complexType>
> </xs:element>
> </xs:schema>'
>
> CREATE XML SCHEMA COLLECTION [Test_XMLText_Schema] AS @XSD
> GO
>
> DECLARE @xmlUnTyped xml
> DECLARE @xmlTyped xml (DOCUMENT Test_XMLText_Schema)
>
> SET @xmlUnTyped = '<?xml version="1.0" encoding="utf-8"?>
> <Widget xmlns="http://a">
> <var1/>
> </Widget>'
>
>
> BEGIN TRY
> SET @xmlTyped = @xmlUnTyped
> SELECT @xmlTyped
> END TRY
> BEGIN CATCH
> PRINT ERROR_MESSAGE()
> END CATCH
>
> DROP XML SCHEMA COLLECTION [dbo].[Test_XMLText_Schema]
>
> -- TSQL END
>
>
> It loads the XML fine into the untyped XML variable. Trying to load it
> into the typed XML variable I receive the following error:
> XML Validation: Invalid simple type value: ''. Location: /*:Widget[1]/
> *:var1[1]
>
> Adding nillable="true" to var1 XSD declaration does not help.
> Changing the type of var1 element to xs:string allows it to work but I
> don't want to do that if I don't have to.
>
> Ithink I'm just missing something really simple.
>
> Thanks in advance,
>
> Kevin

 >> Stay informed about: Type XML 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
XML data type in distributed environment - In SQL server 2005, distributed query is not supported if there is xml type column in the table. I could use nvarchar in the table and create view and cast it to xml to work around it. But how could I utilize the xml schema? How to utilize XML index in..

Study Material : Data type conversion - I am trying to upload an xml file into SQL database by writing a xsd and using bulkload in VB. While doing this, I find that the datatypes in SQL differ from xsd. How do I perform type casting in this cases. Can someone guide me to a good material? ..

XQuery SQL 2005 Insert block of xml into existing xml type - I am trying to use XQuery in SQL Server 2005 to insert a block of xml, 2 people into an existing XML document. In order to use modify('insert... I cannot use the type XML, but the Varchar creates improper tags &lt; instead of < , etc... how can I ...

XML / XML Data Type Question - I am pretty much a neophyte when it comes to XML. Here is my question. Assume the simple XML fragment below is assigned to an SQL 2005 XML data type variable, could someone tell me how I could programmatically accomplish the following? 1) Determine..

xml to SQL DB - We have some large existing Xml files that we'd like to store in the SQL server. We don't want to store just the Xml stream, but we rather like to build the hierarchical structure that represents the Xml in SQL and store the Xml that way. We also want...
   Database Help (Home) -> XML All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]