我有以下Perl
脚本用于XML
正确缩进文件:
@files = glob "*.xml";
undef $/;
for $file (@files) {
$indent = 0;
open FILE, $file or die "Couldn't open $file for reading: $!";
$_ = readline *FILE;
close FILE or die "Couldn't close $file: $!";
# Remove whitespace between > and < if that is the only thing separating them
s/(?<=>)\s+(?=<)//g;
# Indent
s{ # Capture a tag <$1$2$3>,
# a potential closing slash $1
# the contents $2
# a potential closing slash $3
<(/?)([^/>]+)(/?)>
# Optional white space
\s*
# Optional tag.
# $4 contains either undef, "<" or "</"
(?=(</?))?
}
{
# Adjust the indentation level.
# $3: A <foo/> tag. No alteration to indentation.
# $1: A closing </foo> tag. Drop one indentation level
# else: An opening <foo> tag. Increase one indentation level
$indent +=
$3 ? 0 :
$1 ? -1 :
1;
# Put the captured tag back into place
"<$1$2$3>" .
# Two closing tags in a row. Add a newline and indent the next line
($1 and defined($4) and ($4 eq "</") ? "\n" . (" " x $indent) :
$4 ? "\n" . (" " x $indent) :
""
)
# /g repeat as necessary
# /e Execute the block of perl code to create replacement text
# /x Allow whitespace and comments in the regex
}gex;
open FILE, ">", $file or die "Couldn't open $file for writing: $!";
print FILE or die "Couldn't write to $file: $!";
close FILE or die "Couldn't close $file: $!";
}
首先,它缩进了 my tabs
,我想要两个空格。此外,它在同一缩进中生成标签,使其位于同一行,而不是下一行,但具有相同的缩进:
<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE kit SYSTEM "tc.dtd"><kit><contact/><description>
它应该在哪里:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE kit SYSTEM "tc.dtd">
<kit>
<contact/>
<description>
...
我承认有用于缩进的 Perl 工具XML
,例如XML-Tidy
但由于tc.dtd
标记,我总是收到一个错误,抱怨tc.dtd
文件上无法解决的依赖关系,而我只关心相同(格式)的缩进,而不是依赖关系本身。我的Perl
正则表达式有什么问题?