•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Posts: 76
Reputation:
Rep Power: 2
Solved Threads: 0
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>
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>
•
•
Join Date: Oct 2007
Posts: 76
Reputation:
Rep Power: 2
Solved Threads: 0
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:
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
xml Syntax (Toggle Plain Text)
<Comments> <Comment> <CommentID>133</CommentID> <ProductID>3926</ProductID> <CustomerID>8877</CustomerID> <Message>Heels on shoes wear out too quickly.</Message> <ResponseRequested>No</ResponseRequested> </Comment> <Comment> <CommentID>514</CommentID> <ProductID>3926</ProductID> <CustomerID>3227</CustomerID> <Message>Where can I find a supplier in San Jose?</Message> <ResponseRequested>Yes</ResponseRequested> </Comment> </comments>
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb XML, XSLT and XPATH Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- XML Management (RSS, Web Services and SOAP)
- Database Design : DB2 + PHP + Windows? (Database Design)
- Unable to uninstall DB2 Express-c (Win XP Home) (Windows NT / 2000 / XP / 2003)
- Complete guideline book for the DB2 (Shell Scripting)
- shell script manual for db2 (Shell Scripting)
- extracting db2 table records to csv (Shell Scripting)
- Simple question about using JDBC to access DB2 (Java)
- Immediate project for IBM DB2 UDB database administrator (Software Development Job Offers)
Other Threads in the XML, XSLT and XPATH Forum
- Previous Thread: Xpath
- Next Thread: SQL/XML Query


Linear Mode