XML: Difference between revisions

From wiki
Jump to navigation Jump to search
Created page with "Generic remarks about XML. Comments start with '<!--' and end with '-->'. Comments cannot be nested. Regular expressions to remove comments (Perl code) <syntaxhighlight lang..."
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
Generic remarks about XML.
Generic remarks about XML.


Comments start with '<!--' and end with '-->'.
Comments start with <code><nowiki><!--</nowiki></code> and end with <code><nowiki>--></nowiki></code>.
Comments cannot be nested.
Comments cannot be nested.


Regular expressions to remove comments (Perl code)
[[Perl]] [[Regular Expressions]] to check if we enter or leave a comment block
<syntaxhighlight lang=perl>
<syntaxhighlight lang=perl>
#If comment-end is found remove comment-part.
  if ( ( $line =~ /-->/ ) && ( $incomment == 1 )) {
# comment-start may be on the same line.
  $incomment = 0;
if ( $line =~ /(\<\!--)?.*--\>/ ) {
  $line =~ s/(<!--)?.*$&//;
$comment = 0;
}
$line =~ s/$&//;
if ( $incomment == 0 ) {
# If there is only a comment-start, clear line until comment-end is found
  if ( $line =~ /<!--/ ) {
} elsif ( $line =~ /\<\!--/ ) {
  $incomment = 1;
$comment = 1;
  $line =~ s/$&.*?(-->)//;
$line =~ s/\<\!--.*$//;
  if ( $1 ) {
}
    $incomment = 0;
if ( $comment ) {
  }
  $line = '';
  }
}
</syntaxhighlight>
</syntaxhighlight>
Python module and code examples [[Python:XML]].
;xmllint --noout <xmlfile>
:Test if <xmlfile> has valid xml and report errors. Without --noout the tree will be printed too.

Latest revision as of 19:21, 7 September 2018

Generic remarks about XML.

Comments start with <!-- and end with -->. Comments cannot be nested.

Perl Regular Expressions to check if we enter or leave a comment block

 if ( ( $line =~ /-->/ ) && ( $incomment == 1 )) {
  $incomment = 0;
  $line =~ s/(<!--)?.*$&//;
 }
 if ( $incomment == 0 ) {
  if ( $line =~ /<!--/ ) {
   $incomment = 1;
   $line =~ s/$&.*?(-->)//;
   if ( $1 ) {
    $incomment = 0;
   }
  }

Python module and code examples Python:XML.

xmllint --noout <xmlfile>
Test if <xmlfile> has valid xml and report errors. Without --noout the tree will be printed too.