XPath Operators

Here are the operators we use in xslt for various calculations

I have symbolized the numbers by e1, e2,..

Operator Description
e1 + e2 If e1 and e2 are numbers, their sum
e1 – e2 e1 minus e2
e1 * e2 Product of e1 and e2
e1 div e2 If e1 and e2 are numbers, their quotient as a fl oating-point value.
e1 mod e2 The fl oating-point remainder of e1 divided by e2 .
e1 = e2 Tests to see if e1 equals e2
e1 & lt ; e2 Tests to see if e1 is less than e2 . You can’t say e1 < e2 inside an attribute: the less-than sign must be escaped as “& lt; ” .
e1 & lt ;= e2 Tests to see if e1 is less than or equal to e2
e1 & gt ; e2 Tests for greater-than
e1 != e2 Tests for inequality
e1 and e2 True if both e1 and e2 are true. If e1 is false, e2 is not evaluated
e1 or e2 True if either e1 or e2 is true. If e1 is true, e2 is not evaluated
e1 / e2 The / operator separates levels in a tree. For example, “/barge/load” selects all children of the element child of the document node
//e Abbreviation for descendant-or-self:: e
./e Abbreviation for self:: e
../e Abbreviation for parent:: e
@e Abbreviation for attribute:: e
e1 | e2 Selects the union of nodes that match e1 and those that match e2
* A wild-card operator; matches all nodes of the proper type for the context. For example, “*” selects all child elements of the context node, and “feet/@*” selects all attributes of the context node’s children
e1 [ e2 ] Square brackets enclose a predicate , which speci fi es an expression e2 that selects nodes from a larger set e1 .For example, in the XPath expression “para[@class=’note’]” , the para e1 [ e2 ] selects all children of the context node, and then the predicate selects only the children that have an attribute class=”note”. Another example: “item[1]” would select the first child of the context node
$e The dollar sign indicates that the following name is a variable name. For example, in an XSLT script, if variable n is set to 357, is expanded to the string “357” .
Advertisement

XPath String Functions

Below are some of the common string functions used in XSLT

1) format-number() – Converts a number into a string

2) starts-with(string1, string2) – Returns true if the first string starts with the second string.

3) contains(string1, string2) – Returns true if the first string contains the second string.

4) normalize-space(string) – Trims the leading and trailing space from string. Also replaces consecutive occurrences of white space with a single space.

5) translate(string1, string2, string3) – Returns string1 after any matching characters in string2 have been replaced by the characters in string3.

6) concat(string1, string2, ) – Concatenates all strings (i.e. joins them together).

Some more functions —

7) current() – Returns the current node

8) document() – Used to access the nodes in an external XML document

9) element-available() – Tests whether the element specified is supported by the XSLT processor

10) function-available() – Tests whether the function specified is supported by the XSLT processor

11) generate-id() – Returns a string value that uniquely identifies a specified node

12) key() – Returns a node-set using the index specified by an element

Substring function in xslt

Substring function returns the first argument starting at the position specified in the second argument and the length specified in the third argument.
string substring(string, number, number)

Example – 

The following function call returns “456”:
substring(“34567”,2,3)
The following function call returns “234”:
substring(“1234”,2)
The following function returns “sheet”:
substring(“harsheet”,4)

Substring-after

substring-after(string,string)
The substring-after function returns the substring of the first argument string that follows the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string.
The following function call returns “99/04/01”:
substring-after("1999/04/01","19")

Substring-before

substring-before(string, string)

Returns the substring of the first argument string that precedes the first occurrence of the second argument string in the first argument string, or the empty string if the first argument string does not contain the second argument string.

The following function call returns “1999”:

substring-before("1999/04/01","/") 

Elements in XSLT

XSLT is all about selecting one or more nodes from XML document and transforming or replacing its content with something else. A node could be any of the following: elements, attributes, text, namespaces, processing-instructions, and comments.
The <xsl:template> element is to select a node from XML document and transform its contents.

To select an element, you use the match attribute. To transform its contents, you simply place the new content between the opening (<xsl:template>) and closing (</xsl:template>) tags.

Example

<FoodItem>
    <Item1>Juice</Item1>
    <Item2>Milk</Item2>
</FoodItem>

In this case, I’m selecting the root node (i.e. FoodItem). By selecting this node, the template element tells the XSLT processor how to transform the output. The processor will replace the root node (i.e. the whole XML document) with what is being written between the <xsl:template> tags.
In this case, the contents of an HTML document are written inside the tags. When a user views any XML document that uses this XSL document, they will simply see the line “New item…”.

<xsl:template match="FoodItem">
  <html> 
    <body>
      <p>New item...</p>
    </body>
  </html>
</xsl:template>

Root Node Selection

In the example above, we selected the “FoodItem” node which happens to be the root node of our XML document. Another way of selecting the root node is to use a forward slash in place of the node’s name. The following example results in the same output as the above example.

Example:

<xsl:template match="/">
  <html>
    <body>
      <p>New item...</p>
    </body>
  </html>
</xsl:template>

 

Create an array in xslt

You can create any data type model you want to in xslt.

To create an array, just create a variable for it.

Example:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" omit-xml-declaration="yes"/>
 
    <xsl:variable name="array" as="element()*">
        <Item>A</Item>
        <Item>B</Item>
        <Item>C</Item>
    </xsl:variable>
 
    <xsl:template match="/">
        <xsl:value-of select="$array[2]"/>
    </xsl:template>
 
</xsl:stylesheet>

Output

B

%d bloggers like this: