Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 75 lines (66 sloc) 2.698 kb
2fc775d7 » nniclausse
2008-06-23 - apply patch for dyn_variables using xpath for http (html) (TSUN-72;
1 %% xpath_functions.erl
2 %% @author Pablo Polvorin
3 %% @doc Some core xpath functions that can be used in xpath expressions
4 %% created on 2008-05-07
5 -module(mochiweb_xpath_functions).
6
7 -export([default_functions/0]).
8
9 %% Default functions.
10 %% The format is: {FunctionName, fun(), FunctionSignature}
11 %% @type FunctionName = atom()
12 %% @type FunctionSignature = [XPathType]
13 %% @type XPathType = node_set | string | number | boolean
14 %%
15 %% The engine is responsable of calling the function with
16 %% the correct arguments, given the function signature.
17 default_functions() ->
18 [
19 {'count',fun count/2,[node_set]},
20 {'name',fun 'name'/2,[node_set]},
21 {'starts-with', fun 'starts-with'/2,[string,string]},
22 {'substring', fun substring/2,[string,number,number]},
23 {'sum', fun sum/2,[node_set]},
24 {'string-length', fun 'string-length'/2,[string]}
25 ].
26
27
28 %% @doc Function: number count(node-set)
29 %% The count function returns the number of nodes in the
30 %% argument node-set.
31 count(_Ctx,[NodeList]) ->
32 length(NodeList).
33
34 %% @doc Function: string name(node-set?)
35 'name'(_Ctx,[[{Tag,_,_}|_]]) ->
36 Tag.
37
38 %% @doc Function: boolean starts-with(string, string)
39 %% The starts-with function returns true if the first argument string
40 %% starts with the second argument string, and otherwise returns false.
41 'starts-with'(_Ctx,[Left,Right]) ->
42 Size = size(Right),
43 case Left of
44 <<Right:Size/binary,_/binary>> -> true;
45 _ -> false
46 end.
47
48 %% @doc Function: string substring(string, number, number?)
49 %% The substring function returns the substring of the first argument
50 %% starting at the position specified in the second argument with length
51 %% specified in the third argument
52 substring(_Ctx,[String,Start,Length]) when is_binary(String)->
53 Before = Start -1,
54 After = size(String) - Before - Length,
55 case (Start + Length) =< size(String) of
56 true ->
57 <<_:Before/binary,R:Length/binary,_:After/binary>> = String,
58 R;
59 false ->
60 <<>>
61 end.
62
63 %% @doc Function: number sum(node-set)
64 %% The sum function returns the sum, for each node in the argument
65 %% node-set, of the result of converting the string-values of the node
66 %% to a number.
67 sum(_Ctx,[Values]) ->
68 lists:sum(lists:map(fun mochiweb_xpath_utils:number_value/1,Values)).
69
70 %% @doc Function: number string-length(string?)
71 %% The string-length returns the number of characters in the string
72 %% TODO: this isn't true: currently it returns the number of bytes
73 %% in the string, that isn't the same
74 'string-length'(_Ctx,[String]) ->
75 size(String).
Something went wrong with that request. Please try again.