2011-12-17 105 views
0

比方说,我定义以下XML架构,author.xsd:包括XML架构一旦

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

<xs:element name="author">  
<xs:complexType> 
    <xs:sequence>  
    <xs:element name="firstname" type="xs:string"/>  
    <xs:element name="lastname" type="xs:string"/>  
    </xs:sequence> 
</xs:complexType> 
</xs:element>  

</xs:schema> 
</xml> 

如何将我去只是包括XML模式定义一次,在我的实际XML文件中的多个author元素是什么时候?我想避免为每个作者元素定义xmlns:xsi。

<?xml version="1.0"?> 
<author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="author.xsd"> 
    <FirstName>Mark</FirstName> 
    <LastName>Twain</LastName> 
</author> 
<author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="author.xsd"> 
    <FirstName>Awesome</FirstName> 
    <LastName>Possum</LastName> 
</author> 

回答

1

从我的理解,你想xml文件有超过1 author。如果是这种情况,那么您需要定义一个父元素并将author作为它的子元素。父元素就像一个容器。所以这里是XML规则

一个xml文件只能有一个根元素。所有其他元素应该是它的孩子。

所以,如果你想限制的作者说100然后更改maxOccurs该值我会改变你的模式是这样

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

<xs:element name="authors"> 
    <xs:complexType> 
    <xs:sequence> 
     <xs:element name="author" maxOccurs="unbounded">  
     <xs:complexType> 
      <xs:sequence>  
       <xs:element name="firstname" type="xs:string"/>  
       <xs:element name="lastname" type="xs:string"/> 
      </xs:sequence> 
     </xs:complexType> 
     </xs:element> 
    </xs:sequence> 
    </xs:complexType> 
</xs:element> 
</xs:schema> 

注意