XML et bases de données Pierre LAGARDE Formation SANDRE
Données versus Documents
Documents centrés données / XML structuré Utilisé pour les échanges de données Construit pour l’analyse par l’ordinateur Caractéristiques : Structures régulières Données faible granularité Pas de contenu mixte L’ordre des balises n’a pas d’importance Exemples Données scientifiques …
Exemple de fichier XML data-centric XML-SANDRE
Document centré document/ unstructured XML Elaboré pour la lecture “humaine” Caractéristiques Structures irrégulières Données à granularité importante (un élément = le document !) Données mixtes L’ordre des élément est important Souvent ne provient pas d’une base de données Exemples Un livre Un document issu de Word ..
Principales solutions pour gérer du XML XML documents Files system BD XML native Application mapping XML DB BD XML- enabled Middleware XML BD Relationnel ou object
Stocker et retrouver des données
Solutions pour les données Mapping XML documents XML- enabled database database Middleware XML structures (XML schema) Database schema Mapper la structure XML Utilisation d’un langage XML Utilisation de XSLT pour transformer la structure XML structure dans une structure attendue par la base de données
Mapper les fichiers dans le schéma de la base Le mapping concerne Les éléments, attributs et textes Ignore la structure physique (CDATA sections, encoding,…) Ignore les structures logiques (processing instructions, comments, order,…) 2 approches du mapping Table-Based mapping Object-relational mapping Un ”import et un export” ne crée pas le même fichier (mais les mêmes données)
Table-based mapping Transférer les données dans les tables relationnelles <Parametre> <CdParametre>1340</CdParametre> <NomParametre>Nitrates</NomParametre> <StParametre>Validé</StParametre> <DateCreationParametre>1987-06-01</DateCreationParametre> <DateMajParametre>2004-12-20T00:00:00</DateMajParametre> <AuteurParametre>DIREN de bassin ILE-de-FRANCE et CENTRE </AuteurParametre> <LbCourtParametre>NO3-</LbCourtParametre> <LbLongParametre>Nitrates</LbLongParametre> <MethodeAnalyse> <CdMethode>24</CdMethode> <NomMethode>Essais des eaux – Dosage colorimétrique des ions nitrate NF T 90-012 - Février 1952)</NomMethode> </MethodeAnalyse> <CdMethode>25</CdMethode> <NomMethode>Essais des eaux - Dosage des nitrates (NF T 90-012 - Août 1975)</NomMethode> </Parametre> Parameter Methods
Table-based mapping avec Oracle En deux opérations Java OracleXML getXML –conn “jdbc:oracle:thin@localhost:1521:orcl” –user “system/admin” “select * from parametres” <database> <table> <row> <column1>...</column1> <column2>...</column2> ... </row> </table> </database> La structure XML XSLT OracleXML (XSU)
Demonstration with Oracle Need Oracle 8i, 9i or 10g Creates (or exists) a specific schema (relational) To show XML documents : Java OracleXML getXML –user “system/admin” “select * from parametres” To put XML Documents : Java OracleXML putXML –user “system/admin” -fileName “nvpara.xml” “parametres”
Object-Relational mapping Modélisation des données comme des arbres d’objets <Parametre> <CdParametre>1340</CdParametre> <NomParametre>Nitrates</NomParametre> <StParametre>Validé</StParametre> <DateCreationParametre>1987-06-01</DateCreationParametre> <DateMajParametre>2004-12-20T00:00:00</DateMajParametre> <AuteurParametre>DIREN de bassin ILE-de-FRANCE et CENTRE </AuteurParametre> <LbCourtParametre>NO3-</LbCourtParametre> <LbLongParametre>Nitrates</LbLongParametre> <MethodeAnalyse> <CdMethode>24</CdMethode> <NomMethode>Essais des eaux – Dosage colorimétrique des ions nitrate NF T 90-012 - Février 1952)</NomMethode> </MethodeAnalyse> <CdMethode>25</CdMethode> <NomMethode>Essais des eaux - Dosage des nitrates (NF T 90-012 - Août 1975)</NomMethode> </Parametre> XML document Objects view Database XML data binding Object/ relational
Object-Relational mapping dans Oracle 2 solutions Use Object view XMLType Demonstration of object view Demonstration of XMLTYPE… a native XML in Oracle ? Very complex to create…
Les langages de requêtes Permettre d’extraire des données XML d’une base de données Trois approches : Un développement spécifique Un langage SQL amélioré Un langage XML Query <Parametre> <CdParametre>1340</CdParametre> <NomParametre>Nitrates</NomParametre> <StParametre>Validé</StParametre> <DateCreationParametre>1987-06-01</DateCreationParametre> <DateMajParametre>2004-12-20T00:00:00</DateMajParametre> <AuteurParametre>DIREN de bassin ILE-de-FRANCE et CENTRE </AuteurParametre> <LbCourtParametre>NO3-</LbCourtParametre> <LbLongParametre>Nitrates</LbLongParametre> <MethodeAnalyse> <CdMethode>24</CdMethode> <NomMethode>Essais des eaux – Dosage colorimétrique des ions nitrate NF T 90-012 - Février 1952)</NomMethode> </MethodeAnalyse> <CdMethode>25</CdMethode> <NomMethode>Essais des eaux - Dosage des nitrates (NF T 90-012 - Août 1975)</NomMethode> </Parametre> Parameter Methods
Un langage SQL amélioré Utilisé le SQL avec de nouvelles commandes SQL / XML : une évolution de ANSI/ISO SQL 2003 Exemple : select XMLElement("Code",code_parametre) from parametres Select XMLElement("Parametre",XMLElement("CodeParametre",XMLAttributes('SANDRE' as "schemeAgency"),code_parametre),XMLElement("NomParametre",nom_parametre)) from parametres
Langage XML Query Un langage de programmation pour interroger des collections de données XML XQuery Développé par W3C (Working Draft in 2005) Utilisation XPATH Sources multiples Spécifique à XML
For …. Let ….Where …Return …. Exemples Principes XQUERY FLWR Expressions For …. Let ….Where …Return …. Exemples let $a := input()/SI_DC return <p>Actually, there are { count($a) } networks.</p> for $a in input()/SI_DC/Bdd where $a/TypeBdd/CdTypeBanque = '3‘ return $a for $a in input()/SI_DC/Bdd, $b in input()//BddRdd where $b/Bdd/CdBdd=$a/CdBdd return <Bdd>{$b/Bdd/CdBdd}{$a/LbBdd}</Bdd>
Principes XQUERY Tri Fonctions d’update Recherche de texte update replace input()/SI_DC/DispositifCollecte/MnRdd[.="RBESOUQAP"] with <MnRdd>test</MnRdd> for $a in input()//DispositifCollecte return $a/MnRdd Recherche de texte
Stocker et retrouver des documents
Principals solutions to store XML documents Files system Native XML database Application mapping XML DB XML- enabled database XML Middleware Relational database object-oriented database
Stockage Files system Très simple Outils comme grep pour accéder XML Pas très performant
Stocker les fichiers dans un BLOB XML documents BINARY Database schema XML documents (no structured) Mapper les documents XML dans un champ binaire : BLOB Utilisation des technologies full-text
Quelques produits sont are XML-aware Utilisation des BLOBs Avantages transactionnel Sécurité multiutilisateur Administration Quelques produits sont are XML-aware Elimine le problème des tags Compatible avec le langage XQUERY Oracle 9i+ Db2
Utilisation d’une base native XML XML documents XML native database XML documents (no structured) Une base de données optimisée pour le stockage XML Utilise uniquement des XML technologies : XPath, XQuery,…
Définition d’une base de données XML native Un terme marketing de Tamino et non une définition technique. Définition Un modèle logique pour des documents XML XML document = l’unité de base du stockage (= une ligne en BD relationnel) Pas de modèle particulier pour le stockage physique
Native XML Database Architecture Text-Based Native XML Database Store XML as text File system, BLOB, … Indexes of all text Speed advantage when retrieving entire documents Slow to find pieces of many documents Model-Based Native XML Database Store XML documents in an internal object model Performance similar to text-based Less slower to find pieces of many documents Similar to a hierarchical database
Features of Native XML Databases Document Collections Notion of collection (=table in an relational database) Hierarchy of collections like directories in an file system Query Languages Support one or more query languages XPath, XQuery,… Transactions, Locking and Concurrency Support transactions Locking at the level of entire documents In the future, locking at the level of elements,…
Features of Native XML Databases Application Programming Interface : API Offer programmatic APIs Proprietary APIs XQuery for Java Most XML Databases offer the ability to execute HTTP queries Round-tripping Store an XML document and get the “same” document back again Remote data / repository Some database include remote data and repository
Features of native XML Databases Indexes A way to increase query speed Structural indexes Full-text indexes External entity storage The problem to store external entities
An example : Tamino (Software AG) A server oriented native XML Database Features Proprietary Model-based storage Storage and search data with Query interpreter Based from XML Standards Connexion with XML softwares
An example : Tamino (Software AG) XML Engine Native XML storage Query processors XQuery X-Query (!) XML Datamap Metadata about XML documents Describes where the data in a given XML document is stored X-Tension access various external applications Management / Security
Demonstration Tamino
An example : eXist http://exist.sourceforge.net/ An OpenSource native XML database Features Proprietary Text-based storage (B+-trees and paged files) Hierarchical collections Automatic Indexes Supports XQuery Full-text search Work with HTTP / Java Support XUpdate No transaction support
Demonstration eXist
eXist : example of SANDRE Store schemas XML published by the SANDRE Find markup and characteristics Download schemas Parse XML documents
An example SANDRE Schema = an XML document DB Schema = XML database eXist Search and publish tags = XQuery
The market for XML database products Categories of XML products Middleware : Software you call from your application to transfer data between XML documents and databases XML-enabled database Native XML database XML Servers : XML-aware J2EE servers, Web application servers, integration engines, and custom servers. Wrappers : Software that treats XML documents as a source of relational data. These products typically query XML documents using SQL. XML Query engines : Standalone engines that can query XML documents XML Data bindings : Products that can bind XML documents to objects. Some of these can also store/retrieve objects from the database.
XML SQL Utility for Java Products : Middleware Lot of products ! ADO Microsoft DBXML XML DB XML SQL Utility for Java Oracle XSQL Servlets Castor (Java) OpenSource Alliance XML/400 (AS/400) Patrick Townsend Pear_XML (php) Opensource
Products : XML-Enabled database Main databases are XML-Enabled… … if you buy recent versions … with important differences in the implementation of XML features ACCESS (2000+) Microsoft Relational DB2 IBM Informix Oracle 8i,9i, 10g Oracle SQL Server (2000+) Sybase ASE Sybase
Products : Native XML database Lot of opensource implementations… Commercial ? Tamino Sofware AG Proprietary model Sonic XML Server Sonic software Object-oriented dbXML OpenSource eXist Text model Berkeley DB XML Key-value ozone XIndice (Apache) Proprietary (Model-based)
Products : others XML Servers XQuery engine Data bindings Wrappers Cocoon (Apache) ColdFusion (Macromedia) Zope (OpenSource) XQuery engine BQ-XQuery engine Web Logic Integration XQuery engine DataDirect XQuery™ XQEngine XQuery for .NET XQuery Processor Data bindings Castor JAXB for Java Jakarta (Apache) XDK Oracle XML definition tool (.net) Wrappers SQLServer DB2 Information Integrator
Advantages / Disadvantages of relational database Very used ! Rules to organise data (modelling data) and manages the RDMS Efficient with data Allow all types of search SQL standard Complex (and slow) to find hierarchical data (with join,…) Bad performance with document Need engine to generate and understand XML documents
Advantages / Disadvantages of XML-Enabled database A database to store relational AND XML documents Rules to organise data (modelling data) and manages the DDMS Efficient with data and to mapping XML Data SQL extension less new skills to be acquired Complex to manage Data type conversion and Null data Binary data ? Character Sets
Advantages / Disadvantages of native XML database All is made to manage XML documents Efficient to store and use XML documents Performance to find an document or an fraction of document XQuery / XPath No mandatory normalization Problem of referential Integrity Less good performances when you search pieces of documents Scability Tools to develop applications Others types of data ? New skills
Comparison relational, XML-enabled, Native yours needs Relational DB XML-Enabled DB Native XML DB Database to store very structured data. Complex and different queries / analysis,… Lot of data The best solution Allow to answer all queries with the good average performance Why not ? To allow to exchange with XML formats ? Database to store XML documents. Always the equivalent queries XML documents = a binary information Not optimized A good solution allowing to use the same database The best solution. Database to store structured data. Lot of import / export in XML exchanges. A “classical” solution. Need import / export interface A good solution A good solution. Allow to have quickly a database Database to store structured data and to publish information (HTML, PDF, WebServices,…) A “classical” solution but need to develop tools to publish information The best solution. Store in relational database but allows the push of XML Documents and transformation Why not if you succeed in organizing your data
Links about XML Databases XML family : www.w3c.org Introduction to native XML database : http://www.xml.com/pub/a/2001/10/31/nativexmldb.html History of XML database : http://www.eaijournal.com/PDF/XMLMcGoveran.pdf XQuery : http://www.xml.com/pub/a/2002/10/16/xquery.html Oracle : http://www.oracle.com/technology/tech/xml/index.html Tamino : http://www1.softwareag.com/Corporate/products/tamino/default.asp XIndice : http://xml.apache.org/xindice/ eXist : http://exist.sourceforge.net/ SANDRE : www.sandre.eaufrance.fr XML-EAU : http://www.sandre.eaufrance.fr/francais/frame/sagen.htm?page=../../xmleau/index.html