XML :: Compile找不到我给它的根元素 [英] XML::Compile does not find the root element I give it

查看:85
本文介绍了XML :: Compile找不到我给它的根元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:使用现有的XSD模式生成填充和编写XML输出所需的Perl数据结构.

Goal: generate the Perl data structure needed to populate and write XML output, using an existing XSD schema.

我所做的:我已经安装 ,找到了有关如何使用它的教程,并在此处通读了经过修改的示例.我已经使用CAM模板编辑器处理了我的XSD文件,以确保CAM可以从模板生成示例XML文件,并且我已经将XML输出与XSD进行了比较,以确保两者以我认为他们应该的方式关联.他们有.

What I've done: I've installed XML::Compile, found a tutorial on how to use it, and read through a modified example here. I've processed my XSD file using CAM Template Editor to ensure CAM can generate an example XML file from the template, which it can, and I've compared the XML output to the XSD to ensure the two relate the way I think they should. They do.

接下来,我只是将 XML::Compile::Schema 指向XSD文件,然后要求创建模板字符串,要求template()从XSD中指定的根元素开始.

Next, I've simply pointed XML::Compile::Schema to the XSD file, then asked that the template string be created, asking template() to start at the root element as specified in the XSD.

出了什么问题:错误:找不到元素或属性'[root_element_name]'

What's Wrong: "error: cannot find element or attribute '[root_element_name]'

我的XSD与我在示例中看到的XSD确实有所不同.这就是我所拥有的:

My XSD does differ from those I've seen in the examples I've found. Here's what I have:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"

<xs:annotation></xs:annotation>

<!-- Include XML Schema containing type definitions and data validation -->
  <xs:include id="DataDefinitions" schemaLocation="DataDefinitions_XMLSchema.xsd"></xs:include>

  <!-- Root element -->
  <xs:element name="Metrics" type="metrics"></xs:element>

  <xs:complexType name ="metrics">
    <xs:sequence minOccurs ="1" maxOccurs ="1">
      <xs:element name = ...></xs:element>
      .
      .
      <xs:element name = ....></xs:element>
    </xs:sequence>
  </xs:complexType>

  [other complexType definitions used above to type some of the elements in the complexType 'metrics']

</xs:schema>

上面的几个元素都具有一些类型,这些类型然后在我所显示的部分下面进行定义,这就是XSD的本质.我在这里已经清楚地省略了很多细节,但是我不认为(希望")与问题有关.

Several of the elements above have types that are then defined below the section I've shown, and that is the essence of the XSD. I've left out much of that detail here clearly but I don't think ('hope') it's pertinent to the problem.

即使错了,Perl也很简单:

The Perl is pretty straightforward, even if wrong:

use warnings;
use strict;
use XML::LibXML;
use XML::Compile;

my $xsd = 'C:\schema.xsd';
my $schema = XML::Compile::Schema=>new($xsd);
my $xml_template = $schema->template('PERL', 'Metrics');

此操作失败,并出现上述错误:

This fails with the error mentioned above:

error: cannot find element or attribute named 'Metrics'

认为我可能不理解何时需要名称空间限定,何时不需要,我限定了Metrics并调用了

Thinking I probably don't understand when namespace qualification is needed and when it isn't, I qualified Metrics and called

my $xml_template = $schema->template('PERL', 'mstns:Metrics');

并且以相同的方式失败.

and that fails the same way.

我看到过定义根元素的其他示例,如下所示:

Other examples I've seen of defining the root element are done like so:

<element name = "Metrics">
  <complexType>
    <sequence>
      [etc]
    </sequence>
  </complexType>
</element>

不是完成此操作的方式,我想知道问题是否在于XML :: Compile无法识别的允许样式上的某些更改.我只是不知道如何确定为什么template()找不到我指定的元素.

rather than the way this one is done, and I wonder if the problem is some change in allowed style that XML::Compile doesn't recognize. I simply don't know how to determine why template() doesn't find the element I've specified.

建议?

推荐答案

在大括号中将targetNamespace指定为template调用中的类型,如下所示:

Specify the targetNamespace as the type in your template call in curly braces... like this:

use warnings;
use strict;
use XML::LibXML;
use XML::Compile;

my $xsd = 'C:\schema.xsd';
my $schema = XML::Compile::Schema=>new($xsd);
my $xml_template = $schema->template('PERL', '{http://tempuri.org/XMLSchema.xsd}Metrics');

这篇关于XML :: Compile找不到我给它的根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