Are empty child nodes for use in XML syntax?

Why should we be aware of empty XML nodes? What advantage do they bring to the alchemy of XML parsing?

A simple example here with Perl 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 nonempty ones.

It would seem that for these empty nodes there should be at least one reasonable use. It would be interesting to know exactly what it is.

+4
source share
2 answers

Consider this case from HTML:

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

vs this:

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

In the first example, there are no whitespace, and the rendering engine will not place a space between hello world . In the second example, since there is a node space between the text fields, it will display as hello world .

You need to know that there are space nodes, as some XML languages โ€‹โ€‹will take care of their placement.

+7
source

The parser cannot distinguish between significant empty nodes and non-essential empty nodes. It depends entirely on the semantics of XML. If the parser has eliminated the empty nodes and you are writing an application in which they were significant, you should write this question from a different perspective.

+3
source

Source: https://habr.com/ru/post/1387898/


All Articles