Next Question
Email to a friend |
RSS
No Best Answer Selected, Tip Refunded
Can this help:
http://www.mssqltips.com/tip.asp?tip=1609
Permalink | Report
This T-SQL will do what you asked for, but it would need to be modified if you have more than one element to work with. I couldn't tell from your example exactly what the schema for the incoming XML would be like.
DECLARE @xml XML
DECLARE @xml_new XML
DECLARE @idoc INT
-- your sample xml
SET @xml = ''
--create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @xml
-- USE OPENXML to SELECT attributes out of @xml, then use "FOR XML" to create new xml
SET @xml_new = (
SELECT Type, Term, DI, PO
FROM OPENXML (@idoc, '/RB/RI',1)
WITH (
Type VARCHAR(16)
,Term INT
,DI INT
,PO INT
)
FOR XML PATH('RI')
)
-- done with XML document
EXEC sp_xml_removedocument @idoc
Permalink | Report
Question
M$4
May 08, 2009 09:35 PM
XML transformation using OPENXML in SQL 2005?
I have a SQL 2005 xml variable, @xml, which I need to transform from this:
@xml = '< RB >< RI Type="Sausages" Term="12" DI="11" PO="0" />< PromoInfo />< /RB >'
...to this...
@xml = '< RB >< RI >< Type >Sausages< /Type >< Term >12 < /Term >< DI >11 < PO >0 < /PO >< /RI >< /RB >'
How can I do this using either OPENXML or some other means in SQL 2005? Basically I'm needing to "unbox" the attributes in the first @xml and create separate nodes out of each of them.
@xml = '< RB >< RI Type="Sausages" Term="12" DI="11" PO="0" />< PromoInfo />< /RB >'
...to this...
@xml = '< RB >< RI >< Type >Sausages< /Type >< Term >12 < /Term >< DI >11 < PO >0 < /PO >< /RI >< /RB >'
How can I do this using either OPENXML or some other means in SQL 2005? Basically I'm needing to "unbox" the attributes in the first @xml and create separate nodes out of each of them.
Interesting Question?
Yes (0)
No (0)
- In Programming |
- Tags: xml, attributes, nodes, transform, sql |
- |
- Report |
-
Share
RSS
No Best Answer Selected, Tip Refunded
Answers (2)
May 13, 2009 10:23 AM
Hi, Can this help:
http://www.mssqltips.com/tip.asp?tip=1609
Permalink | Report
June 26, 2009 08:10 PM
Hi, This T-SQL will do what you asked for, but it would need to be modified if you have more than one element to work with. I couldn't tell from your example exactly what the schema for the incoming XML would be like.
DECLARE @xml XML
DECLARE @xml_new XML
DECLARE @idoc INT
-- your sample xml
SET @xml = ''
--create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @xml
-- USE OPENXML to SELECT attributes out of @xml, then use "FOR XML" to create new xml
SET @xml_new = (
SELECT Type, Term, DI, PO
FROM OPENXML (@idoc, '/RB/RI',1)
WITH (
Type VARCHAR(16)
,Term INT
,DI INT
,PO INT
)
FOR XML PATH('RI')
)
-- done with XML document
EXEC sp_xml_removedocument @idoc
Permalink | Report
Answer this Question
Voted "No best answer": jellylala,
Related Questions
Any body can help me,Why can not I log in using your account Mahalo, but with faceboo...
How do I tell a close relative that her thirteen(13) years old daughter is ''wiz kid'...
How do you rate Wordpress or Blogger?
How many years do you use the same motif, ornaments and decor scheme for Christmas, o...
How do I tell a close relative that her thirteen(13) years old daughter is ''wiz kid'...
How do you rate Wordpress or Blogger?
How many years do you use the same motif, ornaments and decor scheme for Christmas, o...
Ask a Question
Buy Mahalo Dollars with Credit Card or PayPal
Top Members
Most Popular Tags
Categories
- Anonymous
- Arts & Design
- Beauty & Style
- Books & Authors
- Business
- Cars & Transportation
- Consumer Electronics
- Coupons Deals
- Education
- Entertainment
- Environment
- Fitness
- Food & Drink
- From Email
- From Iphone
- From Twitter
- Health
- History
- Hobbies
- Home & Garden
- How Tos
- Humor
- Jobs
- Legal
- Local
- Love & Relationships
- Mahalo Answers Community
- Money
- Music
- News
- NSFW
- Parenting
- Pets
- Science & Mathematics
- Services
- Shopping
- Social Science
- Society & Culture
- Sports
- Technology & Internet
- Travel
- Video Games
Welcome New Members
- candi, November 29, 2009 01:55 AM
- rosesteidle, November 29, 2009 01:55 AM
- tracydnn, November 29, 2009 01:53 AM
- chelseakissinge..., November 29, 2009 01:52 AM
- zhuzhupets, November 29, 2009 01:39 AM
Mahalo Dollars are the currency of Mahalo Answers.
Each Mahalo Dollar costs $1.
Once you earn more than 40 Mahalo Dollars, you can request to be paid via PayPal. Each Mahalo Dollar is currently worth $0.75 when paid out via PayPal. Learn More