RESTful services, XPath

References for subject 8

Programming:

XPath:

RESTful services

Prepare your environment in the same way as one week ago (Eclipse, Wildfly).

The updated workspace is here XML08-WS.zip. You can also simply add the projects REST directly from the zip to your old workspace.

If the REST project does not compile, you have to add your server runtime to the build path. One of possible solutions is to set "Targeted Runtimes" in the project settings.

Start the server and run the project REST (e.g. Servers / Add and Remove). Test for example hello?who=Me.

Task 1. PathParam

Go to class Kalkulator and test it using a web browser. Then add more arithmetic operations accessible in the same way as addition.

Task 2. XPath support

Go to class XPathService, test it (using web browser).

Add new parameter xpath and make the method return the result of the given expression (it should evaluate to a single node). If the parameter is not given (or empty), return the whole document, as it is now.

All right, there are some technical issues. If you don't know how to manage them, here is an example solution, or here a simpler one, returning a String.

Test the service. Note that slash signs / must be replaced with %2F in URLs.

XPath

If there is some time left, we can start writing XPath expressions.

Go to XPath project in the Eclipse workspace and change the perspective to XML. You should see the "XPath" window in the bottom-left corner.

Open the file staff.xml and type as "XPath expression" //department[2].

Task 3.

Write some XPath queries for the staff.xml example. Select (if possible):

  1. The third employee (person) from the second department.
  2. The third employees from all departments.
  3. The twentieth employee overall.
  4. The employees, who have exactly three phone numbers defined (count is the appropriate function).
  5. The employee with the highest salary.
  6. The employees who have the same first name as some other employee.
  7. The employees who have unique first names.

Task 4.

Dodatkowe zadanko po polsku na sztucznym przykładzie. Za pomocą XPath wybierz określone elementy dokumentu przyklad.xml (lub wyraź określone konstrukcje nawet jeżeli takich elementów nie ma :)).

  1. wszystkie obiekty,
  2. zagnieżdżone opisy (tzn. znajdujące się w innych opisach),
  3. obiekt o id równym E4,
  4. obiekty z opisem,
  5. obiekty bez opisu,
  6. opisy w obiektach bez nazwy,
  7. tylko drugie opisy obiektów,
  8. obiekty parzyste,
  9. dodatki znajdujące się za opisami,
  10. dodatki znajdujące się bezpośrednio za opisami,
  11. obiekty, których wszystkie dzieci posiadają podelement wyr,
  12. sekwencja: dla każdego obiektu, liczba jego dzieci.

Valid XHTML 1.1Valid CSS