Solved

Formatting the output using For XML Path

Posted on 2013-01-07
2
242 Views
Last Modified: 2013-01-08
Hi:

I've created the query below:
SELECT o.OrderID AS "Order/OrderID",
   o.OrderNumber AS "Order/OrderNumber",
   o.BillToFirstName AS "Order/BillToFirstName",
   o.BillToLastName AS "Order/BillToLastName",
   (Select p.Amount from ac_Payments p where p.OrderID = o.OrderID FOR XML PATH ('')) AS "Order/Payments/Amount"
FROM ac_Orders AS o
   INNER JOIN ac_OrderItems as oi
   ON o.OrderID = oi.OrderID
WHERE o.OrderID=104
FOR XML PATH ('O'), ROOT ('Orders'), ELEMENTS;

Open in new window


Works well, except for one issue.  The output of the result (below) seems to include the Amount tag twice, and the second instance shows the HTML code instead of the actual greater than, and less than signs.

How do I get rid of the second instance of the <amount> tag?

<Orders>
	<O>
		<Order>
			<OrderID>104</OrderID>
			<OrderNumber>104</OrderNumber>
			<BillToFirstName>Jean</BillToFirstName>
			<BillToLastName>Denny</BillToLastName>
				<Payments>
				<Amount>&lt;Amount&gt;39.4400&lt;/Amount&gt;</Amount>
				</Payments>
		</Order>
	</O>
<Orders>

Open in new window

0
Comment
Question by:W2Market
2 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
ID: 38752917
I don't claim to be an expert in XML and of course, I don't have your database to test this with but it would seem that this might work:

SELECT o.OrderID AS "Order/OrderID",
   o.OrderNumber AS "Order/OrderNumber",
   o.BillToFirstName AS "Order/BillToFirstName",
   o.BillToLastName AS "Order/BillToLastName",
   (Select p.Amount from ac_Payments p where p.OrderID = o.OrderID) AS "Order/Payments/Amount"
FROM ac_Orders AS o
   INNER JOIN ac_OrderItems as oi
   ON o.OrderID = oi.OrderID
WHERE o.OrderID=104
FOR XML PATH ('O'), ROOT ('Orders'), ELEMENTS;

Open in new window

                                 

You were already turning the Amount from ac_Payments into XML and then applying XML on top of already XML output.

Try that and let us know.
0
 

Author Closing Comment

by:W2Market
ID: 38755238
Perfect.  Thanks for your help!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql help 5 53
How do i Copy All the Tables from One Database to Another in SQL Server? 7 45
Not selecting duplicate data 6 53
Help  needed 3 23
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question