11.  XML

11.1 Introducción

11.1.1 Definición

Qué es XML ?

Extensible Markup Language XML 1.0 1998
Es un subconjunto de SGML (Standard Generalized Markup Language)
XML es un lenguaje de marcado basado en texto
Estándar para el intercambio de datos en la web
Conjunto de reglas para el diseño semántico de tags
Lenguaje de Meta-markup para definir otros lenguajes
La especificación oficial la da el W3C
http://www.w3.org/TR/REC-xml

HTML y XML


XML Archivo Ejemplo

   <?xml version="1.0"?>
<dining-room>
<manufacturer>The Wood Shop</manufacturer>
<table type="round" wood="maple">
<price>$199.99</price>
</table>
<chair wood="maple">
<quantity>6</quantity>
<price>$39.99</price>
</chair>
</dining-room>

XML describe Estructura y Semántica, NO Formato

Ejemplo HTML

   <DL>
<DT>Mambo
<DD>by Enrique Garcia
</DL>
<UL>
<LI>Producer: Enrique Garcia
<LI>Publisher: Sony Music Entertainment
<LI>Length: 3:46
<LI>Written: 1991
<LI>Artist: Azucar Moreno
</UL>
Ejemplo XML
   <SONG>
<TITLE>Mambo</TITLE>
<COMPOSER>Enrique Garcia</COMPOSER>
<PRODUCER>Enrique Garcia</PRODUCER>
<PUBLISHER>Sony Music Entertainment</PUBLISHER>
<LENGTH>3:46</LENGTH>
<YEAR>1991</YEAR>
<ARTIST>Azucar Moreno</ARTIST>
</SONG>

Por qué es conveniente usar XML?

Facilita el Intercambio de Datos

Hacer lenguajes a la medida de las necesidades

Datos auto-descriptivos
   <?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>
<GREETING>Hello from XML</GREETING>
<MESSAGE>Welcome to Programing XML in Java</MESSAGE>
</DOCUMENT>

Datos organizados (semi-estructurados) e integrados
   <?xml version="1.0"?>
<SCHOOL>
<CLASS type="seminar">
<CLASS_TITLE>XML In The Real World</CLASS_TITLE>
<CLASS_NUMBER>6.031</CLASS_NUMBER>
<SUBJECT>XML</SUBJECT>
<START_DATE>6/1/2002</START_DATE>
<STUDENTS>
<STUDENT status="attending">
<FIRST_NAME>Edward</FIRST_NAME>
<LAST_NAME>Samson</LAST_NAME>
</STUDENT>
<STUDENT status="withdrawn">
<FIRST_NAME>Ernestine</FIRST_NAME>
<LAST_NAME>Johnson</LAST_NAME>
</STUDENT>
</STUDENTS>
</CLASS>
</SCHOOL>

11.1.2 Tipos de Documentos XML

Documentos XML Bien-Formados (Well-Formed)

Son aquellos documentos XML que:

Documentos XML Válidos (Valid)

     <?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/css" href="first.css"?>
