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

Repeatable xml elements in xml columns

 
   Database Help (Home) -> XML RSS
Next:  XML Column Advice for XML Newbie  
Author Message
Andy B

External


Since: Feb 13, 2008
Posts: 215



(Msg. 1) Posted: Fri Oct 17, 2008 7:47 am
Post subject: Repeatable xml elements in xml columns
Archived from groups: microsoft>public>sqlserver>xml (more info?)

If I have a document fragment that looks like this:

<Section ID= 1 Title="Section 1">
<Text>
This is section 1. Put your own text here.
</Text>
</Section>
<Section ID=2 Title="Section 2">
<Text>
This is section 2. Put your own text here.
</Text>
</Section>

And the count of the Section elements are not known (1 xml column could have
5 section elements and the other one could have 10), how do you access these
types of "repeating elements" with xpath? The other interesting question
would be how you turn this type of xml column into a view?

 >> Stay informed about: Repeatable xml elements in xml columns 
Back to top
Login to vote
Martin Honnen

External


Since: Apr 20, 2007
Posts: 48



(Msg. 2) Posted: Fri Oct 17, 2008 10:25 am
Post subject: Re: Repeatable xml elements in xml columns [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Andy B wrote:
> If I have a document fragment that looks like this:
>
> <Section ID= 1 Title="Section 1">
> <Text>
> This is section 1. Put your own text here.
> </Text>
> </Section>
> <Section ID=2 Title="Section 2">
> <Text>
> This is section 2. Put your own text here.
> </Text>
> </Section>
>
> And the count of the Section elements are not known (1 xml column could have
> 5 section elements and the other one could have 10), how do you access these
> types of "repeating elements" with xpath? The other interesting question
> would be how you turn this type of xml column into a view?

Well if you want to select all Section elements with XQuery/XPath then
Section
or
/Section
does that, it selects all those Section elements.
You could pass that to the XQuery nodes method as follows:

DECLARE @x XML;
SET @x = N'<Section ID="1" Title="Section 1">
<Text>
This is section 1. Put your own text here.
</Text>
</Section>
<Section ID="2" Title="Section 2">
<Text>
This is section 2. Put your own text here.
</Text>
</Section>';

SELECT
X.S.value('@ID', 'int') AS ID,
X.S.value('@Title', 'nvarchar(20)') AS Title,
X.S.value('Text[1]', 'nvarchar(200)') AS Text
FROM
@x.nodes('Section') AS X(S);

Result is

1 Section 1 This is section 1. Put your own text here.
2 Section 2 This is section 2. Put your own text here.


Or what problem do you exactly have to find an XPath expresssion?

--

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

 >> Stay informed about: Repeatable xml elements in xml columns 
Back to top
Login to vote
Andy B

External


Since: Feb 13, 2008
Posts: 215



(Msg. 3) Posted: Fri Oct 17, 2008 11:13 am
Post subject: Re: Repeatable xml elements in xml columns [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

That looks good so far. What I will end up needing to do eventually is
taking some section sets along with some other xml nodes and combining them
into a view to create a column view of the document. Here is a simple
example:

Take the sections I gave earlier and add some stuff to it like this:

<Document>
<Sections>
<Section ID=1 Title=Section 1">
<Text>
This is section 1. Put your own text here.
</Text>
<Section ID=2 Title="Section 2">
<Text>
This is section 2. Put your own text here.
</Text>
</Section>
<!--Put as many sections here as needed. The actual number of sections
veriess from document to document -->
</Sections>
<Event Title="Sample event" Date="2008-10-17T07:00:00Z" Duration="01:00:00">
<Location Name="Somewhere" Address="112 StreetName" City="ACity" State="MI"
ZipCode="44321">
</Event>
</Document>

How would you turn this into a view where each row represents this entire
document?


"Martin Honnen" wrote in message

> Andy B wrote:
>> If I have a document fragment that looks like this:
>>
>> <Section ID= 1 Title="Section 1">
>> <Text>
>> This is section 1. Put your own text here.
>> </Text>
>> </Section>
>> <Section ID=2 Title="Section 2">
>> <Text>
>> This is section 2. Put your own text here.
>> </Text>
>> </Section>
>>
>> And the count of the Section elements are not known (1 xml column could
>> have 5 section elements and the other one could have 10), how do you
>> access these types of "repeating elements" with xpath? The other
>> interesting question would be how you turn this type of xml column into a
>> view?
>
> Well if you want to select all Section elements with XQuery/XPath then
> Section
> or
> /Section
> does that, it selects all those Section elements.
> You could pass that to the XQuery nodes method as follows:
>
> DECLARE @x XML;
> SET @x = N'<Section ID="1" Title="Section 1">
> <Text>
> This is section 1. Put your own text here.
> </Text>
> </Section>
> <Section ID="2" Title="Section 2">
> <Text>
> This is section 2. Put your own text here.
> </Text>
> </Section>';
>
> SELECT
> X.S.value('@ID', 'int') AS ID,
> X.S.value('@Title', 'nvarchar(20)') AS Title,
> X.S.value('Text[1]', 'nvarchar(200)') AS Text
> FROM
> @x.nodes('Section') AS X(S);
>
> Result is
>
> 1 Section 1 This is section 1. Put your own text here.
> 2 Section 2 This is section 2. Put your own text here.
>
>
> Or what problem do you exactly have to find an XPath expresssion?
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
 >> Stay informed about: Repeatable xml elements in xml columns 
Back to top
Login to vote
Martin Honnen

External


Since: Apr 20, 2007
Posts: 48



(Msg. 4) Posted: Fri Oct 17, 2008 12:25 pm
Post subject: Re: Repeatable xml elements in xml columns [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Andy B wrote:

> Take the sections I gave earlier and add some stuff to it like this:
>
> <Document>
> <Sections>
> <Section ID=1 Title=Section 1">
> <Text>
> This is section 1. Put your own text here.
> </Text>
> <Section ID=2 Title="Section 2">
> <Text>
> This is section 2. Put your own text here.
> </Text>
> </Section>
> <!--Put as many sections here as needed. The actual number of sections
> veriess from document to document -->
> </Sections>
> <Event Title="Sample event" Date="2008-10-17T07:00:00Z" Duration="01:00:00">
> <Location Name="Somewhere" Address="112 StreetName" City="ACity" State="MI"
> ZipCode="44321">
> </Event>
> </Document>
>
> How would you turn this into a view where each row represents this entire
> document?

Sorry, I am not able to see the problem. If you want to create a view
you could just do
CREATE VIEW viewName
AS SELECT xmlColumnName FROM tableName;
for instance. That looks too simple to be what you are looking for but I
am currently not able to understand where you have problems creating a
view from a table with a column of type xml as for a view it does not
matter which type a column has.



--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
 >> Stay informed about: Repeatable xml elements in xml columns 
Back to top
Login to vote
Andy B

External


Since: Feb 13, 2008
Posts: 215



(Msg. 5) Posted: Fri Oct 17, 2008 2:10 pm
Post subject: Re: Repeatable xml elements in xml columns [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

This might sound really interesting, but Out of the second sample xml
document I posted (the one with the Event node) I need to create a view from
it. Here is an example I can give you so you can understand what I mean:

This view was created from an xml column in the base table:

create view [News].[NewsArticlesView]
as

SELECT ID, Article.value('/NewsArticle/@Title[1]', 'VARCHAR(100)') AS
Title, Article.value('/NewsArticle/@Date[1]', 'DateTime') AS Date,
Article.value('NewsArticle/Description[1]',
'VARCHAR(200)') AS Description, Article.value('NewsArticle/Body[1]',
'VARCHAR(max)') AS Body, LastModifiedFROM [News].[News];

My question and worry is about the Sections node. I need to know how to get
the section nodes in the Sections node to show up in the view in a single
row with the rest of the xml document.
"Martin Honnen" wrote in message

> Andy B wrote:
>
>> Take the sections I gave earlier and add some stuff to it like this:
>>
>> <Document>
>> <Sections>
>> <Section ID=1 Title=Section 1">
>> <Text>
>> This is section 1. Put your own text here.
>> </Text>
>> <Section ID=2 Title="Section 2">
>> <Text>
>> This is section 2. Put your own text here.
>> </Text>
>> </Section>
>> <!--Put as many sections here as needed. The actual number of sections
>> veriess from document to document -->
>> </Sections>
>> <Event Title="Sample event" Date="2008-10-17T07:00:00Z"
>> Duration="01:00:00">
>> <Location Name="Somewhere" Address="112 StreetName" City="ACity"
>> State="MI" ZipCode="44321">
>> </Event>
>> </Document>
>>
>> How would you turn this into a view where each row represents this entire
>> document?
>
> Sorry, I am not able to see the problem. If you want to create a view you
> could just do
> CREATE VIEW viewName
> AS SELECT xmlColumnName FROM tableName;
> for instance. That looks too simple to be what you are looking for but I
> am currently not able to understand where you have problems creating a
> view from a table with a column of type xml as for a view it does not
> matter which type a column has.
>
>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
 >> Stay informed about: Repeatable xml elements in xml columns 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
SQLXML Import with nested elements - I am new to SQLXML and have found the tutorial for importing using an XDR schema. The problem I am having is that my xml file has nested elements, and I cannot figure out how to get the process to pull them in. I have found examples using attributes,..

Invalid XML elements found inside sync block - I'm getting the above error when updating a SQL server table using a updategram, with a mapped schema. There is no $ symbol in the XML, nor do I have a timestamp column (two things that also appear to cause the error). I cannot see anything wrong with....

Selecting elements of derived types in SQL Server 2005 XQu.. - I have a schema that looks something like this (the names have been changed to protect the innocent): <?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:eliza="http://www.com" xmlns:xsd="http://www.w3.or...

cdata all columns automatically - I have an application that is extremely dynamic. Users can type in their own sql statements and run queries however they want. Is there a way using SQL to automaticall wrap all the columns with a CDATA notation? EX: Query SELECT field1,..

Compare 2 XML Columns - Hi all: I have a table with 2 XML type columns of the same structure. How do I compare these 2 columns and return the difference? For example, here are some sample data in the two xml columns: xmlcol1 xmlcol2 <r1> ...
   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 ]