XML :: Compile maxOccurs =“1”似乎无限制

时间:2014-09-23 15:56:50

标签: xml perl xsd

我无法理解perl模式编译器的输出,似乎在某些情况下会忽略maxOccurs指示符。如果我尝试多次使用复杂元素,则第一个引用似乎是正确的,但是即使maxOccurs指示符设置为" 1",后续引用也会作为数组输出。我只是开始玩xml架构,所以我的理解非常有限。

我必须遵循架构(对不起,我试图尽可能地减少它):

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

  <xs:element name="Top">   
    <xs:complexType>
      <xs:sequence>         
        <xs:element ref="Foo" minOccurs="0" maxOccurs="1"/>
        <xs:element ref="Bar" minOccurs="0" maxOccurs="1"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="Foo">
    <xs:complexType>
      <xs:sequence>         
        <xs:element ref="Bar" minOccurs="0" maxOccurs="1"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="Bar">
    <xs:complexType>
      <xs:attribute name="Baz" use="optional" />
    </xs:complexType>
  </xs:element>

</xs:schema>

当我跑步时:

perl -MXML::Compile::Schema -e 'print XML::Compile::Schema->new("example.xsd")->template("PERL", "Top");'

我得到以下输出:

# is an unnamed complex
{ # sequence of Foo, Bar

  # is an unnamed complex
  # is optional
  Foo =>
  { # sequence of Bar

    # is an unnamed complex
    # is optional
    Bar =>
    { # is a xs:anyType
      # becomes an attribute
      Baz => "anything", }, },

  # is an unnamed complex
  # complex structure shown above
  # is optional
  Bar => [{},], }

&#34;酒吧&#34;作为一个复杂的元素(正如预期的那样),然而&#34; Bar&#34;尽管maxOccurs =&#34; 1&#34;在顶层下出现并且复杂列表(不是我所期待的)。我的理解是错误的还是我的架构不正确?

1 个答案:

答案 0 :(得分:0)

输出将始终是一个数组,但在您的情况下,它仅限于一个数组。这是为了使访问数据的语法保持一致,而不管maxOccurs值。