Showing posts with label schema. Show all posts
Showing posts with label schema. Show all posts

Monday, March 19, 2012

Missing XML header that specifies the encoding

Hello,
In my solution I ned the xml output with including the XML header thet
specifies the encoding.
Here's the code:
CREATE XML SCHEMA COLLECTION MyNote AS
N'<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://
www.w3schools.com" elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="HedingType" type="xs:string"/>
</xs:schema>'
GO
then input a valid xml
DECLARE @.x xml (dbo.MyNote)
SET @.x=N'<note xmlns="http://www.w3schools.com">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this wend!</body>
</note>'
select @.x
Here's the output:
<note xmlns="http://www.w3schools.com">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this wend!</body>
</note>
I need the XML header that specify the encoding of the document, like
this:
<?xml version="1.0" encoding="UTF-8"?>
<note xmlns="http://www.w3schools.com">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this wend!</body>
</note>
Any ideas?
Thanks in advance!
JohnWell the heading is optional for UTF-8 documents. The heading's only useful
for serialised documents, how are you using the output?
Joe Fawcett (MVP - XML)
http://joe.fawcett.name
"johnk" <johnk_hansen@.hotmail.com> wrote in message
news:1170427960.566676.200010@.s48g2000cws.googlegroups.com...
> Hello,
> In my solution I ned the xml output with including the XML header thet
> specifies the encoding.
> Here's the code:
> CREATE XML SCHEMA COLLECTION MyNote AS
> N'<?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> targetNamespace="http://www.w3schools.com" xmlns="http://
> www.w3schools.com" elementFormDefault="qualified">
> <xs:element name="note">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="to" type="xs:string"/>
> <xs:element name="from" type="xs:string"/>
> <xs:element name="heading" type="xs:string"/>
> <xs:element name="body" type="xs:string"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> <xs:element name="HedingType" type="xs:string"/>
> </xs:schema>'
> GO
> then input a valid xml
> DECLARE @.x xml (dbo.MyNote)
> SET @.x=N'<note xmlns="http://www.w3schools.com">
> <to>Tove</to>
> <from>Jani</from>
> <heading>Reminder</heading>
> <body>Don't forget me this wend!</body>
> </note>'
> select @.x
> Here's the output:
> <note xmlns="http://www.w3schools.com">
> <to>Tove</to>
> <from>Jani</from>
> <heading>Reminder</heading>
> <body>Don't forget me this wend!</body>
> </note>
> I need the XML header that specify the encoding of the document, like
> this:
> <?xml version="1.0" encoding="UTF-8"?>
> <note xmlns="http://www.w3schools.com">
> <to>Tove</to>
> <from>Jani</from>
> <heading>Reminder</heading>
> <body>Don't forget me this wend!</body>
> </note>
> Any ideas?
>
> Thanks in advance!
> John
>|||On 2 Feb, 17:46, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote:
> Well the heading is optional for UTF-8 documents. The heading's only usefu
l
> for serialised documents, how are you using the output?
> --
> Joe Fawcett (MVP - XML)http://joe.fawcett.name
> "johnk" <johnk_han...@.hotmail.com> wrote in message
> news:1170427960.566676.200010@.s48g2000cws.googlegroups.com...
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> - Vis sitert tekst -
I am using the query window in MS SQL Server Management Studio .|||John,
The processing instruction specifies the encoding when the XML is stored as
text. When you store an instance in an XML datatype, it is not stored as tex
t
but in binary XML format. Therefore, what's the point of specifying an XML
encoding? It would have no meaning.
In your case, I would suggest you convert your XML instance to a string type
and
add the processing instruction trhough string manipulation. You can do somet
hing
like this
DECLARE @.x xml (dbo.MyNote)
DECLARE @.strXML varchar(MAX)
SET @.x=N'<note xmlns="http://www.w3schools.com">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this wend!</body>
</note>'
set @.strXML = '<?xml version="1.0" encoding="UTF-8"?>' + CONVERT(varchar(MAX
),
@.x)
SELECT @.strXML
While we're talking about encoding of XML documents I've also noticed someth
ing
strange in your example. When you create your schema collection you do this
CREATE XML SCHEMA COLLECTION MyNote AS
N'<?xml version="1.0" encoding="UTF-8"?>
....
'
go
The string literal is unicode (because of the 'N' character that preceeds it
)
but yet you specify the encoding as UTF-8 in your processing instruction. Th
e
DDL as you've written it should fail with the following error: "XML parsing:
line 1, character 38, unable to switch the encoding"
Denis Ruckebusch
http://blogs.msdn.com/denisruc
--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"johnk" <johnk_hansen@.hotmail.com> wrote in message
news:1170427960.566676.200010@.s48g2000cws.googlegroups.com...
> Hello,
> In my solution I ned the xml output with including the XML header thet
> specifies the encoding.
> Here's the code:
> CREATE XML SCHEMA COLLECTION MyNote AS
> N'<?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> targetNamespace="http://www.w3schools.com" xmlns="http://
> www.w3schools.com" elementFormDefault="qualified">
> <xs:element name="note">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="to" type="xs:string"/>
> <xs:element name="from" type="xs:string"/>
> <xs:element name="heading" type="xs:string"/>
> <xs:element name="body" type="xs:string"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> <xs:element name="HedingType" type="xs:string"/>
> </xs:schema>'
> GO
> then input a valid xml
> DECLARE @.x xml (dbo.MyNote)
> SET @.x=N'<note xmlns="http://www.w3schools.com">
> <to>Tove</to>
> <from>Jani</from>
> <heading>Reminder</heading>
> <body>Don't forget me this wend!</body>
> </note>'
> select @.x
> Here's the output:
> <note xmlns="http://www.w3schools.com">
> <to>Tove</to>
> <from>Jani</from>
> <heading>Reminder</heading>
> <body>Don't forget me this wend!</body>
> </note>
> I need the XML header that specify the encoding of the document, like
> this:
> <?xml version="1.0" encoding="UTF-8"?>
> <note xmlns="http://www.w3schools.com">
> <to>Tove</to>
> <from>Jani</from>
> <heading>Reminder</heading>
> <body>Don't forget me this wend!</body>
> </note>
> Any ideas?
>
> Thanks in advance!
> John
>|||In addition to Denis' post: You cannot get UTF-8 encoded XML data produced.
SQL Server produces UTF-16 encoded XML when you cast it to
NVARCHAR/VARBINARY, or the encoding of the code page if you cast to
VARCHAR(not recommended).
Since the NVARCHAR/VARCHAR carries the encoding on the type, we do not add
an XML declaration with an encoding, so that if you cast your string to a
different collation, the encoding does get adjusted automatically.
Now if you cast it to VARBINARY we will add the UTF-16 BOM.
If you want to produce UTF-8 encoded XML or have the XML declaration added,
you probably should use the System.XML or MSXML components on the
client/midtier side.
Best regards
Michael
"johnk" <johnk_hansen@.hotmail.com> wrote in message
news:1170439250.695037.296940@.q2g2000cwa.googlegroups.com...
> On 2 Feb, 17:46, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote:
> I am using the query window in MS SQL Server Management Studio .
>

