一次包含XML模式
假设我定义了以下XML模式author.xsd:一次包含XML模式,xml,xsd,Xml,Xsd,假设我定义了以下XML模式author.xsd: <?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
<?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文件中定义多个author元素时,如何只包含一次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>
做记号
吐温
真棒
负鼠
据我所知,您希望xml文件包含多个作者。如果是这种情况,那么您需要定义一个父元素,并将作者
作为它的子元素。父元素的行为类似于容器。这就是XML的规则
一个xml文件只能有一个根元素。所有其他元素都应该是它的子元素。
所以,我会像这样改变你的模式
<?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>
注意
如果要将作者限制为100,请将maxOccurs
更改为该值