User Guide

Table Of Contents
874 Chapter 35: Using XML and WDDX
<cfset orderquery = QueryNew("item_Id,
name, qty, unitPrice") >
<cfset temp = QueryAddRow(orderquery,
#numItems#)>
<cfloop index="i" from = "1" to = #numItems#>
<cfset temp = QuerySetCell(orderquery,
"item_Id", #mydoc.order.items.item[i].
XmlAttributes.id#, #i#)>
<cfset temp = QuerySetCell(orderquery,
"name", #mydoc.order.items.item[i].
name.XmlText#, #i#)>
<cfset temp = QuerySetCell(orderquery,
"qty", #mydoc.order.items.item[i].
quantity.XmlText#, #i#)>
<cfset temp = QuerySetCell(orderquery,
"unitPrice", #mydoc.order.items.item[i].
unitprice.XmlText#, #i#)>
</cfloop>
Converts the XML document object into a
query object.
Creates a query with columns for the
item_id, name, qty, and unitPrice values for
each item.
For each XML item entry in the
mydoc.order.items entry, fills one row of
the query with the item’s id attribute and
the text in the name, quantity, and unitprice
entries that the it contains.
<cfquery name="discountQuery"
datasource="cfdocexamples">
SELECT *
FROM employee
WHERE Emp_Id = #accountNum#
</cfquery>
<cfset drate = 0>
<cfif #discountQuery.RecordCount# is 1>
<cfset drate = 10>
</cfif>
If the account number is the same as an
employee ID in the cfdocexamples
database Employee table, the query
returns one record. and RecordCount
equals 1. In this case, sets a discount rate of
10%. Otherwise, sets a discount rate of
0%.
<cfquery name="priceQuery" dbType="query">
SELECT SUM(qty*unitPrice)
AS totalPrice
FROM orderquery
</cfquery>
<cfset discountPrice = priceQuery.totalPrice
* (1 - drate/100)>
Uses a query of queries with the SUM
operator to calculate the total cost before
discount of the ordered items, then applies
the discount to the price. The result of the
query is a single value, the total price.
<cfxml variable="receiptxml">
<receipt num = "34">
<cfoutput>
<price>#discountPrice#</price>
<cfif drate GT 0 >
<discountRate>#drate#</discountRate>
</cfif>
</cfoutput>
<itemsFilled>
<cfoutput query="orderQuery">
<name>#name# </name>
<qty> #qty# </qty>
<price> #qty*unitPrice# </price>
</cfoutput>
</itemsFilled>
</receipt>
</cfxml>
Creates an XML document object as a
receipt. The receipt has a root element
named receipt, which has the receipt
number as an attribute. The receipt
element contains a price element with the
order cost and an itemsFilled element with
one item element for each item.
Code Description