dbTalk Databases Forums  

XQuery for Value Not Getting Attribute - Help!

microsoft.public.sqlserver.xml microsoft.public.sqlserver.xml


Discuss XQuery for Value Not Getting Attribute - Help! in the microsoft.public.sqlserver.xml forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
SnapDive
 
Posts: n/a

Default XQuery for Value Not Getting Attribute - Help! - 06-13-2010 , 09:38 PM






SQL Server 2008 (not r2)


I am trying to get the Attribute values "TestAccount1" and
"TestAccount2" from the below XML. I can't seem to get the xquery
correct in order to get at the value. In my real stuff there are 5,000
<Row> elements.

Help!

Declare @xmlRaw Xml =
'<?xml version="1.0" encoding="utf-8"?>
<Report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://testdom.intrnl.com/quest/dev/v5/SchemaA"
devteamblue="false">
<Possibles>
<Column name="AccountName" />
</Possibles>
<Table>
<Row>
<AccountName value="TestAccount1">unwanted</AccountName>
<AccountGroup value="People" />
</Row>
<Row>
<AccountName value="TestAccount2">wanted</AccountName>
<AccountGroup value="Places" />
</Row>
</Table>
</Report>'

-- returns null
Select xItem.value('(AccountName)[1]','varchar(256)' ) as fff from
@xmlRaw.nodes('/') as x(xItem)


-- Help!

Thanks.

Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: XQuery for Value Not Getting Attribute - Help! - 06-14-2010 , 06:50 AM






SnapDive wrote:

Quote:
I am trying to get the Attribute values "TestAccount1" and
"TestAccount2" from the below XML. I can't seem to get the xquery
correct in order to get at the value. In my real stuff there are 5,000
Row> elements.

Help!

Declare @xmlRaw Xml =
'<?xml version="1.0" encoding="utf-8"?
Report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://testdom.intrnl.com/quest/dev/v5/SchemaA"
devteamblue="false"
Possibles
Column name="AccountName" /
/Possibles
Table
Row
AccountName value="TestAccount1">unwanted</AccountName
AccountGroup value="People" /
/Row
Row
AccountName value="TestAccount2">wanted</AccountName
AccountGroup value="Places" /
/Row
/Table
/Report>'

-- returns null
Select xItem.value('(AccountName)[1]','varchar(256)' ) as fff from
@xmlRaw.nodes('/') as x(xItem)
WITH XMLNAMESPACES (DEFAULT
'http://testdom.intrnl.com/quest/dev/v5/SchemaA')
Select xItem.value('(AccountName/@value)[1]','varchar(256)' ) as fff
from @xmlRaw.nodes('/Report/Table/Row') as x(xItem)

might do what you want, assuming you want to read out the "value"
attribute of the "AccountName" child of each "Row" element.


--

Martin Honnen --- MVP Data Platform Development
http://msmvps.com/blogs/martin_honnen/

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.