Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are blank child nodes of any use to XML parsers?

Why do we have to have the notion of blank XML nodes? What benefit do they bring to the alchemy of XML parsing?

A simple example here with Perl's XML::LibXML:

use strict;
use warnings;
use feature 'say';
use XML::LibXML;

my $xml = XML::LibXML->load_xml( string => <<'XMLDOC' );
<alphabet>
 <child name='alpha'/>
 <child name='bravo'/>
 <child name='charlie'/>
 <child name='delta'/>
 <child name='echo'/>
</alphabet>
XMLDOC

my ( $parent ) = $xml->findnodes( '/alphabet' );

my @all_kids  = $parent->childNodes;
my @real_kids = $parent->nonBlankChildNodes;

say 'All kids : ', scalar @all_kids;   # '11'
say 'Real kids : ', scalar @real_kids; # '5' => 6 blank child nodes

What puzzles me is that the parser makes a distinction between retrieving all child nodes and only non-blank ones.

It would seem then that there must be at least one sane use for these blank nodes. It would be interesting to know exactly what those uses are.

like image 272
Zaid Avatar asked Dec 28 '22 08:12

Zaid


2 Answers

Consider this case from HTML:

<div><b>hello</b><i>world</i></div>

vs this one:

<div>
    <b>hello</b>
    <i>world</i>
</div>

In the first example, there are no whitespace nodes, and the rendering engine will not place a space between helloworld. In the second example, since there is a whitespace node between the textnodes, it will come out as hello world.

You need to know the whitespace nodes are there, since some XML languages will care about their placement.

like image 70
Eric Strom Avatar answered Jan 08 '23 19:01

Eric Strom


The parser cannot distinguish between significant blank nodes and non-significant blank nodes. That depends totally on the semantics of the XML. If the parser eliminated blank-only nodes and you were writing an application where they were significant, you'd be writing this question from the other point of view.

like image 37
Jim Garrison Avatar answered Jan 08 '23 21:01

Jim Garrison