Commits

J Mash committed 79df96f

Added new nodelist functionality.
to_literal_delimited - Returns the concatenation of all the string-values of all the nodes in the list, delimited by the specified separator.
to_literal_list - In array context, this method returns all the string-values of all the nodes in the list as an array. In scalar context, the method returns an array reference.

Comments (0)

Files changed (3)

 Revision history for Perl extension XML::LibXML
 
+2.0105          Wed  4 Sep 10:12:06 IDT 2013
+    - Pull some commits from Jason Mash (JRMASH) to add convenience methods
+      to the XML::LibXML::NodeList module.
+          - New method 'to_literal_delimited($separator)'
+          - New method 'to_literal_list'()'
+
 2.0104          Fri 30 Aug 12:36:06 IDT 2013
     - Fix https://rt.cpan.org/Ticket/Display.html?id=88060
         - Use quoted version number in the SYNOPSIS.

lib/XML/LibXML/NodeList.pm

             );
 }
 
+sub to_literal_delimited {
+    my $self = CORE::shift;
+    return XML::LibXML::Literal->new(
+            join(CORE::shift, CORE::grep {defined $_} CORE::map { $_->string_value } @$self)
+            );
+}
+
+sub to_literal_list {
+    my $self = CORE::shift;
+    my @nodes = CORE::map{ XML::LibXML::Literal->new($_->string_value())->value() } @{$self};
+    
+    if (wantarray) {
+        return( @nodes );
+    }
+    return( \@nodes );
+}
+
 sub to_number {
     my $self = CORE::shift;
     return XML::LibXML::Number->new(
 Returns the concatenation of all the string-values of all
 the nodes in the list.
 
+=head2 to_literal_delimited($separator)
+
+Returns the concatenation of all the string-values of all
+the nodes in the list, delimited by the specified separator.
+
+=head2 to_literal_list()
+
+Returns all the string-values of all the nodes in the list as
+a perl list.
+
 =head2 get_node($pos)
 
 Returns the node at $pos. The node position in XPath is based at 1, not 0.
 use strict;
 use warnings;
 
-use Test::More tests => 27;
+use Test::More tests => 29;
 
 use XML::LibXML;
 use IO::Handle;
 # TEST
 is($nodelist->to_literal, "OKNOT OK", ' TODO : Add test name');
 
+# TEST
+is($nodelist->to_literal_delimited(','), "OK,,,,NOT OK", 'TODO : Add test name');
+
+# TEST
+is_deeply([$nodelist->to_literal_list()], ['OK', '', '', '', 'NOT OK'], 'TODO : Add test name');
+
 {
     my $other_nodelist = $dom->findnodes('//BBB');
     while ($other_nodelist->to_literal() !~ m/\ANOT OK/)