XML And Databases (and XForms) - Mimuw.edu.pl

Transcription

XML and databases(and XForms)Patryk CzarnikXML and Applications 2013/2014Week 13 – 13.01.2014

XML support in databases –categorisationClassic (usually relational) database with XML supportlogical structure – relations and referencesadditional XML-related featuresused for application integration or storing XML data as partof larger data structures Native' XML databaselogical structure – collection of XML document treesXQuery (or XPath) as native query languagenatural XML-related featuresused for storing XML data (or structural data easilymapped to XML tree)2 / 20

XML support in relational databasesPossible functionalitiesimport and export of data in XML formatspecial treatment of XML data stored in fieldsXML validation as part of integrity constraints checkingXPath or XQuery for querying field contentsXSLT applied to query resultHow to store XML data?whole document (fragment) stored in single fieldsplit into prima factorseach XML node in separate fieldtables structure reflects tree structure of XML3 / 20

Example – XML support in OracledatabaseSince Oracle 8i (http://www.oracle.com/xml)details differ from version to versionXML parsersfor database programming (PL/SQL)or middleware programming (Java, C )XML-SQL UtilityXML data import and exportXMLType data type and XML-specific operations4 / 20

XML-SQL UtilitygetXML() function – XML data exportSELECT xmlgen.getXML('select * from emp') FROM dual; rowset row id "1" empno 10 /empno name Scott Tiger /name title specialist /title /row . /rowset 5 / 20

XML in Oracle DB – XMLTypeXMLType – special datatype:to be stored as LOB or used for columns, variables, etc.indexing XML contentXPath expressionsvalidation against XML SchemaXSLTAvailable functions:extract, extractValue, exisitsNode, transform,updateXML, XMLSequence6 / 20

XMLType applications – some examplesCREATE TABLE warehouses(warehouse id NUMBER(4),warehouse spec XMLTYPE,warehouse name VARCHAR2(35),location id NUMBER(4));CREATE TABLE po xtab of XMLType;UPDATE po xml tabSET poDoc UPDATEXML(poDoc,'/PO/CUSTNAME/text()', 'John');INSERT INTO warehouses VALUES(100, XMLType(' Warehouse whNo "100" Building Owned /Building /Warehouse '), 'Tower Records', 1003);SELECT e.poDoc.getClobval() AS poXMLFROM po xml tab eWHERE e.poDoc.existsNode('/PO[PNAME "po 2"]') 1;CREATE INDEX city index ON po xml ;Source: Oracle DB documentation7 / 20

XML support in database enginesSpecification: ISO/IEC 9075-14:2011 SQL/XMLnew data type XML:only well-formed XML documents allowedparsing and serialisationimplementation may add XML-specific operationsSubstantial supportIBM DB2 (since v.9 – pureXML)Oracle (since 8i)Microsoft SQL Server (since v.2000)Sybase ASE (since v.12)Minimal supportMySQL – XPath queries over text fields containing XMLPostgreSQL – as above plus XML datatypebut with no special operations8 / 20

Native XML databaseLogical layerXML document as basic data entitycollections of documents build a databaseXML schema (or equivalent) as structure definitionXQuery (or XPath) as “native” query languagePhysical layer – not necessarily files with XML textMore than just a collection XML files:transactions and concurrent accesssecurity (access privileges etc.), versioning, replication, .API for data access and updateadditional means of data accesse.g. REST-compliant HTTP serverindexing for efficient access to selected nodes9 / 20

Standards for XML databasesHigh level query languages:XQuery – primary language for queriesversions 1.0 and 3.0 in useXQL – former approach to make XML query languageXPath – poor stub for XQueryHigh level update languages:XQuery Update ExtensionXUpdateProgrammer APIsdepend additionally on programming languageXML Database API (XAPI)XQJ (for Java, expected to become XML equivalent of JDBC)vendor-specific APIs.10 / 20

XML:DBInitiative for XML database interfaces specificationXML Database API (XAPI)accessing XML databases from programsresource collections (resource XML document)reading and writing documents via DOM or SAXpluggable “services”; specified: XPath, transactions,operations on collectionslast version: 2001XML Update Language (XUpdate)XML application for updating XML databasesinserting, updating and removing nodesXPath used for node addressinglast version: 200011 / 20

XUpdate – exampleExample (from XUpdate documentation) ?xml version "1.0"? xupdate:modifications version "1.0"xmlns:xupdate "http://www.xmldb.org/xupdate" xupdate:insert-after select "/addresses/address[1]" xupdate:element name "address" xupdate:attribute name "id" 2 /xupdate:attribute fullname Lars Martin /fullname born day '2' month '12' year '1974'/ town Leizig /town country Germany /country /xupdate:element /xupdate:insert-after . /xupdate:modifications 12 / 20

XML database products – overviewProductLicenceQueriesXML:DB APIeXistopen sourceXPath, XQueryyesBaseXopen sourceXPath, XQueryyesMarkLogiccommercialApache XIndiceopen sourceXPathyesSednaopen sourceXPath, XQueryyesGemfire EnterprisecommercialXQuery, OQLyesTaminocommercialXQuery, XPathpartSource: Wikipedia and providers' websites13 / 20

eXist DBOne the most popular and the most elaborated XMLdatabase enginesOpen-source, but developed and supported bya (German) companyFeatures include:storage of XML and binary entitiesvarious means of access, including: human-readable Webinterface, direct HTTP access (REST-compliant), SOAP andXML-RPC, Java API (XQJ, elements of XAPI)full XML model available in XPath, XQuery, and XSLT codefull XQuery support with majority of new 3.0 features,Update extension and some other non-standard extensionsXForms support using betterFORM or XSLTForms plugins14 / 20

eXist – eXideXQuery programmer SDK running within a browsersupports also (to some extent.)XSLT, XML Schema, XHTML, XForms15 / 20

eXist – template mechanismEasy integration of XQuery logic and HTML interface div class "app:hello" /div declare function app:hello( node as node(), model as map(*), .) {'Hello world!'};16 / 20

REST services – recallREST for Representational State TransferPrinciples:Service collection of resourcesURL identifies a resourceResource has a normalised representation and can betransferred through the networkXML for structural databinary and other structural formats (JSON) also permittedHTTP methods directly used to manipulate resourcesGET, PUT, DELETE – obvious semanticsother HTTP methods, HTTP authentication, cookies,additional headers and arguments – all may be used toimplement additional features17 / 20

REST for XML databaseREST – remote access to a repositoryCan it be an XML database? Why not.Possible applications:Access API independent of particular platform or pr.lang.Easy and efficient remote access fromJavascript clients (AJAX)mobile clientsIntegration with XML-related standardsXSLT, XQuery – documents available through HTTP URLsXForms – acquiring and modifying documents directly formXFormsHTTP interface available also to call server-side XQueryscriptsXRX architecture: XForms REST XQuery18 / 20

XFormsXML application for specification of interactive formsVersions:1.0 – 20031.1 – 2009 (currently most commonly used)2.0 – WDMore than HTML forms:data model defined separately from UIby example or using XML Schemaprocessing model specified with events and actionsXPath used to tie data instance with UI controls and actionsvarious data access modes given in submission moduleincluding REST-compliant HTTP accessmore UI controls, interactive switch, automatic repeat19 / 20

XForms – document structureForms are embedded in a host document, usually XHTMLData model – xf:model elementanywhere in host documentheader more elegantbut body more practical for dynamic documentsmore than one model available; in such case they musthave identifiersForm controls (in XForms namespace)placed within normal XHTML tags(some of them) may contain further XHTML fragmentsAction specifications and constraints tied with XFormelementsby inserting them inside model fragments or control tags20 / 20using general xf:bind elements

MarkLogic commercial Apache XIndice open source XPath yes Sedna open source XPath, XQuery yes Gemfire Enterprise commercial XQuery, OQL yes . interface, direct HTTP access (REST-compliant), SOAP and XML-RPC, Java API (XQJ, elements of XAPI) full XML model available in XPath, XQuery, and XSLT code full XQuery support with majority of new 3.0 .