question

ramesh.duppati avatar image
ramesh.duppati asked

How to read value from xml attribute value from xml?

Hi All I need your help for my below 2 queries. Please reply to me asap. Thanks.. ![alt text][1] [1]: /storage/temp/528-aa.jpeg
sqlxmlsqlxml
aa.jpeg (38.0 KiB)
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Valentino Vranken avatar image Valentino Vranken commented ·
Please post text instead of that image, we can't copy/paste from it...
0 Likes 0 ·
mjharper avatar image
mjharper answered
no problem - this should do the job... IF OBJECT_ID('tempdb..#demoData') IS NOT NULL DROP TABLE #demoData CREATE TABLE #demoData ( MyXmlData XML ) INSERT INTO #demoData ( MyXmlData ) VALUES ( '' );--insert xml data in here WITH XMLNAMESPACES (' http://xxx.com/' AS ns1) SELECT N.C.value('(@ColorId)[1]', 'varchar(20)') AS 'Color_id', N.C.value('ns1:font[1]', 'varchar(20)') AS font, N.C.value('ns1:style[1]', 'varchar(20)') AS style FROM #demoData AS dd CROSS APPLY MyXmlData.nodes('/ns1:Colors/ns1:Color') N(C)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

mjharper avatar image
mjharper answered
Hi For the first approach this query will work (you'll need to finish the preceding SQL statement with a semicolon): WITH XMLNAMESPACES (' http://xxx.com' AS ns1) SELECT a.b.value('(@ColorId)[1]', 'varchar(20)') AS 'Color_id' FROM @MyXML.nodes('/ns1:Colors/ns1:Color') a(b) For the second approach you can use a similar query: CREATE TABLE #demoData ( MyXmlData XML ) INSERT INTO #demoData ( MyXmlData ) VALUES ( '' ); --insert your xml here WITH XMLNAMESPACES (' http://xxx.com' AS ns1) SELECT N.C.value('(@ColorId)[1]', 'varchar(20)') AS 'Color_id' FROM #demoData AS dd CROSS APPLY MyXmlData.nodes('/ns1:Colors/ns1:Color') N(C)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ramesh.duppati avatar image
ramesh.duppati answered
Hi Harper, Really thanks for you reply and it is working for me. Also I would like to know for approach 2 that how to get directly colorid from XML database column. By using second approach my work will be easier.. HI Vranken, I try to copy code in text format, but I can 't see everything in post preview. sorry for that.
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

mjharper avatar image mjharper commented ·
Hi - I have update my original answer to include both approaches.
0 Likes 0 ·
ramesh.duppati avatar image
ramesh.duppati answered
Really appreciate your help here Haper. it works for me..
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

mjharper avatar image mjharper commented ·
Glad it works. If you can accept the answer then hopefully that helps future users solve the same or similar problem. Thanks.
0 Likes 0 ·
ramesh.duppati avatar image
ramesh.duppati answered
Hi Harper, Sorry to bug you again. But need one more small help here. In my above XML, for each Color tag I am having two more sub tags with information as bleow. <'Color colorid="aaaa" last_modified="date"> <'font>verdana<'/font> <'style>Italic<'/italic> <'/Color> I would like use 2nd approach to get data like below table format Column1 Column2 Column 3 aaaa verdana Italic bbb Calibri Bold
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ramesh.duppati avatar image
ramesh.duppati answered
Oh man, You rocks really. It works like charm.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.