验证xsd中属于另一个名称空间的元素

时间:2020-08-19 05:54:55

标签: xml xsd

我正在使用此xsd模式来验证存档XML:

http://www.opengroup.org/xsd/archimate/3.0/archimate3_Model.xsd

我有这个xsd模式,其中包含2个新元素,但是基本的xsd是第一个元素

<?xml version="1.0" encoding="utf-8"?>
<xs:schema
    xmlns="....."
    targetNamespace="....."
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:archimate="http://www.opengroup.org/xsd/archimate/3.0/"
    version ="1.0">
                
<xs:complexType name="ShapeLine">       
    <xs:complexContent>
        <xs:extension base="archimate:Line" >
            <xs:attribute name="category" type="xs:string" use="required">                  
                <xs:annotation>
                    <xs:documentation>
                    </xs:documentation>
                </xs:annotation>
            </xs:attribute> 
            <xs:attribute name="source" type="xs:IDREF" use="required" />
            <xs:attribute name="target" type="xs:IDREF" use="required" />
        </xs:extension>             
    </xs:complexContent>
</xs:complexType>  
    
<xs:complexType name="Shape">
    .....
</xs:complexType>
</xs:schema>

,这是我需要使用第二个模式进行验证的xml。 我有2例。一种是我需要验证archimate xml-s,另一种是我使用链接中提供的xsd进行验证,第二种情况是我有xml-s,它可以包含来自archimate的所有元素以及这两种类型(Sahpe,ShapeLine) 。这很好用,只是我只需要在要验证包含新类型的xml-s时才验证该类型。所以以某种方式我只需要在xsd中包含新类型(Shape,ShapeLine)的类型

<?xml version="1.0"?>
<model
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" identifier="id-5f3cf414a7067b6ee47a783e" 
  xmlns="http://www.opengroup.org/xsd/archimate/3.0/">
 <name xml:lang="EN"> </name>
 <type xml:lang="EN"> </type>
   <views>
   <diagrams>
    <view identifier="id-b3cfd2c8-3ead-4980-985b-0e6078e8a67c">
      <name xml:lang="EN">View 8/19/2020 9:42:44 AM (UTC)</name>
      <node xsi:type="q1:Shape" identifier="id-41bed501-edbe-4526-a765- 
    0f70e0db6ded" x="10" y="30" w="1" h="1" nameinternal="BD_shapes_av_Box" shapeId="5bb51746ec9d2668081e05b7" angle="0" isgroup="False" alignment="" textalign="0" size="72 72">
        <label xml:lang="EN" />
        <style>
          <fillColor r="209" g="210" b="212" />
          <font name="Lato" size="13">
            <color r="0" g="0" b="0" />
          </font>
        </style>
      </node>
    </view>
  </diagrams>
</model>

我还要验证xml中位于名称下方的type元素。 我试图这样验证:

<xsd:element name="type" type="xs:string"/>

名称空间“ http://www.opengroup.org/xsd/archimate/3.0/”中的元素“模型”具有无效的子元素“类型”。感谢您的帮助

这是架构的一部分:

 public partial class ModelType : NamedReferenceableType
{

    private List<propertyType> propertiesField;

    private List<ElementType> elementsField;

    private List<RelationshipType> relationshipsField;

    private List<OrganizationType> organizationsField;

    private List<PropertyDefinitionType> propertyDefinitionsField;

    //[System.Xml.Serialization.XmlElementAttribute]
    //public MetadataType metadata { get; set; }

    [System.Xml.Serialization.XmlArrayAttribute]
    [System.Xml.Serialization.XmlArrayItemAttribute("property", IsNullable = false)]
    public List<propertyType> properties
    {
        get
        {
            return this.propertiesField;
        }
        set
        {
            this.propertiesField = value;
        }
    }

    [System.Xml.Serialization.XmlArrayAttribute]
    [System.Xml.Serialization.XmlArrayItemAttribute("element", IsNullable = false)]
    public List<ElementType> elements
    {
        get
        {
            return this.elementsField;
        }
        set
        {
            this.elementsField = value;
        }
    }

    [System.Xml.Serialization.XmlArrayAttribute]
    [System.Xml.Serialization.XmlArrayItemAttribute("relationship", IsNullable = false)]
    public List<RelationshipType> relationships
    {
        get
        {
            return this.relationshipsField;
        }
        set
        {
            this.relationshipsField = value;
        }
    }

    [System.Xml.Serialization.XmlArrayAttribute]
    [System.Xml.Serialization.XmlArrayItemAttribute("item", typeof(OrganizationType), IsNullable = false)]
    public List<OrganizationType> organizations
    {
        get
        {
            return this.organizationsField;
        }
        set
        {
            this.organizationsField = value;
        }
    }

    [System.Xml.Serialization.XmlArrayAttribute]
    [System.Xml.Serialization.XmlArrayItemAttribute("propertyDefinition", IsNullable = false)]
    public List<PropertyDefinitionType> propertyDefinitions
    {
        get
        {
            return this.propertyDefinitionsField;
        }
        set
        {
            this.propertyDefinitionsField = value;
        }
    }

    [System.Xml.Serialization.XmlElementAttribute]
    public ViewsType views { get; set; }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string version { get; set; }
}
 public abstract partial class ReferenceableType
{

    private List<LangStringType> nameField;

    private List<PreservedLangStringType> documentationField;

    private List<System.Xml.XmlElement> anyField;

    private List<System.Xml.XmlAttribute> anyAttrField;

    [System.Xml.Serialization.XmlAttributeAttribute(DataType = "ID")]
    public string identifier { get; set; }

    
    [System.Xml.Serialization.XmlElementAttribute(DataType = "token")]
    public string type { get; set; }

    [System.Xml.Serialization.XmlElementAttribute("name")]
    public List<LangStringType> name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }

    [System.Xml.Serialization.XmlElementAttribute("documentation")]
    public List<PreservedLangStringType> documentation
    {
        get
        {
            return this.documentationField;
        }
        set
        {
            this.documentationField = value;
        }
    }

    [System.Xml.Serialization.XmlAnyElementAttribute]
    public List<System.Xml.XmlElement> Any
    {
        get
        {
            return this.anyField;
        }
        set
        {
            this.anyField = value;
        }
    }

    [System.Xml.Serialization.XmlAnyAttributeAttribute()]
    public List<System.Xml.XmlAttribute> AnyAttr
    {
        get
        {
            return this.anyAttrField;
        }
        set
        {
            this.anyAttrField = value;
        }
    }
}

我在ReferenceableType中声明了我想要的类型。在那里也声明了出现在xml中的名称。

0 个答案:

没有答案