XML technologies / Schematron / Schematron cookbook / Databases I

(Excerpt from "Schematron - Effiziente Business Rules für XML-Dokumente", chapter 8)

Database applications often exchange data through XML. This example uses a book-sales portal: a reporting tool lists sellers, groups turnover by currency and account, and includes every book sold. The basic document structure is:

Database schema of an online book portal

Figure: database schema of an online book portal

The report header contains metadata such as issue date, report name and invoice date. Grouping levels each have a container (for example <buchGrp1>), with a grouping key (for example <buchKeyGrp1>).

The final level lists book details without further grouping (<ISBN-10>, <ISBN-13>, <name>, <autorNachname>, <verlag>, <mwst> and <preis>).

Each group ends with its turnover total, for example per account <sumPreisKonto> or per seller <sumPreisHaendler>. This structure imports directly into Excel without further conversion.

The figure shows such a view:

Importing XML into Excel

Figure: importing XML into Excel

Even thoughtful, complex structures have recurring problems involving implicit content constraints. Here are examples of such dependencies:

<buch xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="buch.xsd">
  <berichtKopf>
    <berichtname>Buchverkauf</berichtname>
    <ausgabedatum>2008-11-25</ausgabedatum>
    <rechnungsdatum>2008-11-24</rechnungsdatum>
  </berichtKopf>
  <buchGrp>
    <buchKeyGrp>
      <haendler>Buch Kosmos</haendler>
    </buchKeyGrp>
    <buchGrp1>
      <buchKeyGrp1>
        <waehrung>EUR</waehrung>
      </buchKeyGrp1>
      <buchGrp2>
        <buchKeyGrp2>
          <konto>1</konto>
        </buchKeyGrp2>
        <buchRec>
          <ISBN-10>389667367X</ISBN-10>
          <ISBN-13>978-3896673671</ISBN-13>
          <name>Der Katalane</name>
          <autorNachname>Gordon</autorNachname>
          <verlag>Blessing</verlag>
          <mwst>7</mwst>
          <preis>19.95</preis>
        </buchRec>
        <buchRec>
          <ISBN-10>3442369762</ISBN-10>
          <ISBN-13>978-3442369768</ISBN-13>
          <name>Hals über Kopf</name>
          <autorNachname>Reichs</autorNachname>
          <verlag>Blanvalet</verlag>
          <mwst>7</mwst>
          <preis>8.95</preis>
        </buchRec>
        <buchRec>
          <ISBN-10>3886808637</ISBN-10>
          <ISBN-13>978-3886808632</ISBN-13>
          <name>Außer Dienst: Eine Bilanz</name>
          <autorNachname>Schmidt</autorNachname>
          <verlag>Siedler</verlag>
          <mwst>7</mwst>
          <preis>22.95</preis>
        </buchRec>
        <sumPreisKonto>41.85</sumPreisKonto>
      </buchGrp2>
      <buchGrp2>
        <buchKeyGrp2>
          <konto>2</konto>
        </buchKeyGrp2>
        <buchRec>
          <ISBN-10>3886808637</ISBN-10>
          <ISBN-13>978-3886808632</ISBN-13>
          <name>Außer Dienst: Eine Bilanz</name>
          <autorNachname>Schmidt</autorNachname>
          <verlag>Siedler</verlag>
          <mwst>7</mwst>
          <preis>22.95</preis>
        </buchRec>
        <sumPreisKonto>22.95</sumPreisKonto>
      </buchGrp2>
      <sumPreisWaehrung>74.80</sumPreisWaehrung>
    </buchGrp1>
    <sumPreisHaendler>
      <waehrung>CHF</waehrung>
      <preis>74.80</preis>
    </sumPreisHaendler>
  </buchGrp>
  <sumPreis>
    <waehrung>EUR</waehrung>
    <preis>74.80</preis>
  </sumPreis>
</buch>

Implicit rules

Element <sumPreisKonto> must contain the sum of all <preis> elements within its parent <buchGrp2>, the account container. This pattern checks the sum:

<pattern id="sum">
  <rule context="sumPreisKonto">
    <assert test="format-number(sum(parent::*/.//preis),'###0.00')=.">Die Summe stimmt an dieser Stelle nicht. Sie müsste <value-of select="format-number(sum(parent::*/.//preis),'###0.00')"/> lauten.</assert>
  </rule>
</pattern>

Next check whether a seller traded in currencies differing from the total’s currency. Here the seller traded in EUR but the total uses CHF.

<buchGrp1>
  <buchKeyGrp1>
    <waehrung>EUR</waehrung>
  </buchKeyGrp1>
  <buchGrp2>
  …
    <sumPreisHaendler>
      <waehrung>CHF</waehrung>
      <preis>74.80</preis>
    </sumPreisHaendler>

This pattern reports the discrepancy:

<pattern id="waehrung">
  <rule context="sumPreisHaendler">
    <assert test="parent::buchGrp/buchGrp1/buchKeyGrp1/waehrung=current()/waehrung">Es werden hier unterschiedliche Währungen verwendet.</assert>
  </rule>
</pattern>

Another implicit rule requires knowledge of business processes. Here the principle is that <konto>2</konto> may be used for non-euro transactions. This pattern checks it:

<pattern id="konto">
  <rule context="konto[text()='2']">
    <report test="ancestor::buchGrp1/*/waehrung='EUR'">Im Konto 2 darf die Währung nicht EUR sein!</report>
  </rule>
</pattern>

   

 

 

 

back to top