You are here: BI Publisher Working with XPath in RTF Template

Working with XPath in RTF Template

Written by  Sridhar Sunchu
Rate this item
(10 votes)

X Path

  •   XPath is a syntax used for selecting parts of an XML document
  •   The way XPath describes paths to elements  is similar to the way an operating system describes paths to files
  •   XPath is almost a small programming language; it has functions, tests, and expressions
  •   XPath is a W3C standard
  •   XPath is not itself written as XML, but is used heavily in XSLT


Terminology

<INSTITUTE>

    <DEPARTMENT>

          <SECTION>

          </SECTION>

           <SECTION>

                <STUDENT>

                     <ROLL_NO> </ROLL_NO>

                      <MARKS>

                              <SUBJECT1></SUBJECT1>

                              <SUBJECT2></SUBJECT2>

                              <SUBJECT3></SUBJECT3>

                       </MARKS>

                </STUDENT>

          </SECTION>

      </DEPARTMENT>

</INSTITUTE>

 

  •   INSTITUTE is the parent of DEPARTMENT; DEPARTMENT is the parent of the two SECTIONS
  •   The two SECTIONS are the children of DEPARTMENT, and the STUDENT is the child of the second SECTION
  •   The two SECTIONS of the DEPARTMENT are siblings (they have the same parent)
  •   INSTITUTE, DEPARTMENT, and the second SECTION are the ancestors of the STUDENT
  •   The two SECTIONS, the STUDENT, and the ROLL_NO are the descendents of the DEPARTMENT

 

Paths

Operating system:

XPath:

/  = the root directory

/INSTITUTE  = the root element (if named INSTITUTE )

/users/dave/foo = the file named foo in dave in users

/INSTITUTE/DEPARTMENT/SECTION/STUDENT  = every STUDENT element in a SECTION in every DEPARTMENT in the INSTITUTE

foo  = the file named foo in the current directory

STUDENT  = every STUDENT element that is a child of the current element

. = the current directory

.  = the current element

.. = the parent directory

 .. = parent of the current element

/users/dave/* = all the files in /users/dave

/INSTITUTE/DEPARTMENT/SECTION/*  = all the elements in /INSTITUTE/DEPARTMENT/SECTION

 

Slashes

  •   A path that begins with a  /  represents an absolute path, starting from the top of the document

  Example:  /email/message/header/from

  Note that even an absolute path can select more than one element

  A slash by itself means “the whole document”

  •   A path that does not begin with a  represents a path starting from the current element

  Example:  header/from

  •   A path that begins with  //  can start from anywhere in the document

  Example:  //header/from selects every element from that is a child of an element header

  This can be expensive, since it involves searching the entire document

Brackets and last()

  •   A number in brackets selects a particular matching child (counting starts from 1, except in Internet Explorer)

  Example:  /institute/department[1] selects the first department of the institute

  Example:  //student/roll_no[2] selects the second roll_no of every student in the XML document

  Example: //department/section[1]/student[2]

  Only matching elements are counted; for example, if a department has both sections and student, the latter are ignored when counting student

  •    The function last() in brackets selects the last matching child

  Example: /institute/department/section[last()]

  • You can even do simple arithmetic

  Example: /institute/department/section[last()-1]

 

Stars

  •   A star, or asterisk, is a “wild card”--it means “all the elements at this level”

  Example: /institute/department/section/* selects every child of every section of every department in the institute

  Example: //department/* selects every child of every department (sections, students, etc.)

  Example: /*/*/*/student selects every student that has exactly three ancestors

  Example: //*  selects every element in the entire document

Arithmetic expressions

           +                             add

           -                              subtract

           *                             multiply

          div                          (not /) divide

           mod                       modulo (remainder)

 

     Equality tests

  =                      “equals”          (Notice it’s not  ==)

!=                     “not equals”

But it’s not that simple!

             value = node-set  will be true if the node-set contains any node with a value that matches value

             value != node-set  will be true if the node-set contains any node with a value that does not match value

Hence,

             value = node-set  and  value != node-set  may both be true at the same time!

     Other boolean operators

and                  (infix operator)

or                     (infix operator)

              Example: count = 0 or count = 1

not()                 (function)

The following are used for numerical comparisons only:

<            “less than”                                  Some places may require &lt;

<=          “less than or equal to”             Some places may require &lt;=

 >            “greater than”                             Some places may require &gt;

         >=          “greater than or equal to”        Some places may require &gt;=

 

Some XPath functions

  XPath contains a number of functions on node sets, numbers, and strings; here are a few of them:

  count(elem) counts the number of selected elements

  Example: //section[count(student)=1] selects section with exactly two students children


  name() returns the name of the element

  Example: //*[name()='student'] is the same as //student


  starts-with(arg1, arg2) tests if arg1 starts with arg2

  Example: //*[starts-with(name(), 'stu']


  contains(arg1, arg2) tests if arg1 contains arg2

  Example: //*[contains(name(), 'tud']

 Example:

Here is the example of an institute which has one department and a few students in it.

<INSTITUTE>

<DEPARTMENT>

     <DEPARTMENT_NAME>COMPUTER SCIENCE ENGINEERING</DEPARTMENT_NAME>

          <SECTION>

                <STUDENT>

                     <ROLL_NO>1201</ROLL_NO>

                      <MARKS>

                              <SUBJECT1>45</SUBJECT1>

                              <SUBJECT2>55</SUBJECT2>

                              <SUBJECT3>65</SUBJECT3>

                      </MARKS>

                </STUDENT>

                 <STUDENT>

                     <ROLL_NO>1205</ROLL_NO>

                      <MARKS>

                              <SUBJECT1>56</SUBJECT1>

                              <SUBJECT2>48</SUBJECT2>

                              <SUBJECT3>58</SUBJECT3>

                      </MARKS>

                </STUDENT>

                <STUDENT>

                     <ROLL_NO>1257</ROLL_NO>

                      <MARKS>

                              <SUBJECT1>51</SUBJECT1>

                              <SUBJECT2>49</SUBJECT2>

                              <SUBJECT3>53</SUBJECT3>

                      </MARKS>

                </STUDENT>

          </SECTION>

     </DEPARTMENT>

</INSTITUTE>

 

Now let’s display all the student details using a table with department name, roll number and marks.

1.JPG


Output:

 

2.JPG

 


Why is the department name missing?

When we say <?for-each:STUDENT?> the control is at <STUDENT> , (as you can see in the below diagram) but department name is not the child of student so we have to move the cursor to appropriate tag using xpath.

6.JPG

3.JPG


Output:


4.JPG


Here we used <?../../DEPARTMENT_NAME?> instead of <?DEPARTMENT_NAME?> so that the control jumps to two levels up in hierarchy and displays the DEPARTMENT NAME.

7.JPG


Now, let’s calculate totals marks secured by each student. To do so, add a column for Total and use <?sum(./MARKS/*)?> syntax to calculate TOTAL.

<?sum(./MARKS/*)?> syntax implies that we are using sum function and calculating all the children in marks

8.JPG

Output:

9.JPG

"If you found this article useful, please rate the same"

Read 17991 times

1 comment

Leave a comment

Make sure you enter the (*) required information where indicated.
Basic HTML code is allowed.