【发布时间】:2015-02-18 08:37:47
【问题描述】:
我给了一个 xsd 文件,一个 Perl 环境,只能使用
XML::LibXML , XML::SAX, XML::Compile
xsd 的相关部分是
<complexType name="property">
<attribute type="propertyvalue" name="name" use="required"/>
<attribute type="string" name="value" use="required"/>
</complexType>
<simpleType name="propertyvalue">
<restriction base="string">
<enumeration value="propertya"/>
<enumeration value="propertyb"/>
[....some more values...]
</restriction>
</simpleType>
我必须为每个值编写多个属性。 到目前为止,我使用的是 XML::Compile::Schema
$schema->template('PERL', $type);
my $doc = XML::LibXML::Document->new('1.0', 'UTF-8');
my $write = $schema->compile(WRITER => $type, use_default_namespace => 1);
my %hash;
$hash = {
properties =>
{
version => 42,
property =>
[ {name => "propertya",
value => "example",
}, ],
}
};
my $xml= $write->($doc, $hash);
$doc->setDocumentElement($xml);
我现在的问题是,我看不出一种方法可以如何添加多个属性标签,例如
@properties =("propertya","propertyb",[.and so on.]);
foreach my $pname (@properties){
$hash = {
properties =>
{
version => 42,
property =>
[ {name => $pname;
value => "example",
}, ],
}
};
my $xml= $write->($doc, $hash);
$doc->setDocumentElement($xml);
}
不会覆盖所有内容或获取文件其他部分丢失的消息。
有没有办法做到这一点,有没有办法在执行后附加标签
my $xml = $write->($doc, $hash);
?
【问题讨论】:
-
看起来属性只是一个匿名数组,包含匿名哈希。你不能只使用你的 foreach 循环来构建数组然后创建你的 xml,一旦给它提供了对属性数组的引用?