- IfIS - Technische Universität Braunschweig

Werbung
5. Mapping Rel2XML
5.1 Mapping to XML
5.2 Mapping tables
XML Databases
5.3 Mapping query results
5. Mapping Relational Data to
XML Documents, 24.11.08
5.4 Individual mapping
Silke Eckstein
Andreas Kupfer
Institut für Informationssysteme
Technische Universität Braunschweig
http://www.ifis.cs.tu-bs.de
5.5 XSLT
5.6 Overview
5.7 References
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
5.1 Mapping to XML
5.1 Mapping to XML
• Why map relational database contents to XML?
• Why do we look at that mapping?
– Interoperability
– What we're really interested in is the mapping in the
opposite direction:
How to get XML into a database!
– Yes, but…
• 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)
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.
•
•
•
•
– 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?
[Scholl07]
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
3
5.1 Mapping to XML
[Scholl07]
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
4
5.1 Mapping to XML
• XML vs. SQL
• Mapping of SQL database to XML
– 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
2
– 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
5
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
6
5.1 Mapping to XML
5.1 Mapping to XML
• Mapping data type names
• Mapping between SQL identifiers and XML names
• Charset
– Data type parameters are added to the XML name
– XML is based on unicode, SQL is not
– Mapping between a SQL charset and unicode depends on the
implementation
• Example: DECIMAL_9_2, VARCHAR_10, BLOB_4000
• For TIME, TIMESTAMP, INTERVAL parameters are added about
accuracy and time zone if applicable
– DOMAIN d in schema s in catalogue c is transformed to the
XML name c.s.d (fully masked)
– DISTINCT TYPE is analogous to domain
– ARRAY of the type t with maximal m elements: ARRAY_m.t
– MULTISET is analogous to ARRAY
– ROW types: depends on implementation, but begin with prefix
Row
– Interval types, structured types and reference types can not be
mapped
• Names
– Not every SQL name is allowed as XML name (delimited identifier)
– Certain characters have to be masked (especially ":" and leading
"xml" )
•
•
•
•
Ω
Gehalt:FY2000
Work@home
Home_Town
_x03A9_ (the unicode value)
Gehalt_x003A_FY2000
Work_x0040_home
Home_x005F_Town
– SQL name becomes XML name (in capitals)
• Employee
<EMPLOYEE>…</EMPLOYEE>
7
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
5.1 Mapping to XML
5.1 Mapping to XML
• ... Mapping of data types
• Mapping of data types
– Differences between SQL and XML schema
– Using 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
• Many predefined data types
length
maxLength
characterSet
collation
precision
– <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>
scale
minExponent
maxExponent
userPrecision
leadingPrecision
maxElements
final
catalogName
schemaName
domainName
typeName
mappedType
mappedElementType
• Characteristics not mapped (like Collation and character set) are
annotated
– Cooperation of the namespaces XML schema and SQL/XML
– XML processors or applications have to know about
and evaluate those information
• To define the SQL base data types und type constructors in XML
• Allows to map constructed and user defined SQL data types
9
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
5.1 Mapping to XML
10
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
5.1 Mapping to XML
• Mapping of SQL basic data types: Character
• Mapping of SQL basic data types: Numeric
– NUMERIC(12,2) with an implementation using 13 decimals
– CHARACTER(20) CHARACTER SET LATIN1
COLLATION ENGLISH
<xsd:simpleType name="CHAR_20">
<xsd:annotation>
Exact type description
<xsd:appinfo>
based on the data
<sqlxml:sqltype kind="PREDEFINED"
type or type
name="CHAR" length="20"
constructor from
characterSetName="LATIN1"
SQLXML
collation="ENGLISH"/>
</xsd:appinfo>
</xsd:annotation>
<xsd:restriction base="xsd:string">
Mapping to data type
<xsd:length value="20"/>
of XML schema
</xsd:restriction>
</xsd:simpleType>
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
8
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
11
<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>
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
There are different
attributes defined for
the various SQL data
types, like precision
and fraction digits for
numeric data types
12
5.1 Mapping to XML
•
5.1 Mapping to XML
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"
Maximum length of
maxOccurs="10"
the array
type="NUMERIC_12_2">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
Mapping of SQL basic data types: CHARACTER(20) MULTISET
<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"
Unlimited cardinality
maxOccurs="unbounded"
type="CHAR_20">
</xsd:element>
</xsd:sequence>
</xsd:complexType>
13
5. Mapping Rel2XML
14
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
5.2 Mapping tables
• Mapping of databases to XML
5.1 Mapping to XML
– Standard mapping of tables
– Standard mapping of query results
– Individual mapping instructions
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.2 Mapping tables
16
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
5.2 Mapping tables
• General approach
• Standard mapping of tables
– How to map
– Database as a 3-tier hierarchy of
• 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
• Database
• Tables
• Columns
– Similar approach for all database system producers
– SQL/XML (next chapter)
Database
Table_1
Column_11
Column_12
Table_2
Column_13
Coulmn_21
Column_2o
Table_n
Column_n1
Column_np
– 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
17
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
18
5.2 Mapping tables
5.2 Mapping tables
• Example: Mapping as elements and attributes
Pizza Delivery:
Address:
• Example: Mapping as elements
Pizza Delivery:
Address:
Pid Name
Category Address
Aid
City
1
Super Pizza
4
1
1
Braunschweig Big str.
Street
Number
2
Turbo Pizza
3
2
2
Braunschweig Small str. 6
55
Pid Name
Category Address
Aid
City
1
Super Pizza
4
1
1
Braunschweig Big str.
2
Turbo Pizza
3
2
2
Braunschweig Small str. 6
<PizzaDelivery
Pid='1'
Name='Super Pizza'
Category='4'
Location='1' />
<PizzaDelivery>
<Pid>1</Pid>
<Name>Super Pizza</Name>
<Category>4</Category>
<Location>1</Location>
</PizzaDelivery>
19
5.2 Mapping tables
• Mapping of keys and foreign keys
• Keys and foreign key as nested elements
– 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
Pid Name
FK
Category Address
Aid
City
1
Super Pizza
4
1
1
Braunschweig Big str.
2
Turbo Pizza
3
2
2
Braunschweig Small str. 6
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
...
– 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)
21
Number
55
PizzaDelivery (Pid, Name, Category?, Address)>
Address (Aid, City, Street, Number)>
Pid (#PCDATA)>
Name (#PCDATA)>
Category (#PCDATA)>
Aid (#PCDATA)>
City (#PCDATA)>
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
5.2 Mapping tables
22
5.2 Mapping tables
• Mapping object relational databases
• Mapping object relational databases
– Instance layer: Appropriate mapping of instances with complex attributes
(tuple, sets or lists)
– Object relational databases support non atomic,
complex columns
Tuple values
Collection values
Object values
Reference values
Pid
Name
1
1
Super Pizza
Super Pizza
<Address>
City
Street
4
Big
str.
BS
Big str.
{Phone}
Number
55
55
1
{'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>
– Existing structure shall be carried over to the XML
document
• Instance layer
• Schema layer
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
Street
– Resolving of foreign key relations by embedding referenced
elements
– Denormalized content in the XML document – possibly high
redundancy
•
•
•
•
20
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
5.2 Mapping tables
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
Number
55
– Standard mapping of the column names to attribute names
– Standard mapping of column names to element names
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
Street
23
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
24
5.2 Mapping tables
5.2 Mapping tables
• Mapping tables – summary
• Mapping object relational databases
– Schema layer: Derive XML schema or DTD from object relational schema
CREATE
City
Street
Number
–
–
–
–
–
ROW TYPE AddressType(
VARCHAR(25),
VARCHAR(20),
INTEGER);
CREATE TABLE PizzaDelivery(
Pid
INTEGER NOT NULL PRIMARY KEY,
Name
VARCHAR(20) NOT NULL,
Address AddressType,
Phone SET(INTEGER NOT NULL));
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
<!ELEMENT
…
• Mapping to hierachies in the XML document
• ID/IDREF
• XML schema
– Special features
PizzaDelivery (Pid, Name, Address, Phone+)>
Address (City, Street, Number)>
Pid (#PCDATA)>
Name (#PCDATA)>
Phone (#PCDATA)>
City (#PCDATA)>
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
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
• 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
25
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
5. Mapping Rel2XML
26
5.3 Mapping query results
• Mapping of SQL query results to XML
documents or XML elements
5.1 Mapping to XML
5.2 Mapping tables
– Standard XML representation
5.3 Mapping query results
• Result table to rowset elements
• Every row to a row element
5.4 Individual mapping
• Columns to subelements or XML attributes
• No variable structure
5.5 XSLT
– Similar process is part of the SQL/XML standard
5.6 Overview
5.7 References
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
27
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
5.3 Mapping query results
5.3 Mapping query results
• Creating XML by using defaults from the DBMS
• SQL
– Implementation dependant, e.g.: MySQL has an option to
produce XML output
SELECT Name, Category, City
FROM PizzaDelivery, Address
WHERE City='Braunschweig' AND
PizzaDelivery.Address=Address.Aid
• XML
28
./mysql -ujon test --xml -e 'SELECT * FROM t1' > t1.xml
• Shell command to execute a query with username and database name,
piped to a file
?
<?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>
<rowset>
<row no='1'>
<Name>Super Pizza</Name>
<Category>4</Category>
<City>Braunschweig</City>
</row>
</rowset>
…
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
– No need to dump the whole database (mysqldump --xml …)
29
[MySQL]
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
30
5.3 Mapping query results
5. Mapping Rel2XML
• Mapping query results – summary
5.1 Mapping to XML
– 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
5.2 Mapping tables
5.3 Mapping query results
5.4 Individual mapping
5.5 XSLT
• Possible to represent relations within the table
5.6 Overview
– Embedding, ID/IDREF, XML schema
– Special features
5.7 References
• none
31
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
32
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
5.4 Individual mapping
5.4 Individual mapping
• Variants for individual mapping instructions
• (Extended) database query languages
Extended database query with transformation instructions
1
2
Standard
transformation
3
[Tür08]
XML query language
XML view
Database
Standard
transformation
Standardized
XML
document
XML
document
XSLT stylesheet
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
– First Idea: use basic SQL
33
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
5.4 Individual mapping
• Problems and limitations of basic SQL
– Valid XML documents require a header and one root
element
– Basic idea is to use string concatenation in the select
clause to build XML markup
• Workaround with additional SELECT statements
• || operator from the SQL standard
– Nesting is difficult
postgres=# SELECT 'abc' || 'def' AS "unspecified";
Result:
abcdef
• The output is a single table
• No dependance on previous or following SELECT statements
• Extend an SQL dialect by XML operators:
• Concatenation requires values to be not NULL
SELECT '<pizzerias><name>'||Name||'</name>',
'<category>'||Category||'</category></pizzerias>'
FROM PizzaDelivery;
– 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)
– Implementation independant
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
34
5.4 Individual mapping
• Creating XML by using basic SQL
[Pow07]
XML
document
Database
35
[Pow07]
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
36
5.4 Individual mapping
5.4 Individual mapping
• Example
• Individual mapping instructions and XML
queries
Select * from PizzaDelivery, Address
where (Address=Aid)
construct
<PizzaDelivery>
<name>
Access to columns
from the SQL result
{$name}
Arbitrary XML
</name>
element and attribute
<city>{$city}</city>
names
<address>{$street}
{$number}</address>
</PizzaDelivery>
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
– Query to the database with XML query languages is
supported
Standard
transformation
37
5.4 Individual mapping
38
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
• Example: Silkroute
– Silkroute
– Naive Implementation
• Middleware beween RDBMS and XML application
• Develloped by AT&T and University of Pennsylvania
• 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)
– Creation of an XML view
• Arbitrary output format
• View is not materialized
– XML queries to the view with XML-QL or XQuery
– Problems
– 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
• Derive SQL queries from a view definition
• Processing of the whole view usually not neccessary
39
5.4 Individual mapping
40
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
5.4 Individual mapping
• Silkroute example
• Benefits of using standardized XML documents as
intermediate format
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>
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
XML
document
5.4 Individual mapping
• Implementing XML queries
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
XML query language
XML view
Database
– Less requirements, more flexibility, better
compatibility
XML
document
Database
Standard
transformation
XSLT stylesheet
Standardized
XML
document
41
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
42
5.4 Individual mapping
5. Mapping Rel2XML
• Individual mapping instructions – summary
5.1 Mapping to XML
– Database output – complete or partial
– Required information
5.2 Mapping tables
• XML query
• XSLT (see next section (5.5))
• View definition
5.3 Mapping query results
5.4 Individual mapping
– Variable output format – yes
– Preservation of data types – usually not, but possible to
deduce
– Storing keys and foreign keys
5.5 XSLT
5.6 Overview
• Embedding, ID/IDREF, XML schema
– Special features
5.7 References
• none
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
43
44
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
5.5 XSLT
5.5 XSLT
• XSLT
• XSLT processor
– 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 called stylesheet
• Can use elements of the XSLT namespace as well as of other
namespaces
• Contains template rules to execute the transformation
XSLT stylesheet
XSLT tree
and
and
XML document
Source tree
Transformation
process
Result tree
Result
document
XML file
XSLT
Stylesheet
XSLT
Processor
XML file
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
45
5.5 XSLT
46
5.5 XSLT
• XSLT processing model
• Template rules
– A rule consists of a pattern and 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
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
47
– 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 the best matching pattern from 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
5.5 XSLT
• Structure of a stylesheet
• Top level elements
– E.g. xsl:import, xsl:include, and most
importantly xsl:template
<xsl:stylesheet id={id}
extension-element-prefixes={token}
exclude-result-prefixes={token}
version=number>
<!-- Content: (xsl:import*, top-level-elements)-->
</xsl:stylesheet>
<xsl:template
match = {pattern}
name = {qname}
priority = {number}
mode = {qname}
<!-- Content: (xsl:param*, template) -->
</xsl:template
– 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
• Multiple matching patterns
– 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
– 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
51
5.5 XSLT
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
52
5.5 XSLT
• Rules
• XSLT contains default rules
– Can be named and be called in templates of other rules
– Can have parameters which can be passed along on
their invocation, default values can be defined
– The mode attribute allows a rule to be processed multiple
times and with different results
– If the template is invoked directly with xsl:calltemplate or xsl:apply-template, the filter
attributes (match, mode, priority or name) are not
processed
– Process the document recursivly
– But have lower priority than rules in the stylesheet
– Example:
<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
50
5.5 XSLT
• A pattern specifies a set of conditions to a
node
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
53
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
54
5.5 XSLT
5.5 XSLT
• Templates
• Instructions to process nodes recusively
– Can contain literal elements (non XSLT
namespace) and elements of the XSLT namespace
(instructions)
– If the rule is selected, the template can construct
fragments of the result tree
– Processing depends on the context
– Default behaviour is to write all elements which are
not in the XSLT namespace to the result tree
– Must be valid XML
– Can contain instructions
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
55
5.5 XSLT
<xsl:apply-template
select = {node set expression}
mode = {qname}>
<!-- Content: (xsl:sort, xsl:with-param)* -->
</xsl:apply-template>
– Without the attribute select all 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
• Instructions to create a node
• Instructions for flow control
<xsl:element
name = {qname}
namespace = {uri-reference}
use-attribute-sets = {qname} >
<!-- Content: template -->
</xsl:element>
– Conditional processing
<xsl:if
test = {boolean expression}
<!-- Content: template -->
</xsl:if>
required
– Test expression is evaluated and result is casted to a
boolean. If it is true the template will be instanciated
– Name attribute 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
58
5.5 XSLT
• Instructions for flow control
• Multiple choice ("if-then-else" / "switch")
• Repetition
<xsl:for-each
select = {node-set expression}
<!-- Content: (xsl:sort*, template) -->
</xsl:for-each>
<xsl:choose
<!-- Content: (xsl:when+, xsl:otherwise?) -->
</xsl:choose>
<xsl:when
test = {boolean expression}
<!-- Content: template -->
</xsl:when>
– 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
<xsl:otherwise
<!-- Content: template -->
</xsl:otherwise>
– If multiple xsl:when elements are true, only the first one is
processed (no "break" needed)
– If no xsl:when element is true and there is no xsl:otherwise, no
content is created
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
59
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
60
5.5 XSLT
5.5 XSLT
• "Calculation" of output text
• Other statements for sorting, numbering,
variables, …
<xsl:value-of
select = {string expression}
disable-output-escaping = "yes" | "no" />
– see http://www.w3.org/TR/xslt
• Some advice
– 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
– "select" is used differently depending on its context!
– Denomination "variable" is misleading!
– Context node is changed by for-each!
61
5.5 XSLT
62
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
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
<?xml version="1.0"?>
<resultset statement="SELECT * FROM t1"
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance">
<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>
63
5. Mapping Rel2XML
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
64
5. Mapping Rel2XML
• Conclusion
5.1 Mapping to XML
– Different methods to generate XML documents from
stored information exist
5.2 Mapping tables
5.3 Mapping query results
• Standard mapping of tables to XML
5.4 Individual mapping
• Standard mapping of query results
– Standard XML document: fixed output format
– Extend database queries by XML functionality
– Standard XML document
5.5 XSLT
• Individual mapping instructions
5.6 Overview
– XML views on database contents
– XML query languages processed on views
– Mapping using XSLT
5.7 References
– Focus on relational and object relational databases
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
65
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
66
5.6 Overview
1.
2.
3.
4.
5.
Introduction
XML Basics
Schema definition
XML query languages I
Mapping relational data
to XML
6. SQL/XML
7. XML processing
5.7 References
• 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]
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
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
67
Questions, Ideas, Comments
• Now, or ...
• Room:
IZ 232
• Office our:
Tuesday, 12:30 – 13:30 Uhr
or on appointment
• Email:
[email protected]
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
69
XML Databases – Silke Eckstein – Institut für Informationssysteme – TU Braunschweig
68
Herunterladen