我很难使用 LibXML 写入包含 XLIFF 元素的 strings.xml 文件。请注意,我正在尝试写入一个已经存在的值(在读取时解析节点没有问题)。我有时在编写 HTML 元素时还需要使用 appendWellBalancedChunk。
#!/usr/bin/env perl
#
# Create a simple XML document
#
use strict;
use warnings;
use XML::LibXML;
my $doc = XML::LibXML::Document->new('1.0', 'utf-8');
my $root = $doc->createElement("resources");
$root->setAttribute('xmlns:xliff' => 'urn:oasis:names:tc:xliff:document:1.2');
my $tag = $doc->createElement("string");
$tag->setAttribute('name'=>'no_messages');
my $string = '<xliff:g id="MILLISECONDS">%s</xliff:g>ms';
$tag->appendWellBalancedChunk($string);
$root->appendChild($tag);
$doc->setDocumentElement($root);
print $doc->toString();
当我运行它时,我得到以下信息:
$ perl xliff.pl
namespace error : Namespace prefix xliff on g is not defined
<xliff:g id="MILLISECONDS">%s</xliff:g>ms
^
谢谢