2013-01-23 87 views
3

我想创作一个XSD来验证XML文件。 XML文件的例子可以是这样的:作者XSD允许扩展元素

<person> 
    <fullname>John Doe</fullname> 
    <age>25</age> 
    <gender>male</gender> 
</person> 

其中一个要求是<person>标签是可扩展的,这意味着,除了上述3个所需的子元素,它可以包含任何名称的任意内容。所以这个文档在被XSD验证时是有效的。

<person> 
    <fullname>John Doe</fullname> 
    <age>25</age> 
    <gender>male</gender> 
    <address>USA</address> 
    <profession>worker</profession> 
</person> 

我看了一下<xs:any />元素,但XSD不允许我把<xs:any />一个<xs:all />元素中。我希望<fullname>,<gender><age>元素是必需的,并且它们中的每一个必须正好显示一个。除此之外,可以有零个或多个可选元素。

是否有可能通过支持的XSD规则实现此目标?

+0

我不认为这是可能的'',因为只有''可以包含''和''不能包含任何其他组。但如果实际上只有3个字段,则可以明确说明每个订单(注意,由于UPA,没有两个选项具有相同的前缀)。即:'a(bc | cb)| b(ac | ca)| C(AB | BA)'。 – 13ren

回答

0

结合xs:all和xs:any可以创建含糊不清的内容,这就是为什么它不被允许。但是,如果内容包含在xs:sequence中,则可以执行此操作。

注意:确保xs:any上的名称空间和processContent属性已根据您的要求正确设置。

他们对这种在XSD 1.1 extensiblity使用xs:openContent标签的更好的支持,但是,对于XSD 1.1支持。仍然有限。

enter image description here

<?xml version="1.0" encoding="utf-8" ?> 
<!--Created with Liquid XML 2016 Developer Bundle Edition 14.1.3.6618 (https://www.liquid-technologies.com)--> 
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="person"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element name="fullname" type="xs:string" /> 
       <xs:element name="age" type="xs:int" /> 
       <xs:element name="gender"> 
        <xs:simpleType> 
         <xs:restriction base="xs:string"> 
          <xs:enumeration value="male" /> 
          <xs:enumeration value="female" /> 
         </xs:restriction> 
        </xs:simpleType> 
       </xs:element> 
       <xs:any namespace="##any" processContents="skip" /> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
</xs:schema>