User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the XML, XSLT and XPATH section within the Software Development category of DaniWeb, a massive community of 455,968 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,739 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our XML, XSLT and XPATH advertiser: Programming Forums
Views: 4617 | Replies: 32
Reply
Join Date: Oct 2007
Posts: 76
Reputation: dilasing is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dilasing dilasing is offline Offline
Junior Poster in Training

Re: Xquery in DB2

  #31  
Feb 14th, 2008
For this you can use XQuery. A powerful aspect of XQuery is its ability to transform XML output from one form of XML into another. For example, you can use XQuery to retrieve all or part of your stored XML documents and convert the output into HTML for easy display in a Web browser.

Consider below mentioned query which retrieves the addresses of our clients, sorts the results by zip code, and converts the output into XML elements that are part of an unordered HTML list:
xquery
<ul> {
for $y in db2-fn:xmlcolumn('CLIENTS.CONTACTINFO')/Client/Address
order by $y/zip
return <li>{$y}</li>
} </ul>

The query begins simply enough with the xquery keyword to indicate to the DB2 parser that XQuery is being used as the primary language. The second line causes the HTML markup for an unordered list (<ul>) to be included in the results. It also introduces a curly bracket,Curly brackets instruct DB2 to evaluate and process the enclosed expression rather than treat it as a literal string.

The output will appear similar to:
<ul>
<li>
<Address>
<street>9407 Los Gatos Blvd.</street>
<city>Los Gatos</city>
<state>CA</state>
<zip>95032</zip>
</Address>
</li>
<li>
<Address>
<street>4209 El Camino Real</street>
<city>Mountain View</city>
<state>CA</state>
<zip>95033</zip>
</Address>
</li>
. . .
</ul>
Reply With Quote  
Join Date: Oct 2007
Posts: 31
Reputation: ssahil11 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
ssahil11 ssahil11 is offline Offline
Light Poster

Re: Xquery in DB2

  #32  
Mar 10th, 2008
Can we use conditional expressions in XQuery?
Reply With Quote  
Join Date: Oct 2007
Posts: 76
Reputation: dilasing is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dilasing dilasing is offline Offline
Junior Poster in Training

Re: Xquery in DB2

  #33  
Mar 17th, 2008
Yes, we can use conditional expressions in XQuery. XQuery's ability to transform XML output can be combined with its built-in support for conditional logic to reduce the complexity of application code. We can look at a simple example. The "items" table includes an XML column containing comments customers have made about products. The comments xml document can be like following document:
  1. <Comments>
  2. <Comment>
  3. <CommentID>133</CommentID>
  4. <ProductID>3926</ProductID>
  5. <CustomerID>8877</CustomerID>
  6. <Message>Heels on shoes wear out too quickly.</Message>
  7. <ResponseRequested>No</ResponseRequested>
  8. </Comment>
  9. <Comment>
  10. <CommentID>514</CommentID>
  11. <ProductID>3926</ProductID>
  12. <CustomerID>3227</CustomerID>
  13. <Message>Where can I find a supplier in San Jose?</Message>
  14. <ResponseRequested>Yes</ResponseRequested>
  15. </Comment>
  16.  
  17. </comments>
For customers who have requested a response to their comments, you may want to create new "action" elements containing the product ID, customer ID, and message so you can route this information to the appropriate person for handling. For Comments that don't require a response create an "info" element with just the product ID and message.







This is how you can use an XQuery if-then-else expression to accomplish this task:

xquery
for $y in db2-fn:xmlcolumn('ITEMS.COMMENTS')/Comments/Comment 
return ( 
	if ($y/ResponseRequested = 'Yes') 
		then <action>
			{$y/ProductID, 
			 $y/CustomerID, 
			 $y/Message}
		      </action>
		else ( <info>
			{$y/ProductID, 
			 $y/Message}
			</info>
		)
)
Last edited by peter_budo : Mar 17th, 2008 at 10:40 pm. Reason: Keep It Organized - please use [code] tags
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb XML, XSLT and XPATH Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the XML, XSLT and XPATH Forum

All times are GMT -4. The time now is 9:09 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC