comparison cpack/dml/lib/xmltraverse.pl @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:718306e29690
1 /* Part of DML (Digital Music Laboratory)
2 Copyright 2014-2015 Samer Abdallah, University of London
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 :- module(xmltraverse, [traverse/3, op(720,xfx,@), op(750,xfy,~), op(750,xfy,..) ]).
20 % for traverse/3 paths
21 :- op(720,xfx,@).
22 :- op(750,xfy,~).
23 :- op(750,xfy,..).
24
25 %% traverse(+Dir:oneof([f,b]), +Doc:xml_element, -P:xml_path) is nondet.
26 %
27 % Succeeds once for each path through an XML element starting at the top
28 % and travelling all or part of the way down through the tree structure.
29 traverse(f,El,Path) :- traverse_f(El,Path).
30 traverse(b,El,Path) :- traverse_b([],El,Path).
31
32 traverse_f(Other,{Other}) :- atomic(Other), !.
33 traverse_f(Element,\Element).
34 traverse_f(element(Name,_,_), n(Name)).
35 traverse_f(element(Name,Attribs,_),Name @ A) :- member(A,Attribs).
36 traverse_f(element(Name,_,Content),Name .. Path1) :-
37 ( Path1 = \Content
38 ; Path1 = (I~Path2),
39 nth1(I,Content,SubE),
40 traverse_f(SubE,Path2)
41 ).
42
43
44 traverse_b(L,Other,{Other}..L) :- atomic(Other), !.
45 traverse_b(L,element(Name,Attribs,Content),Path) :- !,
46 ( Path = (A @ Name .. L), member(A,Attribs)
47 ; nth1(N,Content,SubE),
48 traverse_b(N ~ Name..L,SubE,Path)
49 ).
50