Home All Groups Group Topic Archive Search About

XML Shreading problem

Author
31 May 2009 3:25 PM
RJ

Just when I thought I had a handle on this.  Given the XML file below, I am
trying to shread and obtain all FileName nodes in UpgradeScriptFiles and
RollBackScriptFiles in DBDeployment

In each case I only get the first node i.e.
SQL_510_20090520_Test1.sql and RBK_510_20090520_Test1.sql respectively

What am I missing?

Thanks for any light that can be shead!

RJ



Declare    @DeploymentControlXML XML

<<<code that sets @DeploymentControlXML>>>

Select    x.Item.value('FileName[1]', 'VARCHAR(100)')
FROM    @DeploymentControlXML.nodes('//DBDeployment/UpgradeScriptFiles') As
x(item)

--Insert    @DeploymentRollBackFiles (ScriptFileName)
Select    x.Item.value('FileName[1]', 'VARCHAR(100)')
FROM    @DeploymentControlXML.nodes('//DBDeployment/RollBackScriptFiles') As
x(item)

<DBDeployment>
    <Version>5.0.2</Version>
    <BuildId>1357</BuildId>
    <BuildDate>2009-05-29</BuildDate>
    <BaseVersion>5.0.0</BaseVersion>
    <UpgradeScriptFiles>
        <FileName>SQL_510_20090520_Test1.sql</FileName>
        <FileName>SQL_510_20090520_Test2.sql</FileName>
        <FileName>SQL_510_20090520_Test3.sql</FileName>
        <FileName>SQL_510_20090520_Test4.sql</FileName>
    </UpgradeScriptFiles>
    <RollBackScriptFiles>
        <FileName>RBK_510_20090520_Test1.sql</FileName>
        <FileName>RBK_510_20090520_Test2.sql</FileName>      
    </RollBackScriptFiles>
</DBDeployment>
Author
31 May 2009 4:02 PM
Martin Honnen
RJ wrote:

> In each case I only get the first node i.e.
> SQL_510_20090520_Test1.sql and RBK_510_20090520_Test1.sql respectively
>
> What am I missing?

You need to select those FileName elements e.g.

DECLARE @DeploymentControlXML XML;

SET @DeploymentControlXML = N'<DBDeployment>
    <Version>5.0.2</Version>
    <BuildId>1357</BuildId>
    <BuildDate>2009-05-29</BuildDate>
    <BaseVersion>5.0.0</BaseVersion>
    <UpgradeScriptFiles>
        <FileName>SQL_510_20090520_Test1.sql</FileName>
        <FileName>SQL_510_20090520_Test2.sql</FileName>
        <FileName>SQL_510_20090520_Test3.sql</FileName>
        <FileName>SQL_510_20090520_Test4.sql</FileName>
    </UpgradeScriptFiles>
    <RollBackScriptFiles>
        <FileName>RBK_510_20090520_Test1.sql</FileName>
        <FileName>RBK_510_20090520_Test2.sql</FileName>   
    </RollBackScriptFiles>
</DBDeployment>';


SELECT
   x.item.value('.', 'NVARCHAR(100)') AS FileName
FROM

@DeploymentControlXML.nodes('/DBDeployment/RollBackScriptFiles/FileName')
AS x(item);





--

    Martin Honnen --- MVP XML
    http://msmvps.com/blogs/martin_honnen/
Are all your drivers up to date? click for free checkup

Author
31 May 2009 4:33 PM
RJ
Your solution worked and I thank you for that.  Still not sure of the
difference in methodologies though.  I'll play around with it work it out.

Thanks again.





Show quoteHide quote
"Martin Honnen" wrote:

> RJ wrote:
>
> > In each case I only get the first node i.e.
> > SQL_510_20090520_Test1.sql and RBK_510_20090520_Test1.sql respectively
> >
> > What am I missing?
>
> You need to select those FileName elements e.g.
>
> DECLARE @DeploymentControlXML XML;
>
> SET @DeploymentControlXML = N'<DBDeployment>
>     <Version>5.0.2</Version>
>     <BuildId>1357</BuildId>
>     <BuildDate>2009-05-29</BuildDate>
>     <BaseVersion>5.0.0</BaseVersion>
>     <UpgradeScriptFiles>
>         <FileName>SQL_510_20090520_Test1.sql</FileName>
>         <FileName>SQL_510_20090520_Test2.sql</FileName>
>         <FileName>SQL_510_20090520_Test3.sql</FileName>
>         <FileName>SQL_510_20090520_Test4.sql</FileName>
>     </UpgradeScriptFiles>
>     <RollBackScriptFiles>
>         <FileName>RBK_510_20090520_Test1.sql</FileName>
>         <FileName>RBK_510_20090520_Test2.sql</FileName>   
>     </RollBackScriptFiles>
> </DBDeployment>';
>
>
> SELECT
>    x.item.value('.', 'NVARCHAR(100)') AS FileName
> FROM

> @DeploymentControlXML.nodes('/DBDeployment/RollBackScriptFiles/FileName')
> AS x(item);
>
>
>
>
>
> --
>
>     Martin Honnen --- MVP XML
>     http://msmvps.com/blogs/martin_honnen/
>
Author
31 May 2009 4:39 PM
Martin Honnen
RJ wrote:
> Your solution worked and I thank you for that.  Still not sure of the
> difference in methodologies though. 

The XPath expression you pass to the nodes method determines which nodes
are mapped to rows. You passed in
   //DBDeployment/RollBackScriptFiles
so you got one row for each RollBackScriptFiles element and as there is
only one such element you got one row.

My suggestion


>> @DeploymentControlXML.nodes('/DBDeployment/RollBackScriptFiles/FileName')
>> AS x(item);

uses
   /DBDeployment/RollBackScriptFiles/FileName
so you get one row for each FileName element resulting in two rows with
the sample XML you have.



--

    Martin Honnen --- MVP XML
    http://msmvps.com/blogs/martin_honnen/
Author
31 May 2009 7:13 PM
RJ
Thanks for the clarification; that helped!

Show quoteHide quote
"Martin Honnen" wrote:

> RJ wrote:
> > Your solution worked and I thank you for that.  Still not sure of the
> > difference in methodologies though. 
>
> The XPath expression you pass to the nodes method determines which nodes
> are mapped to rows. You passed in
>    //DBDeployment/RollBackScriptFiles
> so you got one row for each RollBackScriptFiles element and as there is
> only one such element you got one row.
>
> My suggestion
>
>
> >> @DeploymentControlXML.nodes('/DBDeployment/RollBackScriptFiles/FileName')
> >> AS x(item);
>
> uses
>    /DBDeployment/RollBackScriptFiles/FileName
> so you get one row for each FileName element resulting in two rows with
> the sample XML you have.
>
>
>
> --
>
>     Martin Honnen --- MVP XML
>     http://msmvps.com/blogs/martin_honnen/
>

Bookmark and Share