2014-09-23 28 views
1

我無法理解perl模式編譯器的輸出,看起來在某些情況下maxOccurs指示器被忽略。如果我嘗試多次使用一個複雜元素,那麼第一個引用似乎是正確的,但是,即使maxOccurs指示符設置爲「1」,後續引用也會作爲數組輸出。我只是開始玩XML模式,所以我的理解是非常有限的。XML ::編譯maxOccurs =「1」似乎無界

我有以下模式(對不起,我試圖削減下來儘可能地):

<?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 => [{},], } 

作爲Foo元素的「Bar」顯示爲複雜(如預期),但頂層下的「Bar」出現d列表複雜,即使maxOccurs =「1」(不是我所期望的)。我的理解是錯誤的還是我的模式不正確?

回答

0

輸出將始終是一個數組,但在您的情況下,它僅限於第一個。這是爲了保持訪問數據的語法一致,而不管maxOccurs的值如何。

+0

謝謝AKHolland,我試圖爲每個實體創建一個Class :: Struct,以便我可以更容易地填充它們,但是在這種情況下Bar會初始化爲一個數組。我想我可能不得不走完整個dom樹來完成這個,這可能不值得麻煩。 – Rajuk 2014-09-26 14:32:43