XML Schema

2,907 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,907
On SlideShare
0
From Embeds
0
Number of Embeds
133
Actions
Shares
0
Downloads
99
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

XML Schema

  1. 1. XML Schema Carlos Castillo [email_address] Departamento de Ciencias de la Computacion Facultad de Ciencias Fisicas y Matematicas Universidad de Chile
  2. 2. “Data-oriented” XML <ul><li><?xml version=”1.0”?> </li></ul><ul><ul><li><invoice> </li></ul></ul><ul><ul><li><orderDate>1999-01-21</orderDate> </li></ul></ul><ul><ul><li><shipDate>1999-01-25</shipDate> </li></ul></ul><ul><ul><li><billingAddress> </li></ul></ul><ul><ul><li><name>Ashok Malhotra</name> </li></ul></ul><ul><ul><li><street>123 Microsoft Ave.</street> </li></ul></ul><ul><ul><li><city>Hawthorne</city> </li></ul></ul><ul><ul><li><state>NY</state> </li></ul></ul><ul><ul><li><zip>10532-0000</zip> </li></ul></ul><ul><ul><li></billingAddress> </li></ul></ul><ul><ul><li><voice>555-1234</voice> </li></ul></ul><ul><ul><li><fax>555-4321</fax> </li></ul></ul><ul><ul><li></invoice> </li></ul></ul>factura.xml
  3. 3. “Document-oriented” XML <ul><li><?xml version=”1.0”?> </li></ul><ul><ul><li><memo importance='high' </li></ul></ul><ul><ul><li>date='1999-03-23'> </li></ul></ul><ul><ul><li><from>Paul V. Biron</from> </li></ul></ul><ul><ul><li><to>Ashok Malhotra</to> </li></ul></ul><ul><ul><li><subject>Latest draft</subject> </li></ul></ul><ul><ul><li><body> </li></ul></ul><ul><ul><li>We need to discuss the latest </li></ul></ul><ul><ul><li>draft <emph>immediately</emph>. </li></ul></ul><ul><ul><li>Either email me at <email> </li></ul></ul><ul><ul><li>mailto:paul.v.biron@kp.org</email> </li></ul></ul><ul><ul><li>or call <phone>555-9876</phone> </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><ul><li></memo> </li></ul></ul>memo.xml
  4. 4. Propósito lenguaje schema <ul><li>Describir estructura </li></ul><ul><ul><li>Anidación </li></ul></ul><ul><ul><li>Multiplicidad </li></ul></ul><ul><ul><li>Ordenamiento </li></ul></ul><ul><li>Describir tipos </li></ul><ul><ul><li>Para velocidad operatoria </li></ul></ul><ul><ul><li>Para mejor almacenamiento </li></ul></ul><ul><ul><li>Para busquedas </li></ul></ul><ul><ul><li>Para ingreso de datos </li></ul></ul><ul><ul><li>Para detectar errores (ej.: numeros, etc.) </li></ul></ul>
  5. 5. Requerimientos Schema <ul><li>Fácil de usar </li></ul><ul><li>Expresivo </li></ul><ul><ul><li>Orientado a estructura (DTD) v/s restricciones (Schematron) </li></ul></ul><ul><li>Poder como mínimo emular razgos de las bases de datos relacionales </li></ul><ul><ul><li>Llaves primarias </li></ul></ul><ul><ul><li>Llaves secundarias </li></ul></ul><ul><ul><li>Tipos nativos de las BD relacionales </li></ul></ul>
  6. 6. Jerarquía de lenguajes
  7. 7. Características XML Schema <ul><li>Sintaxis en XML </li></ul><ul><li>Espacios de nombres, includes </li></ul><ul><li>45 tipos nativos </li></ul><ul><li>Tipos de usuario, Tipos complejos </li></ul><ul><li>Secuencias, Multiplicidad </li></ul><ul><li>Llaves primarias, unicidad </li></ul><ul><li>Llaves secundarias </li></ul>
  8. 8. Ejemplo simple XML Schema <?xml version=&quot;1.0&quot;?> <xsd: schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; xmlns=&quot;http://www.example.com&quot; targetNamespace=&quot;http://www.example.com&quot; elementFormDefault=&quot;qualified&quot;> <xsd: element name =&quot;note&quot;> <xsd: complexType > <xsd: sequence > <xsd:element name=&quot;to&quot; type =&quot;xsd:string&quot;/> <xsd:element name=&quot;from&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;heading&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;body&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> note.xsd
  9. 9. Ejemplo simple XML Schema + doc. <xsd:element name=&quot;note&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;to&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;from&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;heading&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;body&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element note.xsd <?xml version=”1.0”?> <note> < to >Juan</ to > < from >Miguel</ from > < heading >Llama a X</ heading > < body >X te busca urgente</ body > </note> note.xml
  10. 10. XML Schema v/s DTD, 1/2 <xsd:element name=&quot;note&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;to&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;from&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;heading&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;body&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element note.xsd <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> note.dtd
  11. 11. XML Schema v/s DTD, 2/2 <xsd:element name=&quot;note&quot;> <xsd:complexType> <xsd:sequence> <xsd:element name=&quot;to&quot; type=&quot;xsd:string&quot;/> <xsd:element name=&quot;priority&quot; type=&quot; xsd:integer &quot;/> <xsd:element name=&quot;heading&quot;> <xsd:simpleType> <xsd:restriction base=&quot;xsd:string&quot;> <xsd:maxLength value=&quot;10&quot;/> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name=&quot;body&quot; type=&quot;xsd:string&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> note2.xsd <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT priority ( #PCDATA )> <!ELEMENT heading ( #PCDATA )> <!ELEMENT body (#PCDATA)> note2.dtd
  12. 12. Orden de compra <?xml version=&quot;1.0&quot;?> <purchaseOrder orderDate=&quot;1999-10-20&quot;> <shipTo country=&quot;US&quot;> <name>Alice Smith</name> <street>12 Maple Street</street> <city>Mill Valley</city> <state>CA</state> <zip>90952</zip> </shipTo> <billTo country=&quot;US&quot;> <name>Robert Smith</name> <street>8 Oak Avenue</street> <city>Old Town</city> <state>PA</state> <zip>95819</zip> </billTo> <comment>Hurry, my lawn is going wild!</comment> <items> <item partNum=&quot;872-AA&quot;> < productName >Lawnmower</productName> <quantity>1</quantity> <USPrice>148.95</USPrice> <comment>Confirm this is electric</comment> </item> <item partNum=&quot;926-AA&quot;> <productName>Baby Monitor</productName> <quantity>1</quantity> <USPrice>39.98</USPrice> <shipDate>1999-05-21</shipDate> </item> </items> </purchaseOrder> orden_compra.xml
  13. 13. Orden de compra (tipos dato) <?xml version=&quot;1.0&quot;?> <purchaseOrder orderDate=&quot; 1999-10-20 &quot;> <shipTo country=&quot; US &quot;> <name>Alice Smith</name> <street>12 Maple Street</street> <city>Mill Valley</city> <state> CA </state> <zip> 90952 </zip> </shipTo> <billTo country=&quot;US&quot;> <name>Robert Smith</name> <street>8 Oak Avenue</street> <city>Old Town</city> <state>PA</state> <zip>95819</zip> </billTo> <comment>Hurry, my lawn is going wild!</comment> <items> <item partNum=&quot; 872-AA &quot;> < productName >Lawn mower</productName> <quantity> 2 </quantity> <USPrice> 148.95 </USPrice> <comment>Confirm this is electric</comment> </item> <item partNum=&quot;926-AA&quot;> <productName>Baby Monitor</productName> <quantity>1</quantity> <USPrice>39.98</USPrice> <shipDate>1999-05-21</shipDate> </item> </items> </purchaseOrder> orden_compra.xml
  14. 14. XML Schema 1/3 <schema xmlns=&quot; http://www.w3.org/2001/XMLSchema &quot;> <annotation> <documentation xml:lang=&quot;en&quot;> Purchase order schema for Example.com. Copyright 2000 Example.com. All rights reserved. </documentation> </annotation> <element name=&quot;purchaseOrder&quot; type=&quot;PurchaseOrderType&quot;/> <!-- Tipo complejo --> <complexType name=&quot;PurchaseOrderType&quot;> <sequence> <element name=&quot;shipTo&quot; type=&quot;USAddress&quot;/> <element name=&quot;billTo&quot; type=&quot;USAddress&quot;/> <element ref=&quot;comment&quot; minOccurs=&quot;0&quot;/> <element name=&quot;items&quot; type=&quot;Items&quot;/> </sequence> <attribute name=&quot;orderDate&quot; type=&quot;date&quot;/> </complexType>
  15. 15. XML Schema 2/3 <!-- Tipo complejo --> <complexType name=&quot;USAddress&quot;> <sequence> <element name=&quot;name&quot; type=&quot;string&quot;/> <element name=&quot;street&quot; type=&quot;string&quot;/> <element name=&quot;city&quot; type=&quot;string&quot;/> <element name=&quot;state&quot; type=&quot;string&quot;/> <element name=&quot;zip&quot; type=&quot;decimal&quot;/> </sequence> <attribute name=&quot;country&quot; type=&quot;NMTOKEN&quot; fixed=&quot;US&quot;/> </complexType> <!-- Tipo simple --> <simpleType name=&quot;SKU&quot;> <restriction base=&quot;string&quot;> <pattern value=&quot;d{3}-[A-Z]{2}&quot;/> </restriction> </simpleType> <!-- Elemento --> <element name=&quot;comment&quot; type=&quot;string&quot;/>
  16. 16. XML Schema Ej (3) <complexType name=&quot;Items&quot;> <sequence> <element name=&quot;item&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot;> <complexType> <sequence> <element name=&quot;productName&quot; type=&quot;string&quot;/> <element name=&quot;quantity&quot;> <simpleType> <restriction base=&quot;positiveInteger&quot;> <maxExclusive value=&quot;100&quot;/> </restriction> </simpleType> </element> <element name=&quot;USPrice&quot; type=&quot;decimal&quot;/> <element ref=&quot;comment&quot; minOccurs=&quot;0&quot;/> <element name=&quot;shipDate&quot; type=&quot;date&quot; minOccurs=&quot;0&quot;/> </sequence> <attribute name=&quot;partNum&quot; type=&quot;SKU&quot; use=&quot;required&quot;/> </complexType> </element> </sequence> </complexType> </schema>
  17. 17. Tipos de tipos de dato <ul><li>Singulares </li></ul><ul><ul><li>Primitivos </li></ul></ul><ul><ul><li>Derivados </li></ul></ul><ul><li>Listas </li></ul><ul><ul><li>Dependientes de un solo tipo </li></ul></ul><ul><li>Uniones </li></ul><ul><ul><li>Listas heterogeneas (varios tipos) </li></ul></ul>
  18. 18. Derivacion de tipos <ul><li>Por restriccion </li></ul><ul><ul><li>Tipico: string restringido a una regexp </li></ul></ul><ul><li>Por listado </li></ul><ul><li>Por union </li></ul><ul><ul><li>'Tipos complejos' </li></ul></ul>
  19. 19. Elemento, Compuesto, Attr <ul><li>Tipo complejo = compuesto tipos simples </li></ul><ul><li>ComplexType contiene elementos y atributos </li></ul><complexType name=&quot;USAddress&quot;> <sequence> <element name=&quot;name&quot; type=&quot;string&quot;/> <element name=&quot;street&quot; type=&quot;string&quot;/> <element name=&quot;city&quot; type=&quot;string&quot;/> <element name=&quot;state&quot; type=&quot;string&quot;/> <element name=&quot;zip&quot; type=&quot;decimal&quot;/> </sequence> <attribute name=&quot;country&quot; type=&quot;NMTOKEN&quot; fixed=&quot;US&quot;/> </complexType>
  20. 20. Restricciones multiplicidad <ul><li>Comment es opcional </li></ul><ul><ul><li>minOccurs default 1 </li></ul></ul><ul><ul><li>maxOccurs default 1 (“unbounded) </li></ul></ul><ul><li>Atributos </li></ul><ul><ul><li>use = (required, optional, prohibited) </li></ul></ul><ul><li>Valores fijos y default </li></ul><ul><ul><li>fixed = “...” </li></ul></ul><ul><ul><li>default = “...” </li></ul></ul><element ref=&quot;comment&quot; minOccurs=&quot;0&quot;/>
  21. 21. Tipos simples <ul><li>string </li></ul><ul><li>byte, unsignedByte (-1,126;0,255) </li></ul><ul><li>base64Binary (GpM7) </li></ul><ul><li>hexBinary (FF00EE) </li></ul><ul><li>integer, positiveInteger, negativeInteger </li></ul><ul><li>int, long, short, float, double, boolean </li></ul><ul><li>time, duration </li></ul><ul><li>Qname, anyURI </li></ul>
  22. 22. Nuevos tipos simples <simpleType name=”myInteger”> <restriction base=”integer”> <minInclusive value=”10000”/> <maxInclusive value=”99999”/> </restriction> </simpleType> <ul><li>Se definen como restricciones en base a otros tipos de dato simples </li></ul><ul><li>Pueden ser anónimos (ej.: quantity) </li></ul>
  23. 23. Nuevos tipos enumerados <simpleType name=”deptosIngenieria”> <restriction base=”string”> <enumeration value=”cc”/> <enumeration value=”ci”/> <enumeration value=”eh”/> <enumeration value=”ma”/> ... <enumeration value=”in”/> </restriction> </simpleType> <ul><li>Se definen por extensión </li></ul>
  24. 24. Tipo lista <simpleType name=”listDeptos”> <list itemType=”deptosIngenieria”/> </simpleType> <!-- Ejemplo --> <listado>in ma cc</listado> <ul><li>Soporte nativo para listas en elementos </li></ul>
  25. 25. Contenido mixed <ul><li>Mixed en este mundo significa texto plano y elementos </li></ul><element name=”body”> <complexType mixed=”true”> <element name=”b”/> </complexType> </element>
  26. 27. Otras características <ul><li>Soporte para anotaciones </li></ul><ul><li>Extensiones a sequence </li></ul><ul><ul><li>Choice </li></ul></ul><ul><li>Grupos de atributos compartidos </li></ul><ul><li>Espacios de nombres </li></ul><ul><li>Tipos globales, locales, scope de archivo </li></ul><ul><li>Tipos abstractos </li></ul><ul><ul><li>no pueden tener instancias, sólo subtipos </li></ul></ul>

×