<!DOCTYPE DOCUMENT [
<!ELEMENT DOCUMENT (GREETING, MESSAGE)>
<!ELEMENT GREETING (#PCDATA)>
<!ELEMENT MESSAGE (#PCDATA)>
]>


<DOCUMENT>
<GREETING>Hello from XML</GREETING>
<MESSAGE>Welcome to Programing XML in Java</MESSAGE>
</DOCUMENT>


Tecnologías Relacionadas

Hypertext Markup Language

Cascading Style Sheets (CSS)

 

URLs and URIs

 

The Unicode Character Set





11.1.3 Utilización de documentos XML

Cómo se utiliza ?

XML Flowchart

Para hacer este "parsing" se utilizan 2 técnicas


Simple API for XML - SAX

Document Object Model - DOM

Sun's Java API for XML Parsing - JAXP

Java y XML: La pareja ideal

El ciclo original de un documento XML

Life of an XML Document

Editores de XML

Sirven para crear documentos XML

Editores de XML (XML Spy)

XML Spy Screenshot

 

XM Spy Grid Screenshot

XML Parsers

  1. Leen el documento XML
  2. Verifican si el documento XML está bien-formado (well formed)
  3. Verifican si el documento es válido

XML Validators

Unicamente verifican si un documento es válido

XML Validator Screenshot

XML Browsers

Despliegan los datos al usuario

Recursos disponibles en el web acerca de XML

Aplicaciones XML

Lenguajes basados en XML




11.2 Documentos XML

 

11.2.1 Creando Documentos XML


Documentos XML

Qué es un documento XML bien-formado ?

Es un documento de texto está bien-formado si:

  1. tomado como un todo, coinciden las etiquetas del documento
  2. Cumple con las reglas de formación mencionadas en la especificación:
    http://www.w3.org/TR/REC-xml
  3. Cada una de las entidades analizadas o parseadas que se encuentran referenciadas directa o indirectamente en el documento están bien formadas

Componentes de un documento XML

document ::= prolog element Misc*

Tags y Elements

Attributes

 

Elements vs Attributes

    Ejemplo 1:

<CUSTOMER LAST_NAME="Smith" FIRST_NAME="Sam"
DATE="October 15, 2001" PURCHASE="Tomatoes"
PRICE="$1.25" NUMBER="8" />



Ejemplo 2:

<CUSTOMER>
<NAME>
<LAST_NAME>Smith</LAST_NAME>
<FIRST_NAME>Sam</FIRST_NAME>
</NAME>
<DATE>October 15, 2001</DATE>
<ORDERS>
<ITEM>
<PRODUCT>Tomatoes</PRODUCT>
<NUMBER>8</NUMBER>
<PRICE>$1.25</PRICE>
</ITEM>
</ORDERS>
</CUSTOMER>

Construyendo la estructura de un Documento XML Bien-Formado

Debe comenzar con una declaración
<?xml version="1.0" standalone="yes"?>

 
Debe incluir uno o más elementos
<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>
<GREETING>Hello from XML</GREETING>
<MESSAGE>Welcome to Programing XML in Java</MESSAGE>
</DOCUMENT>

 

Incluir ambos tags (inicio y terminación) para aquellos elementos que no están vacíos
<GREETING>Hello from XML</GREETING>

Cerrar los tags vacíos con />
<SUBJECT name="XML"/>

Se debe establecer un elemento raíz que debe contener a todos los demás elementos
<DOCUMENT> ..... </DOCUMENT>

 

Mezclar los elementos de manera correcta:
<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>
<GREETING>Hello from XML</MESSAGE> <--- error end tag
<MESSAGE>
Welcome to Programing XML in Java
</GREETING> <--- error end tag
</DOCUMENT>

 

Utilizar nombres únicos para los atributos:
<PERSON LAST_NAME="Smith" LAST_NAME="Punin"> <--- error mismo nombre

Utilizar las 5 referencias ya existentes de entidades:
&amp;  & 
&lt; <
&gt; >
&apos; `
&quot; "

<TOUR CAPTION="The S&amp;O Railway"/>

 

Encerrar los valores de atributos con comillas:
<IMG SRC="image.jpg"/>




CDATA Sections

 

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Using The if Statement In JavaScript</title>
</head>
<body>
<script language="javascript">
<![CDATA[
var budget
budget = 234.77
if (budget < 0){
document.writeln("Uh oh.")}
]]>
</script>
<center> <h1>Using The if Statement In JavaScript</h1> </center>
</body>
</html>


11.2.2 Creación de DTDs (Document Type Definitions)

Document Type Definition:
Especifica la estructura y sintáxis de un documento XML
<!ELEMENT DOCUMENT (CUSTOMER)*>                 
<!ELEMENT CUSTOMER (NAME,DATE,ORDERS)>
<!ELEMENT NAME (LAST_NAME,FIRST_NAME)>
<!ELEMENT LAST_NAME (#PCDATA)>
<!ELEMENT FIRST_NAME (#PCDATA)>
<!ELEMENT DATE (#PCDATA)>
<!ELEMENT ORDERS (ITEM)*>
<!ELEMENT ITEM (PRODUCT,NUMBER,PRICE)>
<!ELEMENT PRODUCT (#PCDATA)>
<!ELEMENT NUMBER (#PCDATA)>
<!ELEMENT PRICE (#PCDATA)>

Mini Ejemplo de un documento válido y un DTD

<?xml version="1.0"?>
<!DOCTYPE BOOK [
<!ELEMENT BOOK (P*)>
<!ELEMENT P (#PCDATA)>
]>

<BOOK>
<P>chapter 1 - Intro</P>
<P>chapter 2 - Conclusion</P>
<P>Index</P>
</BOOK>

Sintáxis de DTDs

Document Type Declaration (declaración del DTD)

Ejemplo de DTD

<?xml version="1.0"?>
<!DOCTYPE BOOK [
<!ELEMENT p (#PCDATA)>
<!ELEMENT BOOK (OPENER,SUBTITLE?,INTRODUCTION?,(SECTION | PART)+)>
<!ELEMENT OPENER (TITLE_TEXT)*>
<!ELEMENT TITLE_TEXT (#PCDATA)>
<!ELEMENT SUBTITLE (#PCDATA)>
<!ELEMENT INTRODUCTION (HEADER, p+)+>
<!ELEMENT PART (HEADER, CHAPTER+)>
<!ELEMENT SECTION (HEADER, p+)>
<!ELEMENT HEADER (#PCDATA)>
<!ELEMENT CHAPTER (CHAPTER_NUMBER, CHAPTER_TEXT)>
<!ELEMENT CHAPTER_NUMBER (#PCDATA)>
<!ELEMENT CHAPTER_TEXT (p)+>
]>
<BOOK>
<OPENER>
<TITLE_TEXT>All About Me</TITLE_TEXT>
</OPENER>
<PART>
<HEADER>Welcome To My Book</HEADER>
<CHAPTER>
<CHAPTER_NUMBER>CHAPTER 1</CHAPTER_NUMBER>
<CHAPTER_TEXT>
<p>Glad you want to hear about me.</p>
<p>There's so much to say!</p>
<p>Where should we start?</p>
<p>How about more about me?</p>
</CHAPTER_TEXT>
</CHAPTER>
</PART>
</BOOK>

External DTDs (Privates)

External DTDs (Publics)

<!DOCTYPE name PUBLIC "FPI" "URL">

Formal Public Identifier (FPI)

<!DOCTYPE BOOK PUBLIC "-//Joseph Smith//General Book Version 5.3//EN" "http://www.library.org/book.dtd">

XHTML Document

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Virtual Library</title>
</head>
<body>
<p>Moved to <a href="http://vlib.org/">vlib.org</a>.</p>
</body>
</html>

Definición de Elementos

<!ELEMENT name content_model>

Ejemplos :

Content_model

Múltiples Hijos

Sequences

<!ELEMENT Name (Last_Name, First_Name)
  <Name>
<Last_Name>Punin</Last_Name>
<First_Name>John</First_Name>
</Name>

Choices

<!ELEMENT ITEM (PRODUCT, NUMBER, (PRICE | CHARGEACCT | SAMPLE))>
  <ITEM>
<PRODUCT>Tomatoes</PRODUCT>
<NUMBER>8</NUMBER>
<PRICE>$1.25</PRICE>
</ITEM>
<ITEM>
<PRODUCT>Oranges</PRODUCT>
<NUMBER>24</NUMBER>
<SAMPLE>$4.98</SAMPLE>
</ITEM>

Mixed Content

Ejemplo 1:
<!ELEMENT PRODUCT (#PCDATA | PRODUCT_ID)*>                    

<PRODUCT>Tomatoes</PRODUCT>

<PRODUCT>
<PRODUCT_ID>12411.95411.02121</PRODUCT_ID>
</PRODUCT>
Ejemplo 2:
<!ELEMENT p (#PCDATA | b)*>
<!ELEMENT b (#PCDATA)>

<p>This is <b>bold</b> text</p>

Elementos Vacíos & Comentarios en DTDs

Elementos Vacíos:
   <!ELEMENT CREDIT_WARNING EMPTY>

<CREDIT_WARNING></CREDIT_WARNING> or
<CREDIT_WARNING/>

Comentarios DTD:

   <!-- DOCUMENT is the root element -->
<!ELEMENT DOCUMENT (CUSTOMER)*>



Attributes

Declarando atributos en DTDs

<!ATTLIST element_name
attribute_name type default_value
attribute_name type default_value
.
.
.
attribute_name type default_value>
<!ELEMENT greeting (#PCDATA)>
<!ATTLIST greeting
language CDATA "English">

<greeting language="Spanish">
Hola!
</greeting>

Tipos de Atributos

     <!ELEMENT Rectangle EMPTY>
<!ATTLIST Rectangle
length CDATA "0px"
width CDATA "0px">

<Rectangle width="11.px" length="40px"/>

Valores por default para CDATA

  • #REQUIRED
    No hay un default, pero el atributo del documento debe indicar algun valor necesariamente
     <!ELEMENT img EMPTY>
<!ATTLIST img
alt CDATA #REQUIRED
src CDATA #REQUIRED>

<img src="xmlj.jpg" alt="XMLJ Image"/>

  • #IMPLIED
    No hay un default, y el atributo del documento no necesariamente tiene que utilizarse
     <!ELEMENT img EMPTY>
<!ATTLIST img
alt CDATA #REQUIRED
src CDATA #REQUIRED
width CDATA #IMPLIED
height CDATA #IMPLIED>

<img src="xmlj.jpg" alt="XMLJ Image" width="300"/>

  • #FIXED VALUE
    Se fija el valor del atributo al valor "VALUE".
     <!ELEMENT ADDRESS (#PCDATA)>
<!ATTLIST ADDRESS
country CDATA #FIXED "USA">

<ADDRESS country="USA">123 15th St. Troy NY 12111.</ADDRESS>
     <!ELEMENT TITLE (#PCDATA)>
<!ATTLIST TITLE
ALIGN (LEFT | CENTER | RIGHT) "LEFT">


<TITLE>Programming XML in Java</TITLE>

<TITLE ALIGN="CENTER">Programming XML in Java</TITLE>

     <!ELEMENT student_name (#PCDATA)>
<!ATTLIST student_name student_no NMTOKEN #REQUIRED>


<student_name student_no="9216735">Jo Smith</student_name>


<performances dates="27-02-1977 04-11-1911. 24-12-2002">
    Kat and the Kings
</performances>


     <!ELEMENT student_name (#PCDATA)>
<!ATTLIST student_name student_id ID #REQUIRED>


<student_name student_id="S9216735">Jo Smith</student_name>

 

<?xml version="1.0" standalone="yes"?>
<!DOCTYPE lab_group [
<!ELEMENT lab_group (student_name)*>
<!ELEMENT student_name (#PCDATA)>
<!ATTLIST student_name student_id ID #REQUIRED
tutor IDREF #IMPLIED>
]>



<lab_group>
<student_name student_id="S11.0411.5">Alex Foo</student_name>
<student_name student_id="S9011133">Sarah Bar</student_name>
<student_name student_id="S9216735"
tutor="S11.0411.5">Jo Smith</student_name>
</lab_group>

Entidades (Entities)

Internal General Entities

<!ENTITY name definition>

Ejemplo 1:
   <!ELEMENT DATE (#PCDATA)>
<!ENTITY TODAY "February 7, 2001">


<DATE>&TODAY;</DATE>
Ejemplo 2:
   <!ENTITY NAME "John Punin">
<!ENTITY CNAME "&NAME; Palacios">

External General Entities

<!ENTITY name SYSTEM URI>
<!ENTITY name PUBLIC FPI URI>

Ejemplo:
   <!ELEMENT DATE (#PCDATA)>
<!ENTITY TODAY SYSTEM "date.xml">


<DATE>&TODAY;</DATE>

Predefined General Entity References

<!ELEMENT EMAIL (#PCDATA)>
<!ENTITY at_new "&#64;">

<EMAIL>carlos&at_new;mail.udlap.mx</EMAIL>

Parameter Entities

Ejemplo:

   <!ELEMENT CUSTOMER (NAME, DATE, ORDERS)>
<!ELEMENT BUYER (NAME, DATE, ORDERS)>
<!ELEMENT DISCOUNTER (NAME, DATE, ORDERS)>

<!ENTITY % record "(NAME, DATE, ORDERS)">
<!ELEMENT CUSTOMER %record;>
<!ELEMENT BUYER %record;>
<!ELEMENT DISCOUNTER %record;>



11.3 SAX

11.3.1 Simple API for XML

Qué es un un API basado en eventos (Event-Based)?

Cómo funciona SAX ?

     <?xml version="1.0"?>
<Name>
<Last_Name>Punin</Last_Name>
<First_Name>John</First_Name>
</Name>
     start document
start element: Name
start element: Last_Name
characters: Punin
end element: Last_Name
start element: First_Name
characters: John
end element: First_Name
end element: Name
end document

SAX2/Java Parsers y Drivers



11.3.2 Utilización de SAX

Problema:   Imprime una lista de los Elementos del siguiente documento XML:
   <?xml version="1.0"?> 
<course>
<name id="csci_2962">Programming XML in Java</name>
<teacher id="jp">
<name>John Punin</name>
</teacher>
<student id="js">
<name>John Smith</name>
<hw1>30</hw1>
<hw2>70</hw2>
<project>11.</project>
<final>11.</final>
</student>
<student id="gl">
<name>George Lucas</name>
<hw1>11.</hw1>
<hw2>90</hw2>
<project>100</project>
<final>40</final>
</student>
<student id="lr">
<name>Elizabeth Roberts</name>
<hw1>60</hw1>
<hw2>95</hw2>
<project>50</project>
<final>90</final>
</student>
</course>


DefaultHandler

SAXParser

     java.lang.Object
|
+--org.apache.xerces.framework.XMLParser
|
+--org.apache.xerces.parsers.SAXParser

Usando Xerces XML Parser 

   import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.parsers.SAXParser;

public class SaxNames extends DefaultHandler
{
public void startElement(String uri, String localName, String rawName,
Attributes attributes)
{
System.out.println("Element : " + localName);
}

public static void main(String[] args)
{
try{
SaxNames SAXHandler = new SaxNames();
SAXParser parser = new SAXParser();
parser.setContentHandler(SAXHandler);
parser.setErrorHandler(SAXHandler);
parser.parse(args[0]);
}
catch(Exception e){
e.printStackTrace(System.err);
}
}
}

Compilando y Corriendo el ejemplo

     Element : course
Element : name
Element : teacher
Element : student
Element : name
Element : hw1
Element : hw2
Element : project
Element : final
Element : student
Element : name
Element : hw1
Element : hw2
Element : project
Element : final
Element : student
Element : name
Element : hw1
Element : hw2
Element : project
Element : final

 

11.3.3 Utilización de SAX (2)

Problema:   Imprimir la lista de estudiantes, su promedio y el promedio del curso
   <?xml version="1.0"?> 
<course>
<name id="csci_2962">Programming XML in Java</name>
<teacher id="jp">
<name>John Punin</name>
</teacher>
<student id="js">
<name>John Smith</name>
<hw1>30</hw1>
<hw2>70</hw2>
<project>11.</project>
<final>11.</final>
</student>
<student id="gl">
<name>George Lucas</name>
<hw1>11.</hw1>
<hw2>90</hw2>
<project>100</project>
<final>40</final>
</student>
<student id="lr">
<name>Elizabeth Roberts</name>
<hw1>60</hw1>
<hw2>95</hw2>
<project>50</project>
<final>90</final>
</student>
</course>

Creando el SAXHandler y Parser

   import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.parsers.SAXParser;

class SaxGrades extends DefaultHandler
{
....



public static void main(String[] args)
{
try{
SaxGrades SAXHandler = new SaxGrades();
SAXParser parser = new SAXParser();
parser.setContentHandler(SAXHandler);
parser.setErrorHandler(SAXHandler);
parser.parse(args[0]);
}
catch(Exception e){
e.printStackTrace(System.err);
}
}
}

Manejado los eventos del startElement y endElement

   static float grades[][] = new float[100][5];
int gi = -1;
int nstudent = 0;

public void startElement(String uri, String localName, String rawName,
Attributes attributes)
{
if(localName.equals("hw1"))
gi = 0;
else if(localName.equals("hw2"))
gi = 1;
else if(localName.equals("project"))
gi = 2;
else if(localName.equals("final"))
gi = 3;
else if(localName.equals("student"))
nstudent++;
}

public void endElement(String uri, String localName, String rawName)
{
gi = -1;
}

Escribiendo los caracteres

   public void characters(char characters[], int start, int length)
{
String chData = (new String(characters, start, length)).trim();
if(chData.indexOf("\n") < 0 && chData.length() > 0) {
if(gi >= 0){
grades[nstudent-1][gi] = Integer.parseInt(chData);
}
}
}

Manejado el endDocument

   public void endDocument()
{
float Ave = 0;
int i = 0, j = 0;
System.out.println("Grades");
for(i = 0; i < nstudent ; i++){
float total = 0;
for(j = 0; j < 4; j++){
total += grades[i][j];
}
grades[i][4] = total/4;
Ave += grades[i][4];
System.out.println("Student " + i + "=" + grades[i][4]);
}
Ave /= nstudent;
System.out.println("Class Average = " + Ave);
}

Poniendo todo junto

   import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.parsers.SAXParser;

class SaxGrades extends DefaultHandler
{
static float grades[][] = new float[100][5];
int gi = -1;
int nstudent = 0;

public void startElement(String uri, String localName, String rawName,
Attributes attributes)
{
if(localName.equals("hw1"))
gi = 0;
else if(localName.equals("hw2"))
gi = 1;
else if(localName.equals("project"))
gi = 2;
else if(localName.equals("final"))
gi = 3;
else if(localName.equals("student"))
nstudent++;
}

public void endElement(String uri, String localName, String rawName)
{
gi = -1;
}

public void characters(char characters[], int start, int length)
{
String chData = (new String(characters, start,length)).trim();
if(chData.indexOf("\n") < 0 && chData.length() > 0){
if(gi >= 0){
grades[nstudent-1][gi] = Integer.parseInt(chData);
}
}
}

public void endDocument()
{
float Ave = 0;
int i = 0, j = 0;
System.out.println("Grades");
for(i = 0; i < nstudent ; i++){
float total = 0;
for(j = 0; j < 4; j++){
total += grades[i][j];
}
grades[i][4] = total/4;
Ave += grades[i][4];
System.out.println("Student " + i + "=" + grades[i][4]);
}
Ave /= nstudent;
System.out.println("Class Average =" + Ave);
}
}


public static void main(String[] args)
{
try{
SaxGrades SAXHandler = new SaxGrades();
SAXParser parser = new SAXParser();
parser.setContentHandler(SAXHandler);
parser.setErrorHandler(SAXHandler);
parser.parse(args[0]);
}
catch(Exception e){
e.printStackTrace(System.err);
}

}


Compilando y corriendo el ejemplo (SaxGrades)

     Grades
Student 0= 66.25
Student 1= 77.5
Student 2= 73.75
Class Average = 72.5



11.3.4 Utilización de SAX (empleando atributos)



Attributes Interface



int getIndex(String rawName)

    Gets the index of an attribute given a raw name

String getRawName(int index)

    Gets the attribute's raw name by index

String getValue(int index)

    Gets an attribute's value by index

String getValue(String rawName)

    Gets an attribute's value by raw name




Problema:   Calcular el área de cada una de las figuras:

     <?xml version="1.0"?> 
<figures>
<circle x="20" y="10" r="20"/>
<rectangle x="-3" y="4" w="5" h="36"/>
<ellipse x="-5" y="6" w="30" h="50"/>
<rectangle x="7" y="23" w="58" h="45"/>
<circle x="-2" y="5" r="35"/>
<ellipse x="-10" y="-8" w="45" h="30"/>
</figures>




   import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.parsers.SAXParser;

public class SaxFigures extends DefaultHandler
{
public void startElement(String uri, String localName, String rawName,
Attributes attributes)
{
if(localName.equals("circle"))
{
String sr = attributes.getValue("r");
float radius = Float.valueOf(sr).floatValue();
float area = (float)Math.PI*radius*radius;
System.out.println("Circle : Radius = " + radius +
" Area = " + area);
}
else if(localName.equals("ellipse"))
{
String sw = attributes.getValue("w");
String sh = attributes.getValue("h");
float width = Float.valueOf(sw).floatValue();
float height = Float.valueOf(sh).floatValue();
float area = (float)Math.PI*(width/2)*(height/2);
System.out.println("Ellipse : Width = " + width +
" Height = " + height +
" Area = " + area);
}
else if(localName.equals("rectangle"))
{
String sh = attributes.getValue("h");
String sw = attributes.getValue("w");
float width = Float.valueOf(sw).floatValue();
float height = Float.valueOf(sh).floatValue();
float area = width * height;
System.out.println("Rectangle : Width = " + width +
" Height = " + height +
" Area = " + area);
}
}

public static void main(String[] args)
{
try{
SaxFigures SAXHandler = new SaxFigures();
SAXParser parser = new SAXParser();
parser.setContentHandler(SAXHandler);
parser.setErrorHandler(SAXHandler);
parser.parse(args[0]);
}
catch(Exception e){
e.printStackTrace(System.err);
}
}
}


Compilando y corriendo el ejemplo (SaxFigures)



11.4   DOM


11.4.1 W3C Document Object Model (DOM)

 

Objetos del W3C XML DOM

Objetos relacionado a los nodos

Documento XML como un árbol de nodos

   <?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>
<GREETING>Hello from XML</GREETING>
<MESSAGE>Welcome to Programing XML in Java</MESSAGE>
</DOCUMENT>

XML Document as a Tree

Niveles de DOM

DOM XML Parsers



11.4.2 Utilización de DOM

Problema:   Desplegar el conteo de elementos  <students> del siguiente documento:

   <?xml version="1.0"?> 
<course>
<name id="csci_2962">Programming XML in Java</name>
<teacher id="jp">
<name>John Punin</name>
</teacher>
<student id="js">
<name>John Smith</name>
<hw1>30</hw1>
<hw2>70</hw2>
<project>11.</project>
<final>11.</final>
</student>
<student id="gl">
<name>George Lucas</name>
<hw1>11.</hw1>
<hw2>90</hw2>
<project>100</project>
<final>40</final>
</student>
<student id="lr">
<name>Elizabeth Roberts</name>
<hw1>60</hw1>
<hw2>95</hw2>
<project>50</project>
<final>90</final>
</student>
</course>


Clase DOMParser

     java.lang.Object
|
+--org.apache.xerces.framework.XMLParser
|
+--org.apache.xerces.parsers.DOMParser

 

Métodos de la interface Document

 

Métodos de la interface NodeList

 

Usando Xerces XML Parser 

   import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

public class DOMCountNames
{
public static void main(String[] args)
{
try{
DOMParser parser = new DOMParser();
parser.parse(args[0]);
Document doc = parser.getDocument();
NodeList nodelist = doc.getElementsByTagName("student");
System.out.println(args[0] + " has " + nodelist.getLength()
+ " <student> elements.");
}
catch(Exception e){
e.printStackTrace(System.err);
}
}
}

Compilando y corriendo el ejemplo DOMCountNames

     students.xml has 3 <student> elements.


11.4.3 Utilización de DOM (documentos enteros)

 Métodos de la interface Node

Node Types

 

Recorriendo el documento entero

Problema:   Desplegar una lista de los elementos del documento:

   <?xml version="1.0"?> 
<course>
<name id="csci_2962">Programming XML in Java</name>
<teacher id="jp">
<name>John Punin</name>
</teacher>
<student id="js">
<name>John Smith</name>
<hw1>30</hw1>
<hw2>70</hw2>
<project>11.</project>
<final>11.</final>
</student>
<student id="gl">
<name>George Lucas</name>
<hw1>11.</hw1>
<hw2>90</hw2>
<project>100</project>
<final>40</final>
</student>
<student id="lr">
<name>Elizabeth Roberts</name>
<hw1>60</hw1>
<hw2>95</hw2>
<project>50</project>
<final>90</final>
</student>
</course>

   import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

class DOMDisplayElements
{
public static void displayDocument(String uri)
{
try{
DOMParser parser = new DOMParser();
parser.parse(uri);
Document doc = parser.getDocument();
display_names(doc);
}
catch(Exception e){
e.printStackTrace(System.err);
}
}

public static void display_names(Node node)
{
.
.
.
}

public static void main(String[] args)
{
DOMDisplayElements.displayDocument(args[0]);
}
}

 

Handling Document Nodes:

   public static void display_names(Node node)
{
if(node == null) {
return;
}
int type = node.getNodeType();
switch (type) {
case Node.DOCUMENT_NODE: {
display_names(((Document)node).getDocumentElement());
break;
}
case Node.ELEMENT_NODE: {
.
.
.
break;
}
}
}

 

Handling Element Nodes:

   case Node.ELEMENT_NODE: {
System.out.println("Element : " + node.getNodeName());

NodeList childNodes = node.getChildNodes();
if(childNodes != null) {
int length = childNodes.getLength();
for (int loopIndex = 0; loopIndex < length ; loopIndex++)
{
display_names(childNodes.item(loopIndex));
}
}
break;
}

Poniendo todo junto

 import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

class DOMDisplayElements
{
public static void displayDocument(String uri)
{
try{
DOMParser parser = new DOMParser();
parser.parse(uri);
Document doc = parser.getDocument();
display_names(doc);
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}

public static void display_names(Node node)
{
if(node == null) {
return;
}
int type = node.getNodeType();
switch (type) {
case Node.DOCUMENT_NODE: {
display_names(((Document)node).getDocumentElement());
break;
}
case Node.ELEMENT_NODE: {
System.out.println("Element : " + node.getNodeName());

NodeList childNodes = node.getChildNodes();
if(childNodes != null) {
int length = childNodes.getLength();
for (int loopIndex = 0; loopIndex < length ; loopIndex++)
{
display_names(childNodes.item(loopIndex));
}
}
break;
}
}
}

public static void main(String[] args)
{
DOMDisplayElements.displayDocument(args[0]);
}
}

Compilando y corriendo el ejemplo DOMNameElements

   Element : course
Element : name
Element : teacher
Element : student
Element : name
Element : hw1
Element : hw2
Element : project
Element : final
Element : student
Element : name
Element : hw1
Element : hw2
Element : project
Element : final
Element : student
Element : name
Element : hw1
Element : hw2
Element : project
Element : final


11.4.4 Utilización de DOM (Manejando Nodes)

Problema:   Desplegar una lista de estudiantes, su promedio y el promedio del curso

   <?xml version="1.0"?> 
<course>
<name id="csci_2962">Programming XML in Java</name>
<teacher id="jp">
<name>John Punin</name>
</teacher>
<student id="js">
<name>John Smith</name>
<hw1>30</hw1>
<hw2>70</hw2>
<project>11.</project>
<final>11.</final>
</student>
<student id="gl">
<name>George Lucas</name>
<hw1>11.</hw1>
<hw2>90</hw2>
<project>100</project>
<final>40</final>
</student>
<student id="lr">
<name>Elizabeth Roberts</name>
<hw1>60</hw1>
<hw2>95</hw2>
<project>50</project>
<final>90</final>
</student>
</course>

Creando DOMParser y Document

   import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

class DOMGrades
{
public static void computeGrades(String uri)
{
try{
DOMParser parser = new DOMParser();
parser.parse(uri);
Document doc = parser.getDocument();
traverse_tree(doc);
compute_final_grades();
}
catch(Exception e){
e.printStackTrace(System.err);
}
}
.
.
.

public static void main(String[] args)
{
Grades.computeGrades(args[0]);
}
}

Manejando Document Node, Element Node y Text Node

   static float grades[][] = new float[100][5];
static int nstudent = 0;
static int gi = -1;

private static void traverse_tree(Node node)
{
if(node == null) {
return;
}
int type = node.getNodeType();
switch (type) {
case Node.DOCUMENT_NODE: {
traverse_tree(((Document)node).getDocumentElement());
break;
}
case Node.ELEMENT_NODE: {
String elementName = node.getNodeName();
gi = -1;
if(elementName.equals("hw1"))
gi = 0;
else if(elementName.equals("hw2"))
gi = 1;
else if(elementName.equals("project"))
gi = 2;
else if(elementName.equals("final"))
gi = 3;
else if(elementName.equals("student"))
nstudent++;

NodeList childNodes = node.getChildNodes();
if(childNodes != null) {
int length = childNodes.getLength();
for (int loopIndex = 0; loopIndex < length ; loopIndex++)
{
traverse_tree(childNodes.item(loopIndex));
}
}
break;
}
case Node.TEXT_NODE: {
String chData = node.getNodeValue().trim();
if(chData.indexOf("\n") < 0 && chData.length() > 0) {
if(gi >= 0)
grades[nstudent-1][gi] = Integer.parseInt(chData);
}
}
}
}

Calculando las calificaciones después de recorrer el árbol

   static float grades[][] = new float[100][5];
static int nstudent = 0;

private static void compute_final_grades()
{
float Ave = 0;
int i = 0, j = 0;
System.out.println("Grades");
for(i = 0; i < nstudent ; i++)
{
float total = 0;
for(j = 0; j < 4; j++) {
total += grades[i][j];
}
grades[i][4] = total/4;
Ave += grades[i][4];
System.out.println("Student " + i + "=" + grades[i][4]);
}
Ave /= nstudent;
System.out.println("Class Average =" + Ave);
}

Poniendo todo junto

   import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

class DOMGrades
{
static float grades[][] = new float[100][5];
static int nstudent = 0;
static int gi = -1;

public static void computeGrades(String uri)
{
try {
DOMParser parser = new DOMParser();
parser.parse(uri);
Document doc = parser.getDocument();
traverse_tree(doc);
compute_final_grades();
} catch (Exception e) {
e.printStackTrace(System.err);
}
}

private static void compute_final_grades()
{
float Ave = 0;
int i = 0, j = 0;
System.out.println("Grades");
for(i = 0; i < nstudent ; i++)
{
float total = 0;
for(j = 0; j < 4; j++) {
total += grades[i][j];
}
grades[i][4] = total/4;
Ave += grades[i][4];
System.out.println("Student " + i + "=" + grades[i][4]);
}
Ave /= nstudent;
System.out.println("Class Average =" + Ave);
}

private static void traverse_tree(Node node)
{
if(node == null) {
return;
}
int type = node.getNodeType();
switch (type) {
case Node.DOCUMENT_NODE: {
traverse_tree(((Document)node).getDocumentElement());
break;
}
case Node.ELEMENT_NODE: {
String elementName = node.getNodeName();
gi = -1;
if(elementName.equals("hw1"))
gi = 0;
else if(elementName.equals("hw2"))
gi = 1;
else if(elementName.equals("project"))
gi = 2;
else if(elementName.equals("final"))
gi = 3;
else if(elementName.equals("student"))
nstudent++;

NodeList childNodes = node.getChildNodes();
if(childNodes != null) {
int length = childNodes.getLength();
for (int loopIndex = 0; loopIndex < length ; loopIndex++)
{
traverse_tree(childNodes.item(loopIndex));
}
}
break;
}
case Node.TEXT_NODE: {
String chData = node.getNodeValue().trim();
if(chData.indexOf("\n") < 0 && chData.length() > 0) {
if(gi >= 0)
grades[nstudent-1][gi] = Integer.parseInt(chData);
}
}
}
}

public static void main(String[] args)
{
DOMGrades.computeGrades(args[0]);
}
}

Compilando y corriendo el ejemploDOMGrades

     Grades
Student 0=66.25
Student 1=77.5
Student 2=73.75
Class Average =72.5


11.4.5 Utilización de DOM (empleando atributos)

Attr Interface Methods:


NamedNodeMap Interface Methods:


Problema:   Calcular el área de las figuras

   <?xml version="1.0"?> 
<figures>
<circle x="20" y="10" r="20"/>
<rectangle x="-3" y="4" w="5" h="36"/>
<ellipse x="-5" y="6" w="30" h="50"/>
<rectangle x="7" y="23" w="58" h="45"/>
<circle x="-2" y="5" r="35"/>
<ellipse x="-10" y="-8" w="45" h="30"/>
</figures>
   case Node.ELEMENT_NODE: 
{
String elementName = node.getNodeName();
NamedNodeMap attrs = node.getAttributes();
if(elementName.equals("circle")) {
Attr attrib = (Attr)attrs.getNamedItem("r");
String sr = attrib.getValue();
float radius = Float.valueOf(sr).floatValue();
float area = (float)Math.PI*radius*radius;
System.out.println("Circle : Radius = " + radius +
" Area = " + area);
}
}

Poniendo todo junto

   import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;

class DOMFigures
{
public static void computeArea(String uri)
{
try{
DOMParser parser = new DOMParser();
parser.parse(uri);
Document doc = parser.getDocument();
traverse_tree(doc);
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}

private static void traverse_tree(Node node)
{
if(node == null) {
return;
}
int type = node.getNodeType();
switch (type) {
case Node.DOCUMENT_NODE: {
traverse_tree(((Document)node).getDocumentElement());
break;
}
case Node.ELEMENT_NODE: {
String elementName = node.getNodeName();
NamedNodeMap attrs = node.getAttributes();
if(elementName.equals("circle")) {
Attr attrib = (Attr)attrs.getNamedItem("r");
String sr = attrib.getValue();
float radius = Float.valueOf(sr).floatValue();
float area = (float)Math.PI*radius*radius;
System.out.println("Circle : Radius = " + radius + " Area = " + area);
}
else if(elementName.equals("rectangle")) {
Attr attrib = (Attr)attrs.getNamedItem("w");
String sw = attrib.getValue();
attrib = (Attr)attrs.getNamedItem("h");
String sh = attrib.getValue();
float width = Float.valueOf(sw).floatValue();
float height = Float.valueOf(sh).floatValue();
float area = width * height;
System.out.println("Rectangle : Width = " + width +
" Height = " + height + " Area = " + area);
}
else if(elementName.equals("ellipse")) {
Attr attrib = (Attr)attrs.getNamedItem("w");
String sw = attrib.getValue();
attrib = (Attr)attrs.getNamedItem("h");
String sh = attrib.getValue();
float width = Float.valueOf(sw).floatValue();
float height = Float.valueOf(sh).floatValue();
float area = (float)Math.PI*(width/2)*(height/2);
System.out.println("Ellipse : Width = " + width +
" Height = " + height + " Area = " + area);
}
NodeList childNodes = node.getChildNodes();
if(childNodes != null) {
int length = childNodes.getLength();
for (int loopIndex = 0; loopIndex < length ; loopIndex++)
{
traverse_tree(childNodes.item(loopIndex));
}
}
break;
}
}
}

public static void main(String[] args)
{
DOMFigures.computeArea(args[0]);
}
}

Compilando y corriendo el ejemploDOMFigures

     Circle : Radius = 20.0 Area = 1256.6371
Rectangle : Width = 5.0 Height = 36.0 Area = 111..0
Ellipse : Width = 30.0 Height = 50.0 Area = 11711.0973
Rectangle : Width = 511.0 Height = 45.0 Area = 2610.0
Circle : Radius = 35.0 Area = 311.11.4512
Ellipse : Width = 45.0 Height = 30.0 Area = 1060.211.6



11.5 XML Namespaces

11.5.1 XML Namespaces

 

11.5.2 Creando un Namespace

Documento XML sin namespaces

   <?xml version="1.0"?>
   <book>
      <title>Programming XML in Java</title>
   </book>

Documento XML con un namespace

   <?xml version="1.0"?>
   <!-- both namespace prefixes are available throughout -->
   <bk:book xmlns:bk='http://www.books.org/books'>
      <bk:title>Programing XML in Java</bk:title>
   </bk:book>

 

Documento XML con namespaces

   <?xml version="1.0"?>
   <!-- both namespace prefixes are available throughout -->
   <bk:book xmlns:bk='http://www.books.org/books'
            xmlns:isbn='urn:ISBN:0-395-36341-6'>
      <bk:title>Programing XML in Java</bk:title>
      <isbn:number>15611.91379</isbn:number>
   </bk:book>

 

Creando Namespaces locales

     <?xml version="1.0"?>
     <!-- both namespace prefixes are available throughout -->
     <bk:book xmlns:bk='http://www.books.org/books'>
        <bk:title>Programing XML in Java</bk:title>
        <isbn:number xmlns:isbn='urn:ISBN:0-395-36341-6'>
           15611.91379
        </isbn:number>
     </bk:book>

Default Namespaces

     <?xml version="1.0"?>
     <!-- both namespace prefixes are available throughout -->
     <book xmlns='http://www.books.org/books'>
        <title>Programing XML in Java<title>
        <isbn:number xmlns:isbn='urn:ISBN:0-395-36341-6'>
           15611.91379
        </isbn:number>
     </book>

Combining XHTML and MathML using namespaces

   <?xml version="1.0" encoding="UTF-8"?>
   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
             "DTD/xhtml1-strict.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   <head>
      <title>A Math Example</title>
   </head>
   <body>
   <p>The following is MathML markup:</p>
      <math xmlns="http://www.w3.org/19911.Math/MathML">
         <apply> 
         <log/>
         <logbase><cn>3</cn></logbase> 
         <ci> x </ci>
      </apply>
      </math>
   </body>
   </html>

Notas a cerca de los Atributos

Ningún tag debe contener 2 atributos con:

Ejemplo Válido:

   <?xml version="1.0"?>
   <RESERVATION xmlns="http://www.aeroline.com/reservations"
                xmlns:html="http://www.w3.org/1999/xhtml">
      <NAME html:class="largeSansSerif">Layman, A</NAME>
      <SEAT class="Y" html:class="largeMonotype">33B</SEAT>
      <html:a href='/cgi-bin/ResStatus'>Check Status</html:a>
      <DEPARTURE>1997-05-24T07:55:00+1</DEPARTURE>
   </RESERVATION>

Ejemplo Inválido:

   <!-- http://www.w3.org is bound to n1 and n2 -->
   <x xmlns:n1="http://www.w3.org" 
      xmlns:n2="http://www.w3.org" >
      <bad a="1"     a="2" />
      <bad n1:a="1"  n2:a="2" />
   </x>

 



11.6 XPath

11.6.1 XML Path

11.6.2 XPath axis

11.6.3 XPath Node tests

11.6.4 XPath Predicates

 

11.6.5 Ejemplo de XPath


11.6.6 Sintáxis abreviada de XPath

Expression

self::node()
parent::node()
child::childname
attribute::childname
/descendant-or-self::node()/

Abbreviation

.
..
childname
@childname
//

 

XPath Abbreviated Syntax Examples

 


11.7 Extensible Style Language Transformations (XSLT)

11.7.1 Qué es XSL?

 

11.7.2 Operaciones en árbol dentro de XSL

 

11.7.3 Usando XSLT Style Sheets

XML Document (students.xml)

   <?xml version="1.0"?> 
<course>
<name id="csci_2962">Programming XML in Java</name>
<teacher id="jp">John Punin</teacher>
<student id="js">
<name>John Smith</name>
<hw1>30</hw1>
<hw2>70</hw2>
<project>11.</project>
<final>11.</final>
</student>
<student id="gl">
<name>George Lucas</name>
<hw1>11.</hw1>
<hw2>90</hw2>
<project>100</project>
<final>40</final>
</student>
<student id="lr">

<name>Elizabeth Roberts</name>
<hw1>60</hw1>
<hw2>95</hw2>
<project>50</project>
<final>90</final>
</student>
</course>

XSLT Style Sheet (students.xsl)

   <?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="course">
<HTML>
<HEAD>
<TITLE>Name of students</TITLE>
</HEAD>
<BODY>
<xsl:apply-templates select="student"/>
</BODY>
</HTML>
</xsl:template>


<xsl:template match="student">
<P>
<xsl:value-of select="name"/>
</P>
</xsl:template>

</xsl:stylesheet>

Realizando la transformación

Nota: Algunos browsers no tienen asociada la extensión/mimetype xsl marcarán un error, la solución es nombrar la hoja de estilo con extensión xml.

 

11.7.4 Procesadores de XSLT

 

Utilizando el procesador Xalan

java -classpath "xalan.jar" org.apache.xalan.xslt.Process 
-IN students.xml -XSL students.xsl -OUT students.html

import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.StringWriter;
import java.io.IOException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import mx.udlap.ict.util.*;


public class SimpleTransform
{


public static void main(String[] args)
throws TransformerException, TransformerConfigurationException,
FileNotFoundException, IOException
{


TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer(new StreamSource("students.xsl"));


String out=new String();
StringWriter sw=new StringWriter();
transformer.transform(new StreamSource("students.xml"), new StreamResult(sw));
out=sw.toString();

}


}

Output File (students.html)

     <HTML>
<HEAD>
<TITLE>Name of students</TITLE>
</HEAD>
<BODY>
<P>John Smith</P>
<P>George Lucas</P>
<P>Elizabeth Roberts</P>
</BODY>
</HTML>

11.7.5 Creando XSLT Style Sheets

 

XSLT Style Sheets are well formed XML Documents

   <?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
.
.
.
</xsl:template>

</xsl:stylesheet>

La hoja de estilo más simple, sin hacer nada.

   <?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>
New XML Document
</TITLE>
</HEAD>
<BODY>
Programming XML in Java
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>

El elemento xsl:apply-templates

     <?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>Name of students</TITLE>
</HEAD>
<BODY>
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="course">
<xsl:apply-templates select="student"/>
</xsl:template>

<xsl:template match="student">
<P>
Student Data
</P>
</xsl:template>
</xsl:stylesheet>

El elemento xsl:value-of

Obtener el valor de los nodos y poder escribirlo en archivo de salida

   <?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>Name of students</TITLE>
</HEAD>
<BODY>
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="course">
<xsl:apply-templates select="student"/>
</xsl:template>

<xsl:template match="student">
<P>
<xsl:value-of select="name"/>
</P>
</xsl:template>
</xsl:stylesheet>

HTML output file:

   <HTML>
<HEAD>
<TITLE>Name of students</TITLE>
</HEAD>
<BODY>
<P>John Smith</P>
<P>George Lucas</P>
<P>Elizabeth Roberts</P>
</BODY>
</HTML>

Manejando selecciones múltiples con xsl:for-each

   <?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>Name of students</TITLE>
</HEAD>
<BODY>
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="course">
<xsl:apply-templates select="student"/>
</xsl:template>

<xsl:template match="student">
<xsl:for-each select="name">
<P> <xsl:value-of select="."/> </P>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Especificando patrones para el atributo "match"

Encontrando el nodo raíz:

   <xsl:template match="/">
...
</xsl:template>

Encontrando elementos:

   <xsl:template match="student">
...
</xsl:template>

Encontrando elementos hijos:

   <xsl:template match="course/student">
...
</xsl:template>

<xsl:template match="course/*/name">
...
</xsl:template>

Encontrando hijos descendientes:

   <xsl:template match="course//name">
...
</xsl:template>

Ejemplo:

XSL Style Sheet

   <?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>Names</TITLE>
</HEAD>
<BODY>
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="text()">
</xsl:template>

<xsl:template match="course/name">
<H2>
<xsl:value-of select="."/>
</H2>
</xsl:template>

<xsl:template match="course/*/name">
<H3>
<xsl:value-of select="."/>
</H3>
</xsl:template>
</xsl:stylesheet>


HTML output file:

   <HTML>
<HEAD>
<TITLE>Names</TITLE>
</HEAD>
<BODY>
<H2>Programming XML in Java</H2>
<H3>John Smith</H3>
<H3>George Lucas</H3>
<H3>Elizabeth Roberts</H3>
</BODY>
</HTML>

 

Encontrando atributos


Ejemplo:

XML Document Input (figures.xml)

   <?xml version="1.0"?> 
<figures>
<circle x="20" y="10" r="20"/>
<rectangle x="-3" y="4" w="5" h="36"/>
<ellipse x="-5" y="6" w="30" h="50"/>
<rectangle x="7" y="23" w="58" h="45"/>
<circle x="-2" y="5" r="35"/>
<ellipse x="-10" y="-8" w="45" h="30"/>
</figures>

XSL Style Sheet

   <?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<HTML>
<HEAD><TITLE>Figures</TITLE></HEAD>
<BODY>
<xsl:apply-templates/>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="figures">
<TABLE>
<TR><TD>X</TD><TD>Y</TD></TR>
<xsl:apply-templates select="circle"/>
</TABLE>
</xsl:template>

<xsl:template match="circle">
<TR> <TD><xsl:value-of select="@x"/></TD>
<TD><xsl:value-of select="@y"/></TD> </TR>
</xsl:template>
</xsl:stylesheet>

HTML output file:

   <HTML>
<HEAD>
<TITLE>Figures</TITLE>
</HEAD>
<BODY>
<TABLE>
<TR>
<TD>X</TD><TD>Y</TD>
</TR>
<TR>
<TD>20</TD><TD>10</TD>
</TR>
<TR>
<TD>-2</TD><TD>5</TD>
</TR>
</TABLE>
</BODY>
</HTML>

 

Matching Comments:

    <xsl:template match="comment()">
...
</xsl:template>

Matching Text Nodes:

    <xsl:template match="text()">
...
</xsl:template>

 

Using the OR operator:

Example:   Generar una lista de los nombres de estudiantes y las calificaciones de sus proyectos

XSL Style Sheet

   <?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="course">
<HTML>
<HEAD>
<TITLE>Projects Grades</TITLE>
</HEAD>
<BODY>
<TABLE>
<xsl:apply-templates select="student"/>
</TABLE>
</BODY>
</HTML>
</xsl:template>

<xsl:template match="text()">
</xsl:template>

<xsl:template match="student">
<TR><xsl:apply-templates/></TR>
</xsl:template>

<xsl:template match="name | project">
<TD><xsl:value-of select="."/></TD>
</xsl:template>
</xsl:stylesheet>

HTML output file:

   <HTML>
<HEAD>
<TITLE>Projects Grades</TITLE>
</HEAD>
<BODY>
<TABLE>
<TR>
<TD>John Smith</TD><TD>11.</TD>
</TR>
<TR>
<TD>George Lucas</TD><TD>100</TD>
</TR>
<TR>
<TD>Elizabeth Roberts</TD><TD>50</TD>
</TR>
</TABLE>
</BODY>
</HTML>

 

Validando con []


Example:   Nombre del estudiante con id 'js'

XSL Style Sheet:

   <?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="course">
<NAME>
<xsl:apply-templates select="student"/>
</NAME>
</xsl:template>

<xsl:template match="text()">
</xsl:template>

<xsl:template match="student[@id = 'js']">
<xsl:value-of select="name"/>
</xsl:template>
</xsl:stylesheet>

11.7.6 Reglas por default de XSLT

1.    <xsl:template match="/ | *">
<xsl:apply-templates/>
</xsl:template>

2. <xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>

3. <xsl:template match="@">
<xsl:value-of select="."/>
</xsl:template>

4. <xsl:template match="comment()"/>

5. <xsl:template match="processing-instruction()"/>


Style-sheet sin reglas:

   <?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
</xsl:stylesheet>


Output File (aplicado a students.xml):

<?xml version="1.0" encoding="UTF-8"?>

Programming XML in Java
John Punin

John Smith
30
70
11.
11.


George Lucas
11.
90
100
40


Elizabeth Roberts
60
95
50
90

11.7.7 Creando "templates" para atributos


XSL Style Sheet:

   <?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="course">
<STUDENTS>
<xsl:apply-templates select="student"/>
</STUDENTS>
</xsl:template>

<xsl:template match="student">
<STUDENT NAME="{name}"
HW1="{hw1}"
HW2="{hw2}"
PROJECT="{project}"
FINAL="{final}"/>
</xsl:template>
</xsl:stylesheet>

XML output file:

<?xml version="1.0" encoding="UTF-8"?>
<STUDENTS>
<STUDENT FINAL="11." PROJECT="11." HW2="70" HW1="30" NAME="John Smith"/>
<STUDENT FINAL="40" PROJECT="100" HW2="90" HW1="11." NAME="George Lucas"/>
<STUDENT FINAL="90" PROJECT="50" HW2="95" HW1="60" NAME="Elizabeth Roberts"/>
</STUDENTS>

11.7.11.Creando nuevos Elementos


XML document (animals.xml):

   <?xml version="1.0"?> 
<animals>
<animal name="dog" class="mammal" legs="4"/>
<animal name="shark" class="fish" legs="0"/>
<animal name="chicken" class="bird" legs="2"/>
</animals>

Transformarlo al XML document (pets.xml):

   <?xml version="1.0" encoding="UTF-8"?>
<pets>
<mammal>
<name>dog</name>
<legs>4</legs>
</mammal>
<fish>
<name>shark</name>
<legs>0</legs>
</fish>
<bird>
<name>chicken</name>
<legs>2</legs>
</bird>
</pets>

XSL Style Sheet:

   <?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="animals">
<pets>
<xsl:apply-templates select="animal"/>
</pets>
</xsl:template>

<xsl:template match="animal">
<xsl:element name="{@class}">
<name><xsl:value-of select="@name"/></name>
<legs><xsl:value-of select="@legs"/></legs>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

11.7.9 Creando nuevos Atributos


XML document (animals.xml):

   <?xml version="1.0"?> 
<animals>
<animal name="dog" class="mammal" legs="4"/>
<animal name="shark" class="fish" legs="0"/>
<animal name="chicken" class="bird" legs="2"/>
</animals>

Transformarlo al XML document (pets.xml):

   <?xml version="1.0" encoding="UTF-8"?>
<pets>
<mammal name="dog" legs="4"/>
<fish name="shark" legs="0"/>
<bird name="chicken" legs="2"/>
</pets>

XSL Style Sheet:

   <?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="animals">
<pets>
<xsl:apply-templates select="animal"/>
</pets>
</xsl:template>

<xsl:template match="animal">
<xsl:element name="{@class}">
<xsl:attribute name="name">
<xsl:value-of select="@name"/>
</xsl:attribute>
<xsl:attribute name="legs">
<xsl:value-of select="@legs"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

11.7.10 Copiando y ordenando nodos


Ejemplo: Ordenar los elementos "animal" por el numero de "legs" en (animals.xml)

XML Input file:

   <?xml version="1.0"?> 
<animals>
<animal name="dog" class="mammal" legs="4"/>
<animal name="shark" class="fish" legs="0"/>
<animal name="chicken" class="bird" legs="2"/>
</animals>

XML Output file:

   <?xml version="1.0"?> 
<animals>
<animal name="shark" class="fish" legs="0"/>
<animal name="chicken" class="bird" legs="2"/>
<animal name="dog" class="mammal" legs="4"/>
</animals>

XSL Style Sheet:

   <?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>

<xsl:template match="text()">
</xsl:template>

<xsl:template match="animals">
<animals>
<xsl:apply-templates>
<xsl:sort data-type="number" select="@legs"/>
</xsl:apply-templates>
</animals>
</xsl:template>

<xsl:template match="animal">
<xsl:copy>
<xsl:apply-templates select="* | @*"/>
</xsl:copy>

</xsl:template>

<xsl:template match="* | @*">
<xsl:copy>
<xsl:apply-templates select="* | @*"/>
</xsl:copy>

</xsl:template>
</xsl:stylesheet>

11.7.11 Usando "xsl:variable" y "text output" type

Problema:   Calcular el promedio de cada estudiante y el promedio del grupo

XML Input file (students.xml):

XSL Style Sheet :

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="course">
<xsl:apply-templates select="student"/>
total average="<xsl:value-of select="(sum(//hw1) +
sum(//hw2) +
sum(//final) +
sum(//project))
div (4*count(//student))"/>"

</xsl:template>

<xsl:template match="student">
<xsl:variable name="ave">
<xsl:value-of select="(hw1 + hw2 + project + final) div 4"/>
</xsl:variable>
Student name="<xsl:value-of select="name"/>"
average="<xsl:value-of select="$ave"/>"
</xsl:template>
</xsl:stylesheet>

Output file (grades.txt):

Student name="John Smith" 
average="66.25"
Student name="George Lucas"
average="77.5"
Student name="Elizabeth Roberts"
average="73.75"
total average="72.5"

11.7.12 Usando "xsl:if"

Problema:   Desplegar el nombre de los estudiantes cuyos promedios sean mayores que 70

XML Input file (students.xml):

XSL Style Sheet:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="course">
<xsl:apply-templates select="student"/>
</xsl:template>

<xsl:template match="student">
<xsl:variable name="ave">
<xsl:value-of select="(hw1 + hw2 + project + final) div 4"/>
</xsl:variable>
<xsl:if test="$ave > 70">
Student name="<xsl:value-of select="name"/>"
average="<xsl:value-of select="$ave"/>"
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Output file (grades70.txt):

Student name="George Lucas" 
average="77.5"

Student name="Elizabeth Roberts"
average="73.75"

11.7.13 Controlando el tipo de salida



11.8 XML Databases

11.8.1 Antecedentes

 

11.8.2 Definición

 

Hablar de documentos XML es hablar de 2 categorías

 

Para cada caso la manera de realizar una consulta es diferente:


Para los documentos centrados en los datos, podemos pensar en datos estructurados que pueden extraerse del documento e indexarse con alguna base de datos convencional

Por otro lado para los datos centrados en el documento el reto no es tan trivial ya que se pretende hacer consultas pero no solo sobre el contenido, sino también sobre la estructura del documento.

De modo que una base de datos XML es aquella que define un modelo lógico de un documento XML y almacena y recupera documentos de acuerdo a ese modelo.


11.8.3 Tipos de Bases de Datos XML

XML Enabled Databases (Bases de datos habilitadas para XML): son aquellas que desglosan la información de un documento XML en su correspondiente esquema relacional o de objetos

 

Product Developer License DB Type
Access 2002 Microsoft Commercial Relational
Cache InterSystems Corp. Commercial Multi-valued
DB2 IBM Commercial Relational
eXtremeDB McObject Commercial Navigational
FileMaker FileMaker Commercial FileMaker
FoxPro Microsoft Commercial Relational
Informix IBM Commercial Relational
Matisse Matisse Software Commercial Object-oriented
Objectivity/DB Objectivity Commercial Object-oriented
OpenInsight Revelation Software Commercial Multi-valued
Oracle 11., 9i Oracle Commercial Relational
SQL Server 2000 Microsoft Commercial Relational
Sybase ASE 12.5 Sybase Commercial Relational
Versant enJin Versant Corp. Commercial Object-oriented

 

 

XML Native Databases (Bases de datos nativas de XML): son aquellas que respetan la estructura del documento, se pueden hacer consultas sobre dicha estructura y es posible recuperar el documento tal como fue insertado originalmente.

Product Developer License DB Type
4Suite, 4Suite Server FourThought Open Source Object-oriented
Birdstep RDM XML Birdstep Commercial Object-oriented
Centor Interaction Server Centor Software Corp. Commercial Proprietary
Cerisent XQE Cerisent Commercial Proprietary(?)
Coherity XML Database Coherity Commercial Proprietary
DBDOM K. Ari Krupnikov Open Source Relational
dbXML dbXML Group Commercial Proprietary
DOM-Safe Ellipsis Commercial Proprietary
eXist Wolfgang Meier Open Source Relational
eXtc M/Gateway Developments Ltd. Commercial Multi-valued
eXtensible Information Server (XIS) eXcelon Corp. Commercial Object-oriented (ObjectStore). Relational and other data through Data Junction
GoXML DB XML Global Commercial Proprietary (Text-based)
Infonyte DB Infonyte Commercial Proprietary (Model-based)
Ipedo XML Database Ipedo Commercial Proprietary
Lore Stanford University Research Semi-structured
Lucid XML Data Manager Ludic'i.t. Commercial Proprietary
MindSuite XDB Wired Minds Commercial Object-oriented
Natix data ex machina Commercial File system(?)
Neocore XML Management System NeoCore Commercial Proprietary
ozone ozone-db.org Open Source Object-oriented
Sekaiju / Yggdrasill Media Fusion Commercial Proprietary
SQL/XML-IMDB QuiLogic Commercial Proprietary (native XML and relational)
Tamino Software AG Commercial Proprietary. Relational through ODBC.
TeraText DBS TeraText Solutions Commercial Proprietary
TEXTML Server IXIA, Inc. Commercial Proprietary (Text-based)
TigerLogic XDMS Raining Data Commercial Pick
TOTAL XML Cincom Commercial Object-relational?
Virtuoso OpenLink Software Commercial Proprietary. Relational through ODBC
XDBM Matthew Parry, Paul Sokolovsky Open Source Proprietary (Model-based)
XDB ZVON.org Open Source Relational (PostgreSQL only?)
X-Hive/DB X-Hive Corporation Commercial Object-oriented (Objectivity/DB). Relational through JDBC
Xindice Apache Software Foundation Open Source Proprietary (Model-based)
Xyleme Zone Server Xyleme SA Commercial Proprietary




11.8.4 Programando con una XML Database

Afortunadamente el grupo XMLDB ha propuesto un API que todos los proveedores deben implementar

Al igual que con otras aplicaciones el API simplemente define un conjunto de interfaces que cada proveedor implementa:

org.xmldb.api

Interfaces
DatabaseManager

org.xmldb.api.base

Interfaces
Collection
Configurable
Database
Resource
ResourceIterator
ResourceSet
Service

Classes
ErrorCodes

Exceptions
XMLDBException

org.xmldb.api.modules

Interfaces
BinaryResource
CollectionManagementService
TransactionService
XMLResource
XPathQueryService
XUpdateQueryService

Es importante recordar:

Recuperación de un documento
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.*;

public class RetrieveExample {
    public static void main(String args[]) throws Exception {
        String driver = "org.exist.xmldb.DatabaseImpl";
        Class cl = Class.forName(driver);			
        Database database = (Database)cl.newInstance();
        DatabaseManager.registerDatabase(database);
        database.setProperty("create-database", "true");
        
        Collection col = 
            DatabaseManager.getCollection("xmldb:exist:///db/thesis");
        col.setProperty("pretty", "true");
        col.setProperty("encoding", "ISO-11.59-1");
        XMLResource res = (XMLResource)col.getResource(args[0]);
        
        if(res == null) {
            System.err.println("could not retrieve document " 
                + args[0] + "!");
            return;
        }
        System.out.println((String)res.getContent());
    }
}
        

Imports:

import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.*;

El driver puede ser:

El  url puede ser

Ejecutando una consulta
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.*;

public class QueryExample {
    public static void main(String args[]) throws Exception {
        String driver = "exist.xmldb.DatabaseImpl";
        Class cl = Class.forName(driver);			
        Database database = (Database)cl.newInstance();
        database.setProperty("create-database", "true");
        DatabaseManager.registerDatabase(database);
        
        Collection col = 
            DatabaseManager.getCollection("xmldb:exist:///db/thesis");
        XPathQueryService service =
            (XPathQueryService) col.getService("XPathQueryService", "1.0");
        service.setProperty("pretty", "true");
        service.setProperty("encoding", "ISO-11.59-1");
                
        ResourceSet result = service.query(args[0]);
        ResourceIterator i = result.getIterator();
        while(i.hasMoreResources()) {
            Resource r = i.nextResource();
            System.out.println((String)r.getContent());
        }
    }
}
  

*Los resultados estarán expresados en XML

Agregando un documento
import org.xmldb.api.*;
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;

import org.exist.util.XMLUtil;

public class APIParse {

    public static void main(String args[]) throws Exception {

        String collection = args[0], file = args[1];
        
        if(collection.startsWith("/db"))
            // remove /db if specified
            collection = collection.substring(3);
            
        // initialize driver
        String driver = "org.exist.xmldb.DatabaseImpl";
        Class cl = Class.forName(driver);			
        Database database = (Database)cl.newInstance();
        database.setProperty("create-database", "true");
        DatabaseManager.registerDatabase(database);
        
        // try to get collection
        Collection col = 
            DatabaseManager.getCollection("xmldb:exist:///db" + collection);
        if(col == null) {
            // collection does not exist: get root collection and create it
            Collection root = DatabaseManager.getCollection("xmldb:exist:///db");
            CollectionManagementService mgtService = 
                (CollectionManagementService)
                    root.getService("CollectionManagementService", "1.0");
            col = mgtService.createCollection(collection);
        }
        // create new XMLResource; an id will be assigned to the new resource
        XMLResource document = (XMLResource)col.createResource(null, "XMLResource");
        
        File f = new File(file);
        if(!f.canRead())
            System.err.println("can't read file " + file);
        document.setContent(f);
        System.out.print("storing document " + document.getId() + "...");
        col.storeResource(document);
        System.out.println("ok.");
    }
}      
        


  
 


11.8.5 Utilizando una XML Database

Básicamente la utilización radica en los siguientes pasos:

 

 

11.9 Lo que no se vio de XML

 

Esta sección de XML tiene como punto centrar el aprovechar el lenguaje como medio de almacenamiento, pero XML está siendo utilizado para muchos otros fines

XSL-FO: XSL Formatting Objects, que permiten crear presentaciones para los documentos ej. PDF

XML-RPC: Remote Procedure Call en XML

<methodCall>

<methodName>sample.sumAndDifference</methodName>
<params>
<param><value><int>5</int></value></param>
<param><value><int>3</int></value></param>
</params>

</methodCall>

 

 

SOAP: un protocolo simple basado en XML que permite a aplicaciones intercambiar información a través de HTTP

SOAP request:

POST /InStock HTTP/1.1
Host: www.stock.org
Content-Type: application/soap+xml; charset=utf-8
Content-Length: nnn

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">


<soap:Body xmlns:m="http://www.stock.org/stock">


<m:GetStockPrice>
<m:StockName>IBM</m:StockName>
</m:GetStockPrice>


</soap:Body>


</soap:Envelope>

SOAP response:

HTTP/1.1 200 OK
Content-Type: application/soap; charset=utf-8
Content-Length: nnn
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.stock.org/stock">

<m:GetStockPriceResponse>

<m:Price>34.5</m:Price>

</m:GetStockPriceResponse>

</soap:Body>

</soap:Envelope>