Home Forums Programming XML capturing multiple attributes from node RE: capturing multiple attributes from node

  • Hi,

    I would use the newer XML functions rather then the old OPENXML. Syntax wise you could probably do this in a couple of ways, but here is one that I have come up with (i've tweaked your example xml to make it valid)

    DECLARE @xml XML

    SET @xml = '<xml>

    <Document>

    <FileDetail>

    <BookNumber>12345</BookNumber>

    <BookAuthorLoop>

    <Author name="John Doe"/>

    <Author name="Jane Doe"/>

    </BookAuthorLoop>

    </FileDetail>

    </Document>

    </xml>'

    SELECT a.c.value('(BookNumber/text())[1]', 'int')

    , b.c.value('(@name)[1]', 'varchar(30)')

    FROM @xml.nodes('/xml/Document/FileDetail') AS a(c)

    CROSS APPLY a.c.nodes('BookAuthorLoop/Author') AS b(c)