Browse DevX
Sign up for e-mail newsletters from DevX


Five Practical XQuery Applications : Page 4

XQuery is still a relatively new and unfamiliar XML-based language, but it offers many possibilities for simplifying tasks that today are difficult or tedious. These five practical scenarios should give you ideas for leveraging XQuery in your own applications.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Application 3: Stock Analysis Automation
In the financial world, news is a business tool. If you can find a way to compose news stories automatically from wire sources you can save staff time. In addition, building stories targeted to a specific user or client can increase customer satisfaction. XQuery is well suited to both tasks, because it lets you perform targeted full text search even across structured data sources such as stock prices, and can compose the results into a custom format.

Suppose a trader wants to be alerted of specific types of news for a company he's following. He wants to see the effect that positive earnings from a NASDAQ bellwether has on some smaller cap stocks he's tracking. The task involves more than just popping up all AP and Reuter's stories with a stock list; he wants to test for a combination of conditions.

Reliant Corp maintains a news service through which traders can get custom news reports. Reliant uses XML extensively to hold information about exchange-listed companies as well as daily news stories. Traders maintain lists of companies in their portfolio. The trader portal needs to execute a search of daily news for stories that mention traders' portfolio companies in the headlines, and that also mention any of that company's partners within a single paragraph in the story content. You can write an XQuery application that fulfills all these needs.

First, create a simple user-defined function to retrieve all partners of a given company.

define function getPartners($company as xs:string) as element* { let $c := document("company/company.xml") //company[name = $company] return $c//partner }

Next, you need to find news items where a specific trader's portfolio company appears in the headline, and one or more of its partners are mentioned within a single paragraph in the document body. The result should list each news item by its headline and date. The trader is identified by a user id passed to the query shown below as the parameter $user. The portions of the query below in boldface text apply the required constraints.

<trader_portal trader_name="{$user}"> <news_alerts> { for $companyName in document("UserProfiles")//Profile [userId=$user]//company return <news_by_company name="{$companyName}"> { let $foobar_partners := getPartners($companyName) for $item in document("News/companyNews.xml") //NewsItem where some $line in $item//NewsLines/HeadLine satisfies (contains($line, $companyName)) and (some $para in $item//ContentItem/DataContent//p satisfies (some $partner in $foobar_partners satisfies contains($para, $partner)) ) return <news_item> {$item//NewsLines/HeadLine} {$item//NewsLines/DateLine} </news_item> } </news_by_company> } </news_alerts> ...... ...... </trader_portal>

The query is specific to each trader because it retrieves only news items that contain company names that also appear in that trader's user profile, which the query retrieves via the passed-in $user value. The where condition restricts the output to news items where the headline contains a company name, and where the data content (the body of the news item) contains the name of one or more partner companies. Here's a sample result:

<trader_portal trader_name="wbuffet"> <news_alerts> <news_by_company name="KO"> <news_item> <HeadLine>KO knocks out Pepsi Co. in Asia</HeadLine> <DateLine>04-01-2002Z05:10:00</DateLine> </news_item> <news_item> <HeadLine>KO releases earning results for Q1 2003</HeadLine> <DateLine>04-01-2002Z07:30:00</DateLine> </news_item> </news_by_company> <news_by_company name="WMT"> <news_item> <HeadLine>WMT to acquire Ben and Jerry</HeadLine> <DateLine>04-01-2002Z08:10:00</DateLine> </news_item> ...... ...... </news_by_company> </news_alerts> ...... ...... </trader_portal>

The results clearly show that the returned data is specific to one trader and meets the query restrictions.

Thanks for your registration, follow us on our social networks to keep up-to-date