XQuery Basics

I want to visually see where the following 8 items below exists:

XQuery returns sequences like:                                                      Examples:
1- Atomic values; sequence can include atomic val.                   1.  Is this like number 3?  
2- Complex values (XML nodes).                                                    2. Are nodes <root>, <a> etc?
3- Element,                                                                                         3. Are a, b, c, d elements?
4- Attribute,                                                                                        4. I guess none given here.??
5- Text,                                                                                                5. I guess none given here.??
6- Processing instruction,                                                                6. n/a ??
7- Comment, or                                                                                 7. n/a ??
8- Document,                                                                                     8. n/a ??

In the following example:
DECLARE @x AS XML;
SET @x=N'
<root>
<a>1<c>3</c><d>4</d></a>
<b>2</b>
</root>';
SELECT
@x.query('*') AS Complete_Sequence, 
@x.query('data(*)') AS Complete_Data,
@x.query('data(root/a/c)') AS Element_c_Data;

Complete_Sequence                                Complete_Data     Element_c_Data
<root><a>1<c>3</c><d>4</d></a><b>2</b></root>       1342            3

Open in new window


I have plenty of links to read through. After reading the links, the above items are questions I have. There will be a follow up question requesting a better example so I could see all of the 8 items listed above.
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
I guess you're looking for:

DECLARE @x AS XML;

SET @x=N'
<!-- Comment -->
<?PITarget PIContent?>
<root>
	<a>1<c>3</c><d>4</d></a>
	<b>2</b>
</root>
';

SELECT	@x.query('*') AS Complete_Sequence, 
	@x.query('data(*)') AS Complete_Data,
	@x.query('data(root/a/c)') AS Element_c_Data,
	@x.query('processing-instruction()') AS processinginstruction,
	@x.query('comment()') AS Comment,
	@x.query('.') AS Document;

Open in new window


text() returns the textual value of a node, thus you need to select one and attributes are selected by node/@attribute_name as value:

DECLARE @x AS XML;

SET @x=N'
<!-- Comment -->
<?PITarget PIContent?>
<root>
	<a>1<c>3</c><d>4</d></a>
	<b id="123">2</b>
</root>
';

SELECT	@x.query('/root/a/text()') AS Text1,
	@x.query('/root/b/text()') AS Text2,
	@x.value('(/root/b)[1]/@id', 'INT') AS Attribute;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.