Index: openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl,v diff -u -r1.37 -r1.38 --- openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl 8 Sep 2003 09:47:21 -0000 1.37 +++ openacs-4/packages/acs-tcl/tcl/utilities-procs.tcl 10 Sep 2003 08:09:42 -0000 1.38 @@ -3871,3 +3871,82 @@ return $result } + +ad_proc -public xml_get_child_node_content_by_path { + node + path_list +} { + Return the first non-empty contents of a child node down a given path from the current node. + +

+ + Example:

+set tree [xml_parse -persist {
+    <enterprise>
+      <properties>
+        <datasource>Dunelm Services Limited</datasource>
+        <target>Telecommunications LMS</target>
+        <type>DATABASE UPDATE</type>
+        <datetime>2001-08-08</datetime>
+      </properties>
+      <person recstatus = "1">
+        <comments>Add a new Person record.</comments>
+        <sourcedid>
+          <source>Dunelm Services Limited</source>
+          <id>CK1</id>
+        </sourcedid>
+        <name>
+          <fn>Clark Kent</fn>
+          <sort>Kent, C</sort>
+          <nickname>Superman</nickname>
+        </name>
+        <demographics>
+          <gender>2</gender>
+        </demographics>
+        <adr>
+          <extadd>The Daily Planet</extadd>
+          <locality>Metropolis</locality>
+          <country>USA</country>
+        </adr>
+      </person>
+    </enterprise>
+}]
+
+set root_node [xml_doc_get_first_node $tree]
+
+aa_equals "person -> name -> nickname is Superman" \
+    [xml_get_child_node_content_by_path $root_node { { person name nickname } }] "Superman"
+
+aa_equals "Same, but after trying a couple of non-existent paths or empty notes" \
+    [xml_get_child_node_content_by_path $root_node { { does not exist } { properties } { person name nickname } { person sourcedid id } }] "Superman"
+aa_equals "properties -> datetime" \
+    [xml_get_child_node_content_by_path $root_node { { person commments foo } { person name first_names } { properties datetime } }] "2001-08-08"
+
+ + @param node The node to start from + @param path_list List of list of nodes to try, e.g. + { { user_id } { sourcedid id } }, or { { name given } { name fn } }. + + @author Lars Pind (lars@collaboraid.biz) +} { + set result {} + foreach path $path_list { + set current_node $node + foreach element_name $path { + set current_node [xml_node_get_first_child_by_name $current_node $element_name] + + if { [empty_string_p $current_node] } { + # Try the next path + break + } + } + if { ![empty_string_p $current_node] } { + set result [xml_node_get_content $current_node] + if { ![empty_string_p $result] } { + # Found the value, we're done + break + } + } + } + return $result +}