2012-04-14 65 views

回答

2

XSD 1.0中的一种方法是使用替换组。

BaseAny.xsd

<?xml version="1.0" encoding="utf-8" ?> 
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="anyOfCType" type="myCType" abstract="true"/> 
    <xsd:complexType name="myCType"> 
     <xsd:sequence> 
      <xsd:element name="something" type="xsd:string"/> 
     </xsd:sequence> 
    </xsd:complexType> 
    <xsd:element name="anyOfSType" type="mySType" abstract="true"/> 
    <xsd:simpleType name="mySType"> 
     <xsd:restriction base="xsd:string"> 
      <xsd:minLength value="2"/> 
     </xsd:restriction> 
    </xsd:simpleType> 
    <xsd:element name="root"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element ref="anyOfCType"/> 
       <xsd:element ref="anyOfSType"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

RefBaseAny.xsd:

<?xml version="1.0" encoding="utf-8" ?> 
<xsd:schema targetNamespace="http://tempuri.org/1/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/1/XMLSchema.xsd" xmlns:b="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:import namespace="http://tempuri.org/XMLSchema.xsd" schemaLocation="BaseAny.xsd"/> 

    <xsd:element name="c.one" type="b:myCType" substitutionGroup="b:anyOfCType"/> 
    <xsd:element name="s.one" type="b:mySType" substitutionGroup="b:anyOfSType"/> 

</xsd:schema> 

示例XML验证其对RefBaseAny.xsd:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) --> 
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:r="http://tempuri.org/1/XMLSchema.xsd"> 
    <r:c.one> 
     <something></something> 
    </r:c.one> 
    <r:s.one>12</r:s.one> 
</root> 

随着RELAX NG你必须定义一个命名模式来定义您的内容模型(模仿xsd:complexType)。

<define name="myType"> 
    ... 
</define> 

然后定义具有这种结构的元素:

<element> 
    <anyName/> 
    <ref name="myType"/> 
</element> 

Test.rng:

<?xml version="1.0" encoding="UTF-8"?> 
<grammar xmlns="http://relaxng.org/ns/structure/1.0"> 
    <start> 
     <element> 
      <anyName/> 
      <ref name="myType"/> 
     </element> 
    </start> 
    <define name="myType"> 
     <element name="c.one"> 
      <interleave> 
       <attribute name="id"> 
        <value>AA</value> 
       </attribute> 
       <attribute name="ref"> 
        <text/> 
       </attribute> 
      </interleave> 
      <element name="s.two"> 
       <text/> 
      </element> 
     </element> 
    </define> 
</grammar> 

有效XML1:

<?xml version="1.0" encoding="UTF-8"?> 
<Data> 
    <c.one id="AA" ref="123"> 
     <s.two>Hello</s.two> 
    </c.one> 
</Data> 

有效XML2:

<?xml version="1.0" encoding="UTF-8"?> 
<Data1> 
    <c.one id="AA" ref="123"> 
     <s.two>Hello</s.two> 
    </c.one> 
</Data1> 
+0

非常感谢您先生,RELAX NG解决方案正是我所需要的。但是在XSD中这不可能给出任何名字?我发现XSD非常有限。他们首先考虑XSD的实现者,而不是用户... – RoadBump 2012-04-15 14:02:43

+0

@RoadBump,带有XSD 1.0,任何名称都以通配符的形式支持(xsd:any表示元素,xsd:anyAttribute表示属性);然而,人们只能对与合格的命名空间和验证处理模型相关的限制进行限制 - 肯定没有任何与您请求的类型相关。有一个新规格,最近获得批准,XSD 1.1;在这个时候,我不评论,如果只是因为没有便宜的解析器可供大家使用... – 2012-04-15 14:39:20

+0

@PetruGardea你可以检查这个http://stackoverflow.com/questions/37851057/xsd-for-no - 请不要自动关闭 – user960567 2016-06-16 06:46:32

相关问题