XSD Element

Source: Internet
Author: User
Tags extend xmlns

xsd <any> xsd element substitution

the <anyAttribute> element gives us the ability to extend an XML document through attributes not defined by the schema. <anyAttribute> Elements

The <anyAttribute> element gives us the ability to extend an XML document through attributes not defined by the schema.

The following example is a fragment from an XML schema named "Family.xsd". It shows us a declaration for the "person" element. By using the <anyAttribute> element, we can add any number of attributes to the "person" element:

<xs:element name= "Person" >
  <xs:complexType>
    <xs:sequence>
      <xs:element name= " FirstName "type=" xs:string "/>
      <xs:element name=" LastName "type=" xs:string "/>
    </xs:sequence >
    <xs:anyAttribute/>
  </xs:complexType>
</xs:element>

Now, we want to extend the "person" element with the "gender" attribute. In this case we can do this even if the author of the schema never declares any "gender" attributes.

Please see this schema file, named "Attribute.xsd":

<?xml version= "1.0" encoding= "iso-8859-1"?> <xs:schema
xmlns:xs= "Http://www.w3.org/2001/XMLSchema"
targetnamespace= "http://www.w3school.com.cn"
xmlns= "http://www.w3school.com.cn
" elementformdefault= "qualified" >

<xs:attribute name= "Gender" >
  <xs:simpleType>
    <xs: Restriction base= "xs:string" >
      <xs:pattern value= "Male|female"/>
    </xs:restriction>
  </xs:simpleType>
</xs:attribute>

</xs:schema>

The following XML (named "Myfamily.xml") uses elements from different schemas, "family.xsd" and "attribute.xsd":

<?xml version= "1.0" encoding= "iso-8859-1"?>

<persons xmlns= "http://www.microsoft.com"
xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://www.microsoft.com family.xsd
http://www.w3school.com.cn attribute.xsd ">

<person gender=" female ">
<firstname>jane</ firstname>
<lastname>Smith</lastname>
</person>

<person gender= "Male" >
<firstname>David</firstname>
<lastname>Smith</lastname>
</person>

</persons>

The above XML file is valid because the schema "Family.xsd" allows us to add attributes to the "person" element.

<any> and <anyAttribute> can be used to make extensible documentation. They make the document capable of containing additional elements that are not declared in the main XML schema.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.