XML模式命名空间 - 获取验证erorr

时间:2016-08-09 04:29:28

标签: xml validation schema

我尝试根据架构验证XML文件。我对此非常陌生,但我几乎100%与命名空间有关。问题是,命名空间让我困惑:/。有人可以解释如何设置它以便验证吗?我非常感激。谢谢!

我的XML:

<?xml version="1.0" encoding="UTF-8"?>

<tvGuide xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="tvguide.xsd">
    <Provider TimeZone="central">      
        <ProviderName>Cox Communications</ProviderName>
        <State>NE</State>
    </Provider>
    <ListingDetails>
        <ShowName>American Horror Story</ShowName>
        <category>Drama</category>
        <HD>true</HD>
        <Rating>TV-MA</Rating>
        <New>false</New>   
        <EpisodeNumber>37</EpisodeNumber>
        <Season>5</Season>
        <Date>2016-07-25</Date>
        <EpisodeDesc>Insert Episode Description Here</EpisodeDesc>
        <Audio>Stereo</Audio>
        <ClosedCaption>true</ClosedCaption>
    </ListingDetails>
    <ChannelDetails>
        <ChannelName>FX</ChannelName>
        <ChannelDescription>Channel Description Here</ChannelDescription>
        <ChannelLogo>Channel Logo Here</ChannelLogo>
    </ChannelDetails>
    <Provider TimeZone="central">      
        <ProviderName>Cox Communications</ProviderName>
        <State>NE</State>
    </Provider>
    <ListingDetails>
        <ShowName>The Arctic Circle</ShowName>
        <category>Documentary</category>
        <HD>false</HD>
        <Rating>TV-G</Rating>
        <New>true</New>   
        <EpisodeNumber>1</EpisodeNumber>
        <Season>0</Season>
        <Date>2016-07-25</Date>
        <EpisodeDesc>This documentary takes viewers on an expedition to the Arctic Cirlee</EpisodeDesc>
        <Audio>Stereo</Audio>
        <ClosedCaption>true</ClosedCaption>
    </ListingDetails>
    <ChannelDetails>
        <ChannelName>NatGeo</ChannelName>
        <ChannelDescription>Educational</ChannelDescription>
        <ChannelLogo>Channel Logo Here</ChannelLogo>
    </ChannelDetails>
    <Provider TimeZone="central">      
        <ProviderName>Cox Communications</ProviderName>
        <State>NE</State>
    </Provider>
    <ListingDetails>
        <ShowName>Dick Van Dyke</ShowName>
        <category>Classic Family</category>
        <HD>true</HD>
        <Rating>TV-G</Rating>
        <New>false</New>   
        <EpisodeNumber>12</EpisodeNumber>
        <Season>1</Season>
        <Date>2016-07-25</Date>
        <EpisodeDesc>This is a classic episode of Dick Van Dyke.</EpisodeDesc>
        <Audio>Stereo</Audio>
        <ClosedCaption>true</ClosedCaption>
    </ListingDetails>
    <ChannelDetails>
        <ChannelName>Nick at Nite</ChannelName>
        <ChannelDescription>Classic TV</ChannelDescription>
        <ChannelLogo>Channel Logo Here</ChannelLogo>
    </ChannelDetails>
</tvGuide>

和我的XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified"
    vc:minVersion="1.1">


        <xs:element name="tvGuide">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="Provider"  maxOccurs="unbounded">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="ProviderName" type="xs:string"/>
                                <xs:element name="State" type="xs:string"/>
                            </xs:sequence>
                            <xs:attribute name="TimeZone" type="xs:string" use="required"/>
                        </xs:complexType>
                    </xs:element>
               <xs:element name="ListingDetails" maxOccurs="unbounded" >
              <xs:complexType>
                  <xs:sequence>
                         <xs:element name="ShowName" type="xs:string"/>
                         <xs:element name="category" type="xs:string"/>
                         <xs:element name="HD" type="xs:boolean"/>
                         <xs:element name="Rating" type="xs:string"/>
                         <xs:element name="New" type="xs:boolean"/>
                         <xs:element name="EpisodeNumber" type="xs:integer"/>
                         <xs:element name="Season" type="xs:integer"/>
                         <xs:element name="Date" type="xs:date"/>
                         <xs:element name="EpisodeDesc" type="xs:string"/>
                         <xs:element name="Audio" type="xs:string"/>
                         <xs:element name="ClosedCaption" type="xs:boolean"/>
                  </xs:sequence>

              </xs:complexType>
          </xs:element>
                    <xs:element name="ChannelDetails" maxOccurs="unbounded" >
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="ChannelName" type="xs:string"/>
                                <xs:element name="ChannelDescription" type="xs:string"/>
                                <xs:element name="ChannelLogo" type="xs:string"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>

                </xs:sequence>
            </xs:complexType>
        </xs:element>
     </xs:schema>

1 个答案:

答案 0 :(得分:0)

您的架构指定vc:minVersion =&#34; 1.1&#34;。它没有使用任何XSD 1.1功能,因此请尝试删除此属性或将其设置为1.0。

我能够通过使用Saxon运行验证而不启用-xsdversion:1.1来重现问题。在这种情况下,您运行Saxon作为1.0处理器,1.0处理器(如果它识别vc:minVersion)将跳过需要1.1的模式的部分。

所以另一种方法是使用-xsdversion运行Saxon:1.1。

验证仍然失败,但是:

Validation error on line 27 column 34 of test.xml:
  FORG0001: In content of element <tvGuide>: The content model does not allow element
  <Provider> to appear immediately after element <ChannelDetails>. Expected <ChannelDetails>
  or nothing. 

但这是一个合理的错误:如果您希望序列(Provider,ListingDetails,ChannelDetails)可重复,那么您的第一个xs:sequence元素应该被赋予maxOccurs="unbounded"属性。