Silke Eckstein Andreas Kupfer
Institut für Informationssysteme Technische Universität Braunschweig http://www.ifis.cs.tu-bs.de
XML Databases
5. Mapping Relational Data to XML Documents, 24.11.08
5.1 Mapping to XML 5.2 Mapping tables 5.3 Mapping query results 5.4 Individual mapping 5.5 XSLT
5.6 Overview 5.7 References
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 2
5. Mapping Rel2XML
• Why map relational database contents to XML?
–Interoperability
•We may want to use (parts of) our RDB contents in many different application contexts (XML as data interchange format) –Reconstruction
•We might have stored (parts of) our XML documents in an RDBMS in the first place (RDBMS as XML store) –Dynamic XML contents
•We may use RDBMS queries to retrieve dynamic XML contents (cf. dynamic Web sites)
–Wrapping
•Everybody likes XML …, so why don't we give it to them?
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 3 [Scholl07]
5.1 Mapping to XML
• Why do we look at that mapping?
–What we're really interested in is the mapping in the opposite direction:
How to get XML into a database!
–Yes, but…
•This one is easier to start with.
•We do get some insight for the other mapping.
•We can see some of the problems.
•We'll see in what respect XML supports semi-structured data.
•We'll learn more about SQL as well.
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 4 [Scholl07]
5.1 Mapping to XML
• XML vs. SQL –Hierarchical vs. flat
–Loose schema vs. fixed schema
–Case-sensitive names vs. case-insensitive names –Several type concepts
–Strictly unicode based vs. heterogeneous encodings –Ordered vs. unordered
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 5
5.1 Mapping to XML
• Mapping of SQL database to XML
–SQL charset to unicode (depends on implementation) –SQL identifiers to XML names
–SQL data types to XML schema data types –SQL values to XML values
–SQL tables to XML and XML schema documents –SQL schemas to XML and XML schema documents –SQL catalogues to XML and XML schema documents
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 6
5.1 Mapping to XML
• Mapping between SQL identifiers and XML names
• Charset
–XML is based on unicode, SQL is not
–Mapping between a SQL charset and unicode depends on the implementation
• Names
–Not every SQL name is allowed as XML name (delimited identifier) –Certain characters have to be masked (especially ":" and leading
"xml" )
•Ω _x03A9_ (the unicode value)
•Gehalt:FY2000 Gehalt_x003A_FY2000
•Work@home Work_x0040_home
•Home_Town Home_x005F_Town –SQL name becomes XML name (in capitals)
•Employee <EMPLOYEE>…</EMPLOYEE>
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 7
5.1 Mapping to XML
• Mapping data type names
–Data type parameters are added to the XML name
•Example: DECIMAL_9_2, VARCHAR_10, BLOB_4000
•For TIME, TIMESTAMP, INTERVAL parameters are added about accuracy and time zone if applicable
–DOMAINdin schema sin catalogue cis transformed to the XML name c.s.d(fully masked)
–DISTINCT TYPEis analogous to domain
–ARRAYof the type twith maximal melements: ARRAY_m.t –MULTISETis analogous to ARRAY
–ROWtypes: depends on implementation, but begin with prefix Row
–Interval types, structured types and reference types can not be mapped
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 8
5.1 Mapping to XML
• Mapping ofdata types –Using XML schema
•Many predefined data types
–<xsd:element name="city" type="xsd:string">
–<xsd:element name="zip" type="xsd:integer">
–Extension of DTDs by reserved attributes
•<city xml-sqltype="varchar">Braunschweig</city>
•<zip xml-sqltype="integer">38100</zip>
–XML processors or applications have to know about and evaluate those information
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 9
5.1 Mapping to XML
• ... Mapping of data types
–Differences between SQL and XML schema
•SQL data types are mapped to the best matching XML schema type
•Facets limit the range of the XML schema type to the SQL range
•Characteristics not mapped (like Collation and character set) are annotated
–Cooperation of the namespacesXML schema and SQL/XML
•To define the SQL base data types und type constructors in XML
•Allows to map constructed and user defined SQL data types
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 10
5.1 Mapping to XML
length scale maxElements domainName
maxLength minExponent final typeName
characterSet maxExponent catalogName mappedType collation userPrecision schemaName mappedElementType precision leadingPrecision
• Mapping of SQL basic data types: Character –CHARACTER(20) CHARACTER SET LATIN1
COLLATION ENGLISH
<xsd:simpleType name="CHAR_20">
<xsd:annotation>
<xsd:appinfo>
<sqlxml:sqltype kind="PREDEFINED"
name="CHAR" length="20"
characterSetName="LATIN1"
collation="ENGLISH"/>
</xsd:appinfo>
</xsd:annotation>
<xsd:restriction base="xsd:string">
<xsd:length value="20"/>
</xsd:restriction>
</xsd:simpleType>
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 11
5.1 Mapping to XML
Exact type description Exact type description based on the data
type or type constructor from
SQLXML
Mapping to data type of XML schema
• Mapping of SQL basic data types: Numeric –NUMERIC(12,2)with an implementation using 13 decimals
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 12
5.1 Mapping to XML
<xsd:simpleType name="NUMERIC_12_2">
<xsd:annotation>
<xsd:appinfo>
<sqlxml:sqltype kind="PREDEFINED"
name="NUMERIC"
precision="12"
scale="2"/>
</xsd:appinfo>
</xsd:annotation>
<xsd:restriction base="xsd:decimal">
<xsd:totalDigits value="13"/>
<xsd:fractionDigits value="2"/>
</xsd:restriction>
</xsd:simpleType>
There are different There are different attributes defined for
the various SQL data types, like precision and fraction digits for
numeric data types
• Mapping of SQL basic data types: DECIMAL(12,2) ARRAY [10]
<xsd:complexType name="ARRAY_10.DECIMAL_12_2">
<xsd:annotation>
<xsd:appinfo>
<sqlxml:sqltype kind="ARRAY"
maxElements="10"
mappedElementType="NUMERIC_12_2"/>
</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="Element"
minOccurs="0"
maxOccurs="10"
type="NUMERIC_12_2">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 13
5.1 Mapping to XML
Maximum length of the array
• Mapping of SQL basic data types: CHARACTER(20) MULTISET
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 14
5.1 Mapping to XML
<xsd:complexType name="MULTISET.CHAR_20">
<xsd:annotation>
<xsd:appinfo>
<sqlxml:sqltype kind="MULTISET"
mappedElementType="CHAR_20"/>
</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="Element"
minOccurs="0"
maxOccurs="unbounded"
type="CHAR_20">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
Unlimited cardinality
5.1 Mapping to XML 5.2 Mapping tables 5.3 Mapping query results 5.4 Individual mapping 5.5 XSLT
5.6 Overview 5.7 References
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 15
5. Mapping Rel2XML
• Mapping of databases to XML –Standard mapping of tables –Standard mapping of query results –Individual mapping instructions
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 16
5.2 Mapping tables
• General approach –How to map
•Table and column names to element and attribute names
•Data types to XML schema data types
•Data from the database to content in XML documents –Similar approach for all database system producers –SQL/XML (next chapter)
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 17
5.2 Mapping tables
• Standard mapping of tables –Database as a 3-tier hierarchy of
•Database
•Tables
•Columns
–Presentation of database contents and structures in an XML document
•As elements
•As elements and attributes
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 18
5.2 Mapping tables
Database
Table_1
Column_11 Column_12 Column_13 Table_2
Coulmn_21 Column_2o Table_n
Column_n1 Column_np
• Example: Mapping as elements Pizza Delivery: Address:
–Standard mapping of column names toelement names
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 19
5.2 Mapping tables
Pid Name Category Address
1 Super Pizza 4 1
2 Turbo Pizza 3 2
Aid City Street Number
1 Braunschweig Big str. 55 2 Braunschweig Small str. 6
<PizzaDelivery>
<Pid>1</Pid>
<Name>Super Pizza</Name>
<Category>4</Category>
<Location>1</Location>
</PizzaDelivery>
• Example: Mapping as elements and attributes Pizza Delivery: Address:
–Standard mapping of the column names toattribute names
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 20
5.2 Mapping tables
Pid Name Category Address
1 Super Pizza 4 1
2 Turbo Pizza 3 2
Aid City Street Number
1 Braunschweig Big str. 55 2 Braunschweig Small str. 6
<PizzaDelivery Pid='1'
Name='Super Pizza' Category='4' Location='1' />
• Mapping of keys and foreign keys –Goal: Keys and foreign keys shall be represented
appropriately in the XML document –Mapping relations to element hierachy
•1:n relations are mapped to nested elements
•n:m relations are problematic
–Denormalized content in the XML document – possibly high redundancy
–Using XML schema
•Representing keys and foreign keys with key / keyref –Using DTD
•Mapping keys and foreign keys to attributes (ID/IDREF)
•Make keys unique (ID must be unique document wide)
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 21
5.2 Mapping tables
• Keys and foreign key as nested elements
–Resolving of foreign key relations by embedding referenced elements
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 22
5.2 Mapping tables
Pid Name Category Address
1 Super Pizza 4 1
2 Turbo Pizza 3 2
Aid City Street Number
1 Braunschweig Big str. 55 2 Braunschweig Small str. 6 FK
<!ELEMENT PizzaDelivery (Pid, Name, Category?, Address)>
<!ELEMENT Address (Aid, City, Street, Number)>
<!ELEMENT Pid (#PCDATA)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Category (#PCDATA)>
<!ELEMENT Aid (#PCDATA)>
<!ELEMENT City (#PCDATA)>
...
• Mapping object relational databases –Object relational databases support non atomic,
complex columns
•Tuple values
•Collection values
•Object values
•Reference values
–Existing structure shall be carried over to the XML document
•Instance layer
•Schema layer
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 23
5.2 Mapping tables
• Mapping object relational databases
–Instance layer:Appropriate mapping of instances with complex attributes (tuple, sets or lists)
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 24
5.2 Mapping tables
Pid Name <Address> {Phone}
1 Super Pizza 4City Big str.Street 55Number 1
1 Super Pizza BS Big str. 55 {'0531/555-7447'
'0531/555-2232'}
<PizzaDelivery>
<Pid>1</Pid>
<Name>Super Pizza</Name>
<Address>
<City>BS</City>
<Street>Big str.</Street>
<Number>55</Number>
</Address>
<Phone>0531/555-7447</Phone>
<Phone>0531/555-2232</Phone>
</PizzaDelivery>
• Mapping object relational databases
–Schema layer: Derive XML schema or DTD from object relational schema
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 25
5.2 Mapping tables
CREATE ROW TYPE AddressType(
City VARCHAR(25), Street VARCHAR(20), Number INTEGER);
CREATE TABLE PizzaDelivery(
Pid INTEGER NOT NULL PRIMARY KEY, Name VARCHAR(20) NOT NULL, Address AddressType, Phone SET(INTEGER NOT NULL));
<!ELEMENT PizzaDelivery (Pid, Name, Address, Phone+)>
<!ELEMENT Address (City, Street, Number)>
<!ELEMENT Pid (#PCDATA)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Phone (#PCDATA)>
<!ELEMENT City (#PCDATA)>
…
• Mapping tables – summary –Database output – complete –Required information – none –Variable output format – no
–Preservation of data types – by extended DTD or XML schema –Storing keys and foreign keys
•Mapping to hierachies in the XML document
•ID/IDREF
•XML schema –Special features
•Parsers must evaluate DTD extensions
•XML schema is better suited
•Appropriate represenation of tuples, sets, lists and references from object relational databases in the XML document
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 26
5.2 Mapping tables
5.1 Mapping to XML 5.2 Mapping tables 5.3 Mapping query results 5.4 Individual mapping 5.5 XSLT
5.6 Overview 5.7 References
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 27
5. Mapping Rel2XML
• Mapping of SQL query results to XML documents or XML elements
–Standard XML representation
•Result table torowsetelements
•Every row to a rowelement
•Columns to subelements or XML attributes
•No variable structure
–Similar process is part of the SQL/XML standard
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 28
5.3 Mapping query results
• SQL
• XML
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 29
5.3 Mapping query results
SELECT Name, Category, City FROM PizzaDelivery, Address WHERE City='Braunschweig' AND
PizzaDelivery.Address=Address.Aid
<rowset>
<row no='1'>
<Name>Super Pizza</Name>
<Category>4</Category>
<City>Braunschweig</City>
</row>
</rowset>
…
?
• Creating XML by using defaults from the DBMS –Implementation dependant, e.g.: MySQL has an option to
produce XML output
•Shell command to execute a query with username and database name, piped to a file
–No need to dump the whole database (mysqldump --xml …)
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 30 [MySQL]
5.3 Mapping query results
./mysql -ujon test --xml -e 'SELECT * FROM t1' > t1.xml
<?xml version="1.0"?>
<resultset statement="SELECT * FROM t1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row> <field name="Pid">1</field>
<field name="Name">Super Pizza</field>
<field name="Category">4</field>
<field name="Location">1</field> </row>
<row>…</row>
</resultset>
• Mapping query results – summary –Database output – partial
–Required information – database query or view –Variable output format – no
–Preservation of data types – usually not, but possible to deduce
–Storing keys and foreign keys
•Possible to represent relations within the table –Embedding, ID/IDREF, XML schema
–Special features
•none
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 31
5.3 Mapping query results
5.1 Mapping to XML 5.2 Mapping tables 5.3 Mapping query results 5.4 Individual mapping 5.5 XSLT
5.6 Overview 5.7 References
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 32
5. Mapping Rel2XML
• Variants for individual mapping instructions
1 2
3
Extended database query with transformation instructions
Standard
transformation XML query language
Standard
transformation XSLT stylesheet
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 33 [Tür08]
5.4 Individual mapping
Database
XML document XML view
Standardized document Standardized
XML document
• (Extended) database query languages
–First Idea: use basic SQL
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 34
5.4 Individual mapping
Database
XML document
• Creating XML by using basic SQL
–Basic idea is to use string concatenation in the select clause to build XML markup
•|| operator from the SQL standard
•Concatenation requires values to be not NULL
–Implementation independant
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 35 [Pow07]
5.4 Individual mapping
SELECT '<pizzerias><name>'||Name||'</name>', '<category>'||Category||'</category></pizzerias>' FROM PizzaDelivery;
postgres=# SELECT 'abc' || 'def' AS "unspecified";
Result:
abcdef
• Problems and limitations of basic SQL –Valid XML documents require a header and one root
element
•Workaround with additional SELECT statements –Nesting is difficult
•The output is a single table
•No dependance on previous or following SELECT statements
• Extend an SQL dialect by XML operators:
–Database query (in SQL) to select the data to be presented in XML
–XML query to define the syntax of the target format (e.g.
CONSTRUCT / RETURN / FOR XML)
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 36 [Pow07]
5.4 Individual mapping
• Example
Select * from PizzaDelivery, Address where (Address=Aid)
construct
<PizzaDelivery>
<name>
{$name}
</name>
<city>{$city}</city>
<address>{$street}
{$number}</address>
</PizzaDelivery>
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 37
5.4 Individual mapping
Access to columns from the SQL result
Arbitrary XML names Arbitrary XML element and attribute
names
• Individual mapping instructions and XML queries
–Query to the database with XML query languages is supported
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 38
5.4 Individual mapping
Database
XML document XML view
Standard
transformation XML query language
• Implementing XML queries –Naive Implementation
•Full content of the database is transformed to an XML document
•XML query is processed on this document
•Very inefficient (XML document can be very large, can contain irrelevant information)
–Better: XML view on the data in a database
•XML document will not be materialized (virtual)
•XML queries are processed on the virtual document
•Much processing within the native database engine
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 39
5.4 Individual mapping
• Example: Silkroute –Silkroute
•Middleware beween RDBMS and XML application
•Develloped by AT&T and University of Pennsylvania –Creation of an XML view
•Arbitrary output format
•View is not materialized
–XML queries to the view with XML-QL or XQuery –Problems
•Derive SQL queries from a view definition
•Processing of the whole view usually not neccessary
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 40
5.4 Individual mapping
• Silkroute example
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 41
5.4 Individual mapping
Construct
<view>
from Address a, PizzaDelivery d construct
<PizzaDelivery>
<name>$d.name</name>
<address>
<city>$a.city</city>
<street>a.$street
a.$number</street>
</PizzaDelivery>
</view>
• Benefits of using standardized XML documents as intermediate format
–Less requirements, more flexibility, better compatibility
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 42
5.4 Individual mapping
Database
XML document
Standardized document Standardized
XML document Standard
transformation XSLT stylesheet
• Individual mapping instructions – summary –Database output – complete or partial
–Required information
•XML query
•XSLT (see next section (5.5))
•View definition
–Variable output format – yes
–Preservation of data types – usually not, but possible to deduce
–Storing keys and foreign keys
•Embedding, ID/IDREF, XML schema –Special features
•none
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 43
5.4 Individual mapping
5.1 Mapping to XML 5.2 Mapping tables 5.3 Mapping query results 5.4 Individual mapping 5.5 XSLT
5.6 Overview 5.7 References
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 44
5. Mapping Rel2XML
• XSLT
–Extensible Stylesheet Language – Transformations –A language to describe transformations from source to
target tree structures (= XML documents) –A transformation in XSLT
•Is described by a well-formed XML document calledstylesheet
•Can use elements of theXSLT namespaceas well as of other namespaces
•Containstemplate rulesto execute the transformation
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 45
5.5 XSLT
XML file
XSLT Stylesheet
XML file XSLT
Processor
XSLT stylesheet and XML document
XSLT tree and Source tree
Transformation
process Result tree Result
document
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 46
5.5 XSLT
• XSLT processor
• Template rules
–A rule consists of a patternand a template –The pattern is compared to the nodes of the source
document tree
–The template can be instanciated to create a part of the target tree. It can contain elements of the XSLT namespace which are instructions to create fragments
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 47
5.5 XSLT
• XSLT processing model –By processing a list of source nodes,
fragments of the target tree can be created –The list starts with the root node only –A node is processed
•By selecting thebest matching patternfrom all rules (resolving any conflicts)
•The template of the best matching rule is instanciated with the current node as context node
–A template usually contains instructions to select further source tree nodes for processing
–Recursivly repeat the selection of matching rules, instanciation and selecting of new source nodes until the list is empty
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 48
5.5 XSLT
• Structure of a stylesheet
–Elements and attributes with the XSLT namespace must be recognized by the XSLT processor –PIs and comments are ignored
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 49
5.5 XSLT
<xsl:stylesheet id={id}
extension-element-prefixes={token}
exclude-result-prefixes={token}
version=number>
<!-- Content: (xsl:import*, top-level-elements)-->
</xsl:stylesheet>
• Top level elements
–E.g. xsl:import, xsl:include, and most importantlyxsl:template
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 50
5.5 XSLT
<xsl:template match = {pattern}
name = {qname}
priority = {number}
mode = {qname}
<!-- Content: (xsl:param*, template) -->
</xsl:template
• A pattern specifies a set of conditions to a node
–Uses a set of alternative (|-seperated) address paths in the child and attribute axis
–The use of '/' and '//', 'id' and 'key' functions is possible –Pattern predicates ('[…]') can use all XPath
expressions
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 51
5.5 XSLT
• Multiple matching patterns
–If multiple patterns match a node, the conflict is resolved by priorities (cf. priority attribute)
•Imported rules have a lower priority than rules of the primary stylesheet
•Alternatives are processed as if each alternative is defined by a single rule
•ChildOrAttributeAxisSpecifier::QName patterns have priority 0
•ChildOrAttributeAxisSpecifier::NCName patterns have priority -0.25
•ChildOrAttributeAxisSpecifier::NodeTest patterns have priority -0.5
•All other patterns have priority 0.5
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 52
5.5 XSLT
• XSLT contains default rules –Process the document recursivly
–But have lower priority than rules in the stylesheet –Example:
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 53
5.5 XSLT
<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>
• Rules
–Can benamedand be called in templates of other rules –Can haveparameterswhich can be passed along on
their invocation, default valuescan be defined
–The modeattribute allows a rule to be processed multiple times and with different results
–If the template is invoked directly withxsl:call- templateorxsl:apply-template, the filter attributes (match, mode, priorityorname) are not processed
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 54
5.5 XSLT
• Templates
–Can containliteral elements(non XSLT namespace) andelementsof the XSLT namespace (instructions)
–If theruleis selected, the template can construct fragments of the result tree
–Processingdepends on the context
–Default behaviouris to write all elements which are not in the XSLT namespace to the result tree –Must be valid XML
–Can containinstructions
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 55
5.5 XSLT
• Instructions to process nodes recusively
–Without the attributeselectall children of the context node are processed
–Select can be a (XPath-) expression to select nodes
•Could result in not terminating recursion!
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 56
5.5 XSLT
<xsl:apply-template select = {node set expression}
mode = {qname}>
<!-- Content: (xsl:sort, xsl:with-param)* -->
</xsl:apply-template>
• Instructions to create a node
–Nameattribute is required, but can be calculated –Other create instructions are similar
•xsl:attribute, xsl:attribute-set, xsl:text (to create a text/leaf node with whitespaces),
xsl:processing-instruction, xsl:comment
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 57
5.5 XSLT
<xsl:element name = {qname}
namespace = {uri-reference}
use-attribute-sets = {qname} >
<!-- Content: template -->
</xsl:element>
• Instructions for flow control –Conditional processing
–Test expression is evaluated and result is casted to a boolean. If it is true the template will be instanciated
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 58
5.5 XSLT
<xsl:if
test = {boolean expression}
<!-- Content: template -->
</xsl:if>
required
• Instructions for flow control
• Multiple choice ("if-then-else" / "switch")
–If multiple xsl:whenelements are true, only the first one is processed (no "break" needed)
–If noxsl:whenelement is true and there is no xsl:otherwise, no content is created
<xsl:choose
<!-- Content: (xsl:when+, xsl:otherwise?) -->
</xsl:choose>
<xsl:when
test = {boolean expression}
<!-- Content: template -->
</xsl:when>
<xsl:otherwise
<!-- Content: template -->
</xsl:otherwise>
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 59
5.5 XSLT
• Repetition
–The template is instanciated for each node selected by the node set expression
–On instanciation the current node becomes the context node and all selected nodes are the node list –If there is no explicit sort statement, the nodes are
processed in document order
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 60
5.5 XSLT
<xsl:for-each
select = {node-set expression}
<!-- Content: (xsl:sort*, template) -->
</xsl:for-each>
• "Calculation" of output text
–The selected object is casted to a string value and is inserted as content of the instanciated text node
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 61
5.5 XSLT
<xsl:value-of
select = {string expression}
disable-output-escaping = "yes" | "no" />
• Other statements for sorting, numbering, variables, …
–seehttp://www.w3.org/TR/xslt
• Some advice
–"select" is used differently depending on its context!
–Denomination "variable" is misleading!
–Context node is changed by for-each!
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 62
5.5 XSLT
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 63
5.5 XSLT
<?xml version="1.1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="resultset">
<html>
<head/>
<body>
<h1>
<xsl:text>Summary about </xsl:text>
<xsl:value-of select="count(child::*)"/>
<xsl:text> Pizzeria</xsl:text>
<xsl:if test="count(child::*) > 1">
<xsl:text>s</xsl:text>
</xsl:if>
</h1>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 64
5.5 XSLT
<?xml version="1.0"?>
<resultset statement="SELECT * FROM t1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance">
<row> <field name="Pid">1</field>
<field name="Name">Super Pizza</field>
<field name="Category">4</field>
<field name="Location">1</field> </row>
<row>…</row>
</resultset>
<xsl:template match="field[attribute::name='Name']">
<h2>
<xsl:value-of select="."/>
</h2>
</xsl:template>
</xsl:stylesheet>
5.1 Mapping to XML 5.2 Mapping tables 5.3 Mapping query results 5.4 Individual mapping 5.5 XSLT
5.6 Overview 5.7 References
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 65
5. Mapping Rel2XML
• Conclusion
–Different methods to generate XML documents from stored information exist
•Standard mapping of tables to XML –Standard XML document: fixed output format
•Standard mapping of query results –Extend database queries by XML functionality –Standard XML document
•Individual mapping instructions –XML views on database contents –XML query languages processed on views –Mapping using XSLT
–Focus on relational and object relational databases
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 66
5. Mapping Rel2XML
1. Introduction 2. XML Basics 3. Schema definition 4. XML query languages I 5. Mapping relational data
to XML 6. SQL/XML 7. XML processing
8. XML query languages II 9. XML storage I 10. XML storage - index 11. XML storage - native 12. Updates / Transactions 13. Systems
14. XML Benchmarks
5.6 Overview
67 XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
• Can Türker, "XML und Datenbanken", Lecture, University of Zurich, 2008 [Tür08]
• Beginning XML Databases. Gavin Powell, Wiley &
Sons, 2007, ISBN 0471791202 [Pow07]
• M. Scholl, "XML and Databases", Lecture, Uni Konstanz, WS07/08 [Scholl07]
• Jon Stephens, MySQL Documentation Team, "MySQL 5.1 Reference Manual", 2007 [MySQL]
• XML & Datenbanken. Konzepte, Sprachen und Systeme. Klettke & Meyer, Dpunkt-Verlag, 2002, ISBN 3898641481 [KM02]
68
5.7 References
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
• Now, or ...
• Room: IZ 232
• Office our: Tuesday, 12:30 – 13:30 Uhr or on appointment
• Email: eckstein@ifis.cs.tu-bs.de
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig 69