Tutorial XML

INICIO XML Introducción XML XML Cómo utilizar Árbol XML Sintaxis XML Elementos XML Atributos XML Espacios de nombres XML Pantalla XML Solicitud Http XML Analizador XML DOM XML XPath XML XML XSLT XQuery XML Enlace X XML Validador XML DTD XML Esquema XML Servidor XML Ejemplos XML Cuestionario XML Certificado XML

XML-AJAX

AJAX Introducción AJAX XMLHttp Solicitud AJAX Respuesta AJAX Archivo XML AJAX AJAXPHP Ajax ASP Base de datos AJAX Aplicaciones AJAX Ejemplos de AJAX

DOM XML

DOM Introducción Nodos DOM Acceso DOM Información del nodo DOM Lista de nodos DOM Atravesando DOM Navegación DOM DOM obtener valores Nodos de cambio de DOM Eliminar nodos DOM Nodos de reemplazo de DOM DOM Crear nodos Agregar nodos DOM Nodos de clonación de DOM Ejemplos de DOM

Tutorial XPath

Introducción a XPath Nodos XPath Sintaxis XPath Ejes XPath Operadores XPath Ejemplos de XPath

Tutorial XSLT

XSLT Introducción Idiomas XSL Transformación XSLT XSLT <plantilla> XSLT <valor-de> XSLT <para-cada> XSLT <ordenar> XSLT <si> XSLT <elegir> Aplicar XSLT XSLT en el cliente XSLT en el servidor XSLT Editar XML Ejemplos de XSLT

Tutorial de XQuery

Introducción a XQuery Ejemplo de XQuery XQuery FLWOR XQuery HTML Términos de XQuery Sintaxis XQuery XQuery Agregar Seleccionar XQuery Funciones XQuery

DTD XML

Introducción a DTD Bloques de construcción DTD Elementos DTD Atributos DTD Elementos DTD vs Attr Entidades DTD Ejemplos de DTD

Esquema XSD

XSD Introducción XSD Cómo XSD <esquema> Elementos XSD Atributos XSD Restricciones XSD

Complejo XSD

Elementos XSD XSD vacío Solo elementos XSD Solo texto XSD XSD mixto Indicadores XSD XSD <cualquiera> XSD <cualquieratributo> Sustitución XSD Ejemplo XSD

Datos XSD

Cadena XSD Fecha XSD XSD Numérico Miscelánea XSD Referencia XSD

Servicios Web

Servicios XML XML WSDL JABÓN XML XML RDF RSS XML

Referencias

Tipos de nodos DOM Nodo DOM Lista de nodos DOM DOM NamedNodeMap Documento DOM Elemento DOM Atributo DOM Texto DOM DOM CDATA Comentario DOM DOM XMLHttpSolicitud Analizador DOM Elementos XSLT Funciones XSLT/XPath

Aplicaciones XML


Este capítulo muestra algunas aplicaciones HTML que utilizan XML, HTTP, DOM y JavaScript.


El documento XML utilizado

En este capítulo utilizaremos el archivo XML denominado "cd_catalog.xml" .


Mostrar datos XML en una tabla HTML

Este ejemplo recorre cada elemento <CD> y muestra los valores de los elementos <ARTIST> y <TITLE> en una tabla HTML:

Ejemplo

<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
  border-collapse:collapse;
}
th, td {
  padding: 5px;
}
</style>
</head>
<body>

<button type="button" onclick="loadXMLDoc()">Get my CD collection</button>
<br><br>
<table id="demo"></table>

<script>
function loadXMLDoc() {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      myFunction(this);
    }
  };
  xmlhttp.open("GET", "cd_catalog.xml", true);
  xmlhttp.send();
}
function myFunction(xml) {
  var i;
  var xmlDoc = xml.responseXML;
  var table="<tr><th>Artist</th><th>Title</th></tr>";
  var x = xmlDoc.getElementsByTagName("CD");
  for (i = 0; i <x.length; i++) {
    table += "<tr><td>" +
    x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
    "</td><td>" +
    x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
    "</td></tr>";
  }
  document.getElementById("demo").innerHTML = table;
}
</script>

</body>
</html>

Para obtener más información sobre el uso de JavaScript y XML DOM, vaya a DOM Intro.



Mostrar el primer CD en un elemento HTML div

Este ejemplo usa una función para mostrar el primer elemento de CD en un elemento HTML con id="showCD":

Ejemplo

displayCD(0);

function displayCD(i) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            myFunction(this, i);
        }
    };
    xmlhttp.open("GET", "cd_catalog.xml", true);
    xmlhttp.send();
}

function myFunction(xml, i) {
    var xmlDoc = xml.responseXML;
    x = xmlDoc.getElementsByTagName("CD");
    document.getElementById("showCD").innerHTML =
    "Artist: " +
    x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
    "<br>Title: " +
    x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
    "<br>Year: " +
    x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue;
}

Navegar entre los CD

Para navegar entre los CD, en el ejemplo anterior, agregue una función siguiente() y anterior():

Ejemplo

function next() {
  // display the next CD, unless you are on the last CD
  if (i < x.length-1) {
    i++;
    displayCD(i);
  }
}

function previous() {
  // display the previous CD, unless you are on the first CD
  if (i > 0) {
  i--;
  displayCD(i);
  }
}

Mostrar información del álbum al hacer clic en un CD

El último ejemplo muestra cómo puede mostrar la información del álbum cuando el usuario hace clic en un CD:

Ejemplo

function displayCD(i) {
    document.getElementById("showCD").innerHTML =
    "Artist: " +
    x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
    "<br>Title: " +
    x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
    "<br>Year: " +
    x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue;
}