Missing XML header that specifies the encoding

Hello,
In my solution I ned the xml output with including the XML header thet
specifies the encoding.
Here's the code:
CREATE XML SCHEMA COLLECTION MyNote AS
N'<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com" xmlns="http://
www.w3schools.com" elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="HedingType" type="xs:string"/>
</xs:schema>'
GO
then input a valid xml
DECLARE @.x xml (dbo.MyNote)
SET @.x=N'<note xmlns="http://www.w3schools.com">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>'
select @.x
Here's the output:
<note xmlns="http://www.w3schools.com">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
I need the XML header that specify the encoding of the document, like
this:
<?xml version="1.0" encoding="UTF-8"?>
<note xmlns="http://www.w3schools.com">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Any ideas?
Thanks in advance!
John
Well the heading is optional for UTF-8 documents. The heading's only useful
for serialised documents, how are you using the output?
Joe Fawcett (MVP - XML)
http://joe.fawcett.name
"johnk" <johnk_hansen@.hotmail.com> wrote in message
news:1170427960.566676.200010@.s48g2000cws.googlegr oups.com...
> Hello,
> In my solution I ned the xml output with including the XML header thet
> specifies the encoding.
> Here's the code:
> CREATE XML SCHEMA COLLECTION MyNote AS
> N'<?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> targetNamespace="http://www.w3schools.com" xmlns="http://
> www.w3schools.com" elementFormDefault="qualified">
> <xs:element name="note">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="to" type="xs:string"/>
> <xs:element name="from" type="xs:string"/>
> <xs:element name="heading" type="xs:string"/>
> <xs:element name="body" type="xs:string"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> <xs:element name="HedingType" type="xs:string"/>
> </xs:schema>'
> GO
> then input a valid xml
> DECLARE @.x xml (dbo.MyNote)
> SET @.x=N'<note xmlns="http://www.w3schools.com">
> <to>Tove</to>
> <from>Jani</from>
> <heading>Reminder</heading>
> <body>Don't forget me this weekend!</body>
> </note>'
> select @.x
> Here's the output:
> <note xmlns="http://www.w3schools.com">
> <to>Tove</to>
> <from>Jani</from>
> <heading>Reminder</heading>
> <body>Don't forget me this weekend!</body>
> </note>
> I need the XML header that specify the encoding of the document, like
> this:
> <?xml version="1.0" encoding="UTF-8"?>
> <note xmlns="http://www.w3schools.com">
> <to>Tove</to>
> <from>Jani</from>
> <heading>Reminder</heading>
> <body>Don't forget me this weekend!</body>
> </note>
> Any ideas?
>
> Thanks in advance!
> John
>
|||On 2 Feb, 17:46, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote:
> Well the heading is optional for UTF-8 documents. The heading's only useful
> for serialised documents, how are you using the output?
> --
> Joe Fawcett (MVP - XML)http://joe.fawcett.name
> "johnk" <johnk_han...@.hotmail.com> wrote in message
> news:1170427960.566676.200010@.s48g2000cws.googlegr oups.com...
>
>
>
>
>
>
>
>
> - Vis sitert tekst -
I am using the query window in MS SQL Server Management Studio .
|||John,
The processing instruction specifies the encoding when the XML is stored as
text. When you store an instance in an XML datatype, it is not stored as text
but in binary XML format. Therefore, what's the point of specifying an XML
encoding? It would have no meaning.
In your case, I would suggest you convert your XML instance to a string type and
add the processing instruction trhough string manipulation. You can do something
like this
DECLARE @.x xml (dbo.MyNote)
DECLARE @.strXML varchar(MAX)
SET @.x=N'<note xmlns="http://www.w3schools.com">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>'
set @.strXML = '<?xml version="1.0" encoding="UTF-8"?>' + CONVERT(varchar(MAX),
@.x)
SELECT @.strXML
While we're talking about encoding of XML documents I've also noticed something
strange in your example. When you create your schema collection you do this
CREATE XML SCHEMA COLLECTION MyNote AS
N'<?xml version="1.0" encoding="UTF-8"?>
.....
'
go
The string literal is unicode (because of the 'N' character that preceeds it)
but yet you specify the encoding as UTF-8 in your processing instruction. The
DDL as you've written it should fail with the following error: "XML parsing:
line 1, character 38, unable to switch the encoding"
Denis Ruckebusch
http://blogs.msdn.com/denisruc
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
"johnk" <johnk_hansen@.hotmail.com> wrote in message
news:1170427960.566676.200010@.s48g2000cws.googlegr oups.com...
> Hello,
> In my solution I ned the xml output with including the XML header thet
> specifies the encoding.
> Here's the code:
> CREATE XML SCHEMA COLLECTION MyNote AS
> N'<?xml version="1.0" encoding="UTF-8"?>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> targetNamespace="http://www.w3schools.com" xmlns="http://
> www.w3schools.com" elementFormDefault="qualified">
> <xs:element name="note">
> <xs:complexType>
> <xs:sequence>
> <xs:element name="to" type="xs:string"/>
> <xs:element name="from" type="xs:string"/>
> <xs:element name="heading" type="xs:string"/>
> <xs:element name="body" type="xs:string"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> <xs:element name="HedingType" type="xs:string"/>
> </xs:schema>'
> GO
> then input a valid xml
> DECLARE @.x xml (dbo.MyNote)
> SET @.x=N'<note xmlns="http://www.w3schools.com">
> <to>Tove</to>
> <from>Jani</from>
> <heading>Reminder</heading>
> <body>Don't forget me this weekend!</body>
> </note>'
> select @.x
> Here's the output:
> <note xmlns="http://www.w3schools.com">
> <to>Tove</to>
> <from>Jani</from>
> <heading>Reminder</heading>
> <body>Don't forget me this weekend!</body>
> </note>
> I need the XML header that specify the encoding of the document, like
> this:
> <?xml version="1.0" encoding="UTF-8"?>
> <note xmlns="http://www.w3schools.com">
> <to>Tove</to>
> <from>Jani</from>
> <heading>Reminder</heading>
> <body>Don't forget me this weekend!</body>
> </note>
> Any ideas?
>
> Thanks in advance!
> John
>
|||In addition to Denis' post: You cannot get UTF-8 encoded XML data produced.
SQL Server produces UTF-16 encoded XML when you cast it to
NVARCHAR/VARBINARY, or the encoding of the code page if you cast to
VARCHAR(not recommended).
Since the NVARCHAR/VARCHAR carries the encoding on the type, we do not add
an XML declaration with an encoding, so that if you cast your string to a
different collation, the encoding does get adjusted automatically.
Now if you cast it to VARBINARY we will add the UTF-16 BOM.
If you want to produce UTF-8 encoded XML or have the XML declaration added,
you probably should use the System.XML or MSXML components on the
client/midtier side.
Best regards
Michael
"johnk" <johnk_hansen@.hotmail.com> wrote in message
news:1170439250.695037.296940@.q2g2000cwa.googlegro ups.com...
> On 2 Feb, 17:46, "Joe Fawcett" <joefawc...@.newsgroup.nospam> wrote:
> I am using the query window in MS SQL Server Management Studio .
>

Saturday, February 25, 2012

missing index on FKY

I am analyzing a database schema I inherited. I am noticing some tables which have FKY to
another table,
but don't have an index on those FKY columns. Coming from another RDBMS background, I find
it strange
that SQLServer even allows this. What is the reason in keeping index-creation a separate and
optional
task from creating a FKY.
Second question: Shouldn't it make sense to create index on FKY columns so that the joins
with the
parent table is faster and can also avoid table locks on the child table.
Yes, creating indexes on foreign keys can be beneficial to query
performance. However, the reason it is not automatically done is that
indexes are not necessary for enforcement of the constraint -- and there are
times that a DBA may not want to create an index. So you're given an
option...
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
"SQL Server DBA" <sqlsdba@.gmail.com> wrote in message
news:39oep6F60jstaU1@.individual.net...
> I am analyzing a database schema I inherited. I am noticing some tables
which have FKY to
> another table,
> but don't have an index on those FKY columns. Coming from another RDBMS
background, I find
> it strange
> that SQLServer even allows this. What is the reason in keeping
index-creation a separate and
> optional
> task from creating a FKY.
> Second question: Shouldn't it make sense to create index on FKY columns so
that the joins
> with the
> parent table is faster and can also avoid table locks on the child table.
>

missing index on FKY

I am analyzing a database schema I inherited. I am noticing some tables whic
h have FKY to
another table,
but don't have an index on those FKY columns. Coming from another RDBMS back
ground, I find
it strange
that SQLServer even allows this. What is the reason in keeping index-creatio
n a separate and
optional
task from creating a FKY.
Second question: Shouldn't it make sense to create index on FKY columns so t
hat the joins
with the
parent table is faster and can also avoid table locks on the child table.Yes, creating indexes on foreign keys can be beneficial to query
performance. However, the reason it is not automatically done is that
indexes are not necessary for enforcement of the constraint -- and there are
times that a DBA may not want to create an index. So you're given an
option...
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--
"SQL Server DBA" <sqlsdba@.gmail.com> wrote in message
news:39oep6F60jstaU1@.individual.net...
> I am analyzing a database schema I inherited. I am noticing some tables
which have FKY to
> another table,
> but don't have an index on those FKY columns. Coming from another RDBMS
background, I find
> it strange
> that SQLServer even allows this. What is the reason in keeping
index-creation a separate and
> optional
> task from creating a FKY.
> Second question: Shouldn't it make sense to create index on FKY columns so
that the joins
> with the
> parent table is faster and can also avoid table locks on the child table.
>

missing index on FKY

I am analyzing a database schema I inherited. I am noticing some tables which have FKY to
another table,
but don't have an index on those FKY columns. Coming from another RDBMS background, I find
it strange
that SQLServer even allows this. What is the reason in keeping index-creation a separate and
optional
task from creating a FKY.
Second question: Shouldn't it make sense to create index on FKY columns so that the joins
with the
parent table is faster and can also avoid table locks on the child table.Yes, creating indexes on foreign keys can be beneficial to query
performance. However, the reason it is not automatically done is that
indexes are not necessary for enforcement of the constraint -- and there are
times that a DBA may not want to create an index. So you're given an
option...
Adam Machanic
SQL Server MVP
http://www.sqljunkies.com/weblog/amachanic
--
"SQL Server DBA" <sqlsdba@.gmail.com> wrote in message
news:39oep6F60jstaU1@.individual.net...
> I am analyzing a database schema I inherited. I am noticing some tables
which have FKY to
> another table,
> but don't have an index on those FKY columns. Coming from another RDBMS
background, I find
> it strange
> that SQLServer even allows this. What is the reason in keeping
index-creation a separate and
> optional
> task from creating a FKY.
> Second question: Shouldn't it make sense to create index on FKY columns so
that the joins
> with the
> parent table is faster and can also avoid table locks on the child table.
>