Vés al contingut

Usuari:Jordibaucells/XML Schema

De la Viquipèdia, l'enciclopèdia lliure
XML Schema


Introducció[modifica]

  • Basat en XML.
  • Descriu l'estructura i la semàntica d'un document XML.
  • Conegut com a XSD(Xml Schema Definition).
  • XML Schema és un estàndard del W3C.

Què és un XML Schema?[modifica]

  • Propòsit: definir els blocs de construcció d'un document XML.
  • Defineix els elements que poden aparèixer a un document.
  • Defineix els atributs que poden aparèixer a un document.
  • Defineix quins elements fills pot tenir un element.
  • Defineix l'ordre dels elements fills.
  • Defineix la cardinalitat d'elements fills.
  • Defineix si un element és buit o pot incloure text.
  • Defineix tipus de dades d'elements i atributs.
  • Defineix valors per defecte i fix a elements i atributs.

Avantatges[modifica]

  • Permeten futures extensions per tenir mes potència.
  • Són rics i més útils que les DTD.
  • Estan escrits en XML.
  • Suporten tipus de dades.
  • Suporten espais de nom.
  • Es el mateix llenguatge
  • Pots reutilitzar l'editor XML.
  • Pots reutilitzar el parser XML.
  • Pots manipular l'esquema amb XML DOM.
  • Pots transformar l'esquema amb XSLT.

Exemple simple[modifica]

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com" 
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Els atributs[modifica]

  • Un element amb atributs sempre és de tipus complexe.
  • Són de tipus bàsic, com els elements simples.
  • Sintaxi: <xs:attribute name="xxx" type="yyy"/>

- Valors per defecte:

<xs:attribute name="lang" type="xs:string" default="EN"/>

- Valors fixes:

<xs:attribute name="lang" type="xs:string" fixed="EN"/>

- Atributs opcionals:

<xs:attribute name="lang" type="xs:string" use="optional"/>

- Atributs obligatoris

<xs:attribute name="lang" type="xs:string" use="required"/>

Validació[modifica]

Exercici: Atributs Personals[modifica]

<?xml version="1.0" encoding="UTF-8"?>
<dp:schema xmlns="http://www.w3.org/2001/XMLSchema" 
	targetNamespace="http://www.example.org/dp" xmlns:tns="http://www.example.org/dp" 
	elementFormDefault="qualified" />
	<dp:element name="dp">
		<dp:complexType>
			<dp:sequence>
				<dp:element name="dp">
					<dp:complexeType>
						<dp:sequence>
						<dp:element name="Nombre" type="dp:string" />
						<dp:element name="Primer_Apellido" type="dp:string" />
						<dp:element name="Segundo_Apellido" type="dp:string" />
						<dp:element name="Edad" type="dp:integer" />
						<dp:element name="Dia_Nacimiento" type="dp:date" />
						<dp:element name="Pais" type="dp:string" />
						<dp:element name="Telefono" type="dp:integer" />
						<dp:element name="Correo_Electronico" type="dp:string" />
						</dp:sequence>
					</dp:complexeType>
				</dp:element>
					<dp:element name="dc">
						<dp:complexType>
							<dp:sequence>
							<dp:element name="Direccion" type="dp:string" />
							<dp:element name="Poblacion" type="dp:string" />
							<dp:element name="Codigo_Postal" type="dp:integer" />
							<dp:element name="Sexo" type="dp:string" />
							<dp:element name="Estado_Civil" type="dp:string" />
							</dp:sequence>
						</dp:complexType>
					</dp:element>
			</dp:sequence>
		</dp:complexType>
	</dp:element>
</dp:schema>

Exercici: Invoice[modifica]

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
	targetNamespace="http://www.example.org/Invoice" xmlns:tns="http://www.example.org/Invoice" 
	elementFormDefault="qualified">
		<element name="Invoice">
			<complexType>
				<sequence>
					<element name="Header">
						<complexType>
						<attribute name="invoiceNumber" type="integer" />
						</complexType>
					</element>
					<element name="Date">
						<complexType>
							<sequence>
							<element name="Month" type="date" />
							<element name="Day" type="date" />
							<element name="Year" type="date" />
							</sequence>
						</complexType>
					</element>
					<element name="Billto">
						<complexType>
							<attribute name="customer" type="ID" />
							<attribute name="name" type="string" />
							<attribute name="phone" type="integer" />
						</complexType>
					</element>	
					<element name="Address">
						<complexType>
							<sequence>
							<element name="street_1" type="string" />
							<element name="street_2" type="string" />
							<element name="city" type="string" />
							<element name="state" type="string" />
							<element name="zip" type="string" />
							<element name="country" type="string" />
							<element name="Item">
								<complexType>
								<attribute name="price" type="decimal" />
								<attribute name="discount" type="decimal" />
								</complexType>
							</element>
							</sequence>
						</complexType>
					</element>
				</sequence>
			</complexType>
		</element>
</schema>

EXÀMEN[modifica]

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.w3schools.com/examen" xmlns="http://www.w3schools.com/examen"
    elementFormDefault="qualified">
	<xsd:element name="socis">
		<xsd:complexType>
			<xsd:sequence>
				<xsd:element name="Socis">
					<xsd:complexType>
						<xsd:sequence>
							<xsd:element name="DNI" type="xsd:integer" />
							<xsd:element name="Nom" type="xsd:string" />
							<xsd:element name="Cognom_1" type="xsd:string" />
							<xsd:element name="Cognom_2" type="xsd:string" />
							<xsd:element name="Edat" type="xsd:integer" />
							<xsd:element name="Dia_Naixement" type="xsd:date" />
							<xsd:element name="Pais" type="xsd:string" />
							<xsd:element name="Telefon" type="xsd:integer" />
							<xsd:element name="Correu_Electronic" type="xsd:string" />
							<xsd:element name="Numero_Soci" type="xsd:string" />
							<xsd:element name="Sexe" type="xsd:date" />
						</xsd:sequence>
					</xsd:complexType>
				</xsd:element>
					<xsd:element name="Videoclubs">
						<xsd:complexType>
							<xsd:sequence>
								<xsd:element name="Nom" type="xsd:string" />
								<xsd:element name="Propietari" type="xsd:string" />
								<xsd:element name="Direccio" type="xsd:string" />
								<xsd:element name="Correu_Electronic" type="xsd:string" />
								<xsd:element name="Telefon" type="xsd:integer" />
								<xsd:element name="Horari" type="xsd:string" />
							</xsd:sequence>
						</xsd:complexType>
					</xsd:element>
						<xsd:element name="Pelicules">
							<xsd:complexType>
								<xsd:sequence>
									<xsd:element name="Nom" type="xsd:string" />
									<xsd:element name="Director" type="xsd:string" />
									<xsd:element name="Web" type="xsd:string" />
									<xsd:element name="Any_Creacio" type="xsd:date" />
									<xsd:element name="Duracio" type="xsd:string" />
									<xsd:element name="Pressupost" type="xsd:string" />
								</xsd:sequence>
						</xsd:complexType>
					</xsd:element>
			</xsd:sequence>
		</xsd:complexType>
	</xsd:element>
</xsd:schema>