XSD - 在嵌套复杂类型中使用Any

时间:2015-08-12 07:08:06

标签: xsd

在下面的XSD中,当我尝试在扩展架构中的AdditionalInfo下定义元素时,我收到一条错误,说“限制无效的粒子”。有人可以帮助我实现这个具有嵌套复杂类型的目标,我可以限制继承类型中的元素吗?

我看到链接xsd sequence of any type of element that is an extension of specific complex type,但我的问题有点不同。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema.xsd"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:complexType name="BaseActivityWithIncidentId">
<xs:sequence>
  <xs:element name="Category" type="xs:string"/>
  <xs:element name="SubCategory" type="xs:string"/>
  <xs:element name="IncidentId" type="xs:long"/>
  <xs:element name="ActivityTime" type="xs:dateTime"></xs:element>
  <xs:element name="Remarks" type="xs:string"></xs:element>
  <xs:element name="AdditionalInfo">
    <xs:complexType>
      <xs:sequence>
        <xs:any minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:sequence>
  </xs:complexType>

  <xs:complexType name="PartInstalledRequest">
<xs:complexContent>
  <xs:restriction base="BaseActivityWithIncidentId">
    <xs:sequence>
      <xs:element name="Category" type="xs:string" fixed="Part"/>
      <xs:element name="SubCategory" type="xs:string" fixed="Installed"/>
      <xs:element name="IncidentId" type="xs:long"/>
      <xs:element name="ActivityTime" type="xs:dateTime"></xs:element>
      <xs:element name="Remarks" type="xs:string"></xs:element>
      <xs:element name="AdditionalInfo">
        <xs:complexType>
          <xs:sequence>    
            <xs:element name="PartName" type="xs:string"></xs:element>
            <xs:element name="PartNumber" type="xs:string"></xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:restriction>
</xs:complexContent>
  </xs:complexType>
</xs:schema>

我正在粘贴有问题的完整代码。我不知道Megha为什么你没有遇到这个问题,但你可以尝试这个代码

string baseSchema = @"<?xml version=""1.0"" encoding=""utf-8""?>
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:complexType name=""BaseActivityWithIncidentId"">
<xs:sequence>
<xs:element name=""Category"" type=""xs:string""/>
<xs:element name=""SubCategory"" type=""xs:string""/>
<xs:element name=""IncidentId"" type=""xs:long""/>
<xs:element name=""ActivityTime"" type=""xs:dateTime""></xs:element>
<xs:element name=""Remarks"" type=""xs:string""></xs:element>
<xs:element name=""AdditionalInfo"">
<xs:complexType>
<xs:sequence>
<xs:any minOccurs=""0"" maxOccurs=""unbounded""/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>";
string inheritedSchema = @"<?xml version=""1.0"" encoding=""utf-8""?>
<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" 
id=""PartInstalled"">
<xs:include id=""BaseSchema"" schemaLocation=""BaseSchema.xsd"" />
<xs:element name=""Activity"">
<xs:complexType>
<xs:complexContent>
<xs:restriction base=""BaseActivityWithIncidentId"">
<xs:sequence>
<xs:element name=""Category"" type=""xs:string"" fixed=""Part""/>
<xs:element name=""SubCategory"" type=""xs:string"" fixed=""Installed""/>
<xs:element name=""IncidentId"" type=""xs:long""/>
<xs:element name=""ActivityTime"" type=""xs:dateTime""></xs:element>
<xs:element name=""Remarks"" type=""xs:string""></xs:element>
<xs:element name=""AdditionalInfo"">
<xs:complexType>
<xs:sequence>    
<xs:element name=""PartName"" type=""xs:string""></xs:element>
<xs:element name=""PartNumber"" type=""xs:string""></xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>";
XmlSchemaSet schemas = new XmlSchemaSet();
var schema = schemas.Add("", 
XmlReader.Create(new StringReader(inheritedSchema)));
schemas.Add("", XmlReader.Create(new StringReader(baseSchema)));
schemas.Compile();
string xml = @"<Activity>
<Category>Part</Category>
<SubCategory>Installed</SubCategory>
<IncidentId>1</IncidentId>
<ActivityTime>2015-08-11T15:00:00.587Z</ActivityTime>
<Remarks>123</Remarks>
<AdditionalInfo>
<PartName>CR</PartName>
<PartNumber>CR123</PartNumber>
</AdditionalInfo>
</Activity>";
XDocument doc = XDocument.Parse(xml);
string msg = "";
doc.Validate(schemas, (o, e) =>
{
msg += e.Message + Environment.NewLine;
});
Console.WriteLine(msg == "" ? "Document is valid" : 
"Document invalid: "+     msg);
Console.ReadLine();

1 个答案:

答案 0 :(得分:0)

如果要定义嵌套复杂类型,可以执行以下操作。您可以定义一个新的复杂类型,就像&#34; PartInstalledRequest&#34;并将其用作AddtionalInfo中的类型。

<xs:complexType name="BaseActivityWithIncidentId">
   <xs:sequence>
     <xs:element name="Category" type="xs:string"/>
     <xs:element name="SubCategory" type="xs:string"/>
     <xs:element name="IncidentId" type="xs:long"/>
     <xs:element name="ActivityTime" type="xs:dateTime"></xs:element>
     <xs:element name="Remarks" type="xs:string"></xs:element>
     <xs:element name="AdditionalInfo" type="AddtionalType" minOccurs="0"
               maxOccurs="unbounded">   
     </xs:element>
     </xs:sequence>
  </xs:complexType>  
  <xs:complexType name="AddtionalType">
    <xs:complexContent>
       <xs:sequence>    
          <xs:element name="PartName" type="xs:string"></xs:element>
          <xs:element name="PartNumber" type="xs:string"></xs:element>
      </xs:sequence>    
    </xs:complexContent>
  </xs:complexType>