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

Query question

 
   Database Help (Home) -> XML RSS
Next:  REPOST - MVP Needed - SQL 2005 Reporting Services..  
Author Message
NancyDotCom

External


Since: Oct 01, 2008
Posts: 1



(Msg. 1) Posted: Wed Oct 01, 2008 8:57 am
Post subject: Query question
Archived from groups: microsoft>public>sqlserver>xml (more info?)

Why doesn't this work?

DECLARE @x XML
SELECT @x = '
<OnlineResponse xmlns="http://www.myURL.com/schema/online" version="1.0"
message="Valid Format"></OnlineResponse>'

SELECT @x.query('data(OnlineResponse/@message)') AS message

But if you remove the xmlns attribute from the XML, it works.

 >> Stay informed about: Query question 
Back to top
Login to vote
Martin Honnen

External


Since: Apr 20, 2007
Posts: 47



(Msg. 2) Posted: Wed Oct 01, 2008 12:25 pm
Post subject: Re: Query question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

NancyDotCom wrote:
> Why doesn't this work?
>
> DECLARE @x XML
> SELECT @x = '
> <OnlineResponse xmlns="http://www.myURL.com/schema/online" version="1.0"
> message="Valid Format"></OnlineResponse>'
>
> SELECT @x.query('data(OnlineResponse/@message)') AS message
>
> But if you remove the xmlns attribute from the XML, it works.

OnlineResponse selects elements with local name 'OnlineResponse' in _no
namespace_ while your element is in the namespace
http://www.myURL.com/schema/online. With XQuery you first need to
declare the default namespace with e.g.

SELECT @x.query('declare default element namespace
"http://www.myURL.com/schema/online"; data(OnlineResponse/@message)') AS
message

or you need to use a prefix e.g.

SELECT @x.query('declare namespace
df="http://www.myURL.com/schema/online";
data(df:OnlineResponse/@message)') AS message

A third option is

SELECT @x.query('data(*:OnlineResponse/@message)') AS message

as that selects elements with local name 'OnlineResponse' in any
namespace, including no namespace.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

 >> Stay informed about: Query question 
Back to top
Login to vote
Jacob Sebastian

External


Since: Aug 18, 2008
Posts: 21



(Msg. 3) Posted: Thu Oct 02, 2008 2:25 am
Post subject: Re: Query question [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

and, if you find the namespace declaration within the query() method too
confusing, you can do it using WITH XMLNAMESPACES as given below.

;WITH XMLNAMESPACES(
DEFAULT 'http://www.myURL.com/schema/online'
)
SELECT @x.query('data(OnlineResponse/@message)') AS message

--
Jacob Sebastian
SQL Server MVP
http://www.sqlserverandxml.com

"Martin Honnen" <mahotrash.RemoveThis@yahoo.de> wrote in message
news:#QW2KB#IJHA.3936@TK2MSFTNGP03.phx.gbl...
> NancyDotCom wrote:
>> Why doesn't this work?
>>
>> DECLARE @x XML SELECT @x = '
>> <OnlineResponse xmlns="http://www.myURL.com/schema/online" version="1.0"
>> message="Valid Format"></OnlineResponse>'
>>
>> SELECT @x.query('data(OnlineResponse/@message)') AS message
>>
>> But if you remove the xmlns attribute from the XML, it works.
>
> OnlineResponse selects elements with local name 'OnlineResponse' in _no
> namespace_ while your element is in the namespace
> http://www.myURL.com/schema/online. With XQuery you first need to declare
> the default namespace with e.g.
>
> SELECT @x.query('declare default element namespace
> "http://www.myURL.com/schema/online"; data(OnlineResponse/@message)') AS
> message
>
> or you need to use a prefix e.g.
>
> SELECT @x.query('declare namespace
> df="http://www.myURL.com/schema/online";
> data(df:OnlineResponse/@message)') AS message
>
> A third option is
>
> SELECT @x.query('data(*:OnlineResponse/@message)') AS message
>
> as that selects elements with local name 'OnlineResponse' in any
> namespace, including no namespace.
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
 >> Stay informed about: Query question 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Easy query question - This one should be easy, but for some reason I can't get it. I have a table with an XML column, containing the following: <xs:ReservationChargeItem xmlns:xs="http://www.hoboo.com/ReservationSchema" DropOffCode="" PickUpCode=&quo...

BulkLoad question - In order to upload data using SQLXML Bulkload, must the data and schema reside in a file, or can you store the data to a variable, then use Bulkload ?

XML Question - Hello I have the following XML file <?xml version="1.0" encoding="us-ascii" ?> - <clsProdDataSingleBill> - <PhoneNo> <string>0893877610</string> </PhoneNo> - <Email> <string>D...

Annotation question - Hi, I was wondering if anybody could assist me with annotated XSDs when attempting to bulkload XML. Here's the structure of the xml I'm receiving and attempting to load into 2 tables. <?xml version="1.0" encoding="UTF-8" sta...

Shredding XML Question - I need to bring the following data in an xml format (many more years included) into the table listed at the bottom of this request. This data is in a large text file, say in c:\calendar.xml <Calendar> <ActualDate>2005-01-01T00:00:00</A...
   Database Help (Home) -> XML All times are: Pacific Time (US & Canada) (change)
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 ]