Page tree
Skip to end of metadata
Go to start of metadata

XmlDomDataContext

The class XmlDomDataContext is no longer recommended, except for exploratory use. It will load the complete DOM of an XML file into memory and model tables based on each element that contains sub-elements or attributes with value-types in it. This makes it a good vehicle for visualizing or demonstrating the capability to read XML files, but unfortunately it also makes it very content-dependent and different XML files may not get the same schema model even if they share the same XSD.

XmlSaxDataContext

The XmlSaxDataContext is the newer and much improved DataContext implementation for XML files.

Since XML files are hierarical and MetaModel tables are tabular, you need to do some mapping. MetaModel provides a mapping model that is XPath based, with a few slight modifications.  

Assume we have the following XML document:

Now imagine that you want to have a table of employee names and gender information, and another table with company name and type information. We define our DataContext and those tables like this:  

As you see, we simply provide XPath expressions to 1) define the record scope and 2) define paths of individual values (or rather - the column definitions). If you query those tables, you will get datasets like these:  

Table: /employee  

 

row_id

/name

/gender

0

John Doe

M

1

Jane Doe

F

2

Peter

M

3

Bob

M

 

Table: /organization

 

row_id

/name

@type

0

Company A

governmental

1

Company B

company

This is nice, but you might be thinking: How can I then join these tables? There doesn't seem to be any cross-reference value that we can join or perform lookups by.  

To solve this issue, MetaModel provides a modification for XPath, the index(...) function. Say we want to add the organization's id to the employee table (as a foreign key). To archieve that, we will need this modified employee table definition (notice the third value XPath expression):  

Now if you query the employees table, this will be your result:  

 

row_id

/name

/gender

index(/root/organization)

0

John Doe

M

0

1

Jane Doe

F

0

2

Peter

M

1

3

Bob

M

1

Moving on, you will be able to define both joins and lookups using this foreign key. For example:

The contents of this queried dataset will now be:  

 

employee

company

John Doe

Company A

Jane Doe

Company A

Peter

Company B

Bob

Company B

  • No labels