SimpleExpressions.jws Sample

This topic inludes the source code for the SimpleExpressions.jws Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/WebServices/xmlBeans/xquery/

Sample Source Code


001 package xmlBeans.xquery; 
002 
003 import com.bea.xml.XmlCursor;
004 import com.bea.xml.XmlObject;
005 
006 /**
007  * This web service illustrates XQuery by showing a few simple expressions in use.
008  * XQuery offers a variety of ways to operate on XML. It support simple path 
009  * expressions that are similar to XPath, as well as more complex expressions for
010  * looping through XML.
011  
012  * All of the XQuery expressions in the source code use the special word "$this".
013  * This is not an XQuery word, but for XQuery expressions in WebLogic Workshop it
014  * signifies "the current context in the XML instance document". Contrast this with
015  * XQuery maps, which use $input to indicate "the start of the XML instance document".
016  
017  * Also, notice that you execute an XQuery using an XmlCursor, and that you
018  * should call the cursor's dispose method when you are finished with it. This
019  * ensures that resources used by the cursor will be properly disposed of.
020  
021  * For more information about XQuery, see http://www.w3.org/TR/xquery/.
022  
023  * @common:target-namespace namespace="http://workshop.bea.com/SimpleExpressions"
024  */
025 public class SimpleExpressions implements com.bea.jws.WebService
026 
027 
028     /*
029      * Declare a namespace for use with XQuery expressions. The syntax shown
030      * in this string is the XQuery way to declare a particular namespace URI and 
031      * the prefix associated with it. The namespace declaration is needed because
032      * the XML received by this web service is namespace-qualified. In order for 
033      * queries to work, that namespace must be taken into account. 
034      */    
035     final static String m_namespaceDeclaration = 
036         "declare namespace xq='http://openuri.org/bea/samples/workshop/xmlBeans/xquery'";
037 
038     /**
039      * Test this method by copying the entire contents of the Employees.xml file and 
040      * pasting them in place of the <AnyElement/> element. Click the 
041      * selectByState button to execute the following expression, which selects 
042      * any employee elements whose state child element has a value of "WA":
043      <br/><br/>
044      
045      * &nbsp;$this/xq:employees/employee[address/state='WA']
046      
047      <br/><br/>
048      
049      * The portion of this expression in square brackets -- [address/state='WA'] -- 
050      * is known as a "predicate." The predicate filters items returned by the 
051      * expression to its immediate left. In other words, the first part of this 
052      * expression returns all of the employee elements, then the predicate 
053      * further filters those values.
054      
055      * @common:operation
056      */
057     public XmlObject selectByState(XmlObject empDoc)
058     {
059         /* 
060          * Insert a cursor with which to execute the XQuery expression. Advance
061          * the cursor to the first element in the document. 
062          */
063         XmlCursor empCursor = empDoc.newCursor();        
064         XmlObject resultXML = null;
065         
066         /* A variable for the XQuery path expression. */
067         String queryExpression = "$this/xq:employees/xq:employee[xq:address/xq:state='WA']";
068 
069         try{
070             /* 
071              * Perform the query, combining the namespace declaration and the rest of query 
072              * expression.
073              */
074             XmlCursor resultCursor = empCursor.execQuery(m_namespaceDeclaration + queryExpression);
075             
076             /* Get an XmlObject instance containing the results of the query. */
077             resultXML = resultCursor.getObject();
078 
079             /* Dispose of the result cursor. */
080             resultCursor.dispose();
081 
082         }catch(Exception e){
083             System.out.println(e.getLocalizedMessage());
084         }
085         /* Dispose of the document cursor. */
086         empCursor.dispose();
087         return resultXML;
088     }
089 
090     /**
091      * Test this method by copying the entire contents of the Employees.xml file and 
092      * pasting them in place of the &lt;AnyElement/&gt; element. Click the 
093      * selectWorkPhonesAttr button to execute the following expression, which selects any 
094      * phone elements whose location attribute value is "work":
095      <br/><br/>
096      
097      * &nbsp;$this/xq:employees/employee/phone[@location='work']
098      
099      <br/><br/>
100      
101      * As with the selectByState method, this method
102      * uses a path expression and predicate to specify the requested XML.
103      
104      * @common:operation
105      */
106     public XmlObject selectWorkPhonesAttr(XmlObject empDoc)
107     {
108         XmlCursor empCursor = empDoc.newCursor();        
109         XmlObject resultXML = null;
110         
111         String queryExpression = "$this/xq:employees/xq:employee/xq:phone[@location='work']";
112 
113         try{
114             XmlCursor resultCursor = empCursor.execQuery(m_namespaceDeclaration + queryExpression);
115             resultXML = resultCursor.getObject();           
116             resultCursor.dispose();
117         }catch(Exception e){
118             System.out.println(e.getLocalizedMessage());
119         }
120         // Dispose of the query cursor.
121         empCursor.dispose();
122         return resultXML;
123     }
124 
125     /**
126      * Test this method by copying the entire contents of the Employees.xml file and 
127      * pasting them in place of the &lt;AnyElement/&gt; element. Click the 
128      * selectEmpsByStateFLWR button to execute the following expression, which selects any 
129      * employee elements whose location state element value is "WA":
130      <br/><br/>
131      
132      * &nbsp;for $e in $this/xq:employees/employee<br/>
133      * &nbsp;&nbsp;let $s := $e/address/state<br/>
134      * &nbsp;&nbsp;where $s = 'WA'<br/>
135      * &nbsp;&nbsp;return $e<br/>
136      <br/><br/>
137      
138      * This method's expression illustrates a FLWR (pronounced "flower") expression.
139      * The letters in the acronym stand for "for... let... where... return". As you
140      * can probably imagine, a FLWR expression is a way to loop through XML, 
141      * binding variables and evaluating based on the variables. This is very similar
142      * to loops in Java.
143      
144      * @common:operation
145      */
146     public XmlObject selectEmpsByStateFLWR(XmlObject empDoc)
147     {
148         XmlCursor empCursor = empDoc.newCursor();        
149         empCursor.toNextSibling();
150 
151         XmlObject resultXML = null;
152 
153         /*
154          * This expression loops through the employee elements, querying for the
155          * state elements whose value is "WA", then returning the results.
156          */        
157         String queryExpression = 
158             "for $e in $this/xq:employees/xq:employee " +
159                 "let $s := $e/xq:address/xq:state " +
160                 "where $s = 'WA' " +
161                 "return $e";
162         try{
163             XmlCursor resultCursor = empCursor.execQuery(m_namespaceDeclaration + queryExpression);
164             resultXML = resultCursor.getObject();
165             resultCursor.dispose();
166         }catch(Exception e){
167             System.out.println(e.getLocalizedMessage());
168         }
169         empCursor.dispose();
170         return resultXML;
171     }
172 
173 
174     /**
175      * Test this method by copying the entire contents of the Employees.xml file and 
176      * pasting them in place of the &lt;AnyElement/&gt; element. Click the 
177      * selectZipsNewDoc button to execute the following expression, which creates a
178      * zip-list XML document that contains a list of zip element values:
179      <br/><br/>
180      
181      * &nbsp;let $e := $this/xq:employees<br/>
182      * &nbsp;&nbsp;return<br/>
183      * &nbsp;&nbsp;&lt;zip-list&gt;<br/>
184      * &nbsp;&nbsp;&nbsp;{for $z in $e/employee/address/zip<br/>
185      * &nbsp;&nbsp;&nbsp;&nbsp;return $z}<br/>
186      * &nbsp;&nbsp;&lt;/zip-list&gt;<br/>
187      <br/><br/>
188      
189      * This method illustrates what is known as an "element constructor."
190      * That constructor in this case is the presence of the zip-list element
191      * in the expression, along with that element's contents.
192      * An element constructor is useful for creating XML of a different 
193      * shape from the results of your query expression. In an element
194      * constructor, the expression contains XML tags that act as a kind
195      * of template to hold query results.
196      <br/><br/>
197      
198      * Here, the expression creates the zip-list element and inserts into
199      * it the results of a for loop that returns zip elements in the XML.
200      * The expression then returns the entire new element with zip-list
201      * as its root.
202      
203      * @common:operation
204      */
205     public XmlObject selectZipsNewDoc(XmlObject empDoc)
206     {
207         XmlCursor empCursor = empDoc.newCursor();        
208         empCursor.toNextSibling();
209 
210         XmlObject resultXML = null;
211 
212         String queryExpression = 
213             "let $e := $this/xq:employees " +
214             "return " +
215                 "<zip-list> " +
216                     "{for $z in $e/xq:employee/xq:address/xq:zip " +
217                     "return $z} " +
218                 "</zip-list>";
219         try{
220             XmlCursor resultCursor = empCursor.execQuery(m_namespaceDeclaration + queryExpression);
221             resultXML = resultCursor.getObject();
222             resultCursor.dispose();
223         }catch(Exception e){
224             System.out.println(e.getLocalizedMessage());
225         }
226         empCursor.dispose();
227         return resultXML;
228     }
229