Home Forums SQL Server 2005 Development How to get the inner XML from an xml element using XQuery RE: How to get the inner XML from an xml element using XQuery

  • Will this suffice?

    DECLARE @XML XML = '<root><Books><Book><ID>2</ID><Name>Sql Server Admin</Name>

    <Author><Name>Joseph</Name><Age>35</Age></Author></Book></Books></root>' ;

    SELECT @XML.query('/root/Books/Book/Author') ;

    If not, you'll need to query the values within the Author node, and then recompose it into XML with a FOR XML clause.

    DECLARE @XML XML = '<root><Books><Book><ID>2</ID><Name>Sql Server Admin</Name>

    <Author><Name>Joseph</Name><Age>35</Age></Author></Book></Books></root>' ;

    SELECT

    @XML.value('(/root/Books/Book/Author/Name/text())[1]','varchar(100)') AS Name,

    @XML.value('(/root/Books/Book/Author/Age/text())[1]','varchar(100)') AS Age

    FOR XML PATH('') ;

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon