XML technologies / Schematron / Schematron cookbook / Databases II
(Excerpt from "Schematron - Effiziente Business Rules für XML-Dokumente", chapter 8)
Another database example uses Schematron to check calculations. A company makes printers and sells, leases, lends or rents them to customers. Its actual profit comes from consumables such as ink and toner sold with the devices. Consumable prices depend on the printer’s distribution model, so the same item may have different prices.
For example, a customer receives a printer in exchange for committing to buy a given amount of toner over the next 12 months. Toner must then cost more than with a sold printer.
Interactive quotation creation needs XML containing the various pricing information, even details not immediately needed. Consumable prices for all distribution types can be retained and selected according to the order. The same XML export could also calculate each model’s margin to identify the economically best option.
Business logic could instead live in the database, emitting a suitable instance for each quotation. Database changes would then require reviewing and updating many rules. For complex applications, the separation above has therefore proved useful.
Moving business logic into XML introduces content constraints not enforceable through the XML schema. Database constraints could check some rules, but complex applications still let inconsistent master data or queries reach XML exports. Schematron is an additional quality check, not the sole language alongside XML Schema.
Here is a greatly simplified example:
Figure: database schema for quotation processing
A quotation schema groups offers for one or more customers.
Each customer (<kunde>) has an ID (<kundeId>) and a name (<name>). Ordered goods link to customers through an ID–IDREF relationship. The element identifying each product is <produktId>. Element <preis> records the customer’s turnover.
Products are collected in a same-named top-level element. Like customers, every product has an identifier <produktId> and a name (<name>). Customers ordering it are also linked through ID <kundeId>. Element <preis> specifies its price. The last top-level element, <preis>, holds the total price of all offered products.
This example contains logical errors explained below:
<angebot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="angebot.xsd">
<kunden>
<kunde>
<kundeId>KU000001</kundeId>
<name>data2type</name>
<produktId>PRO000001</produktId>
<produktId>PRO000002</produktId>
<preis>5.33</preis>
</kunde>
<kunde>
<kundeId>KU000002</kundeId>
<name>HAL</name>
<produktId>PRO000003</produktId>
<produktId>PRO000004</produktId>
<preis>10.33</preis>
</kunde>
</kunden>
<produkte>
<produkt>
<produktId>PRO000001</produktId>
<name>Toner 01 Schwarz</name>
<kundeId>KU000001</kundeId>
<preis>1.00</preis>
</produkt>
<produkt>
<produktId>PRO000002</produktId>
<name>Toner 01 Gelb</name>
<kundeId>KU000001</kundeId>
<preis>3.33</preis>
</produkt>
<produkt>
<produktId>PRO000003</produktId>
<name>Toner 01 Cyan</name>
<kundeId>KU000001</kundeId>
<kundeId>KU000002</kundeId>
<preis>1.00</preis>
</produkt>
<produkt>
<produktId>PRO000004</produktId>
<name>Toner 02 Schwarz</name>
<kundeId>KU000002</kundeId>
<preis>5.33</preis>
</produkt>
</produkte>
<preis>14.33</preis>
</angebot>Several errors are possible even here. Product prices and order totals are related: the value of all goods per customer, content of <preis> within <kunde>, must equal the sum of offered product prices. The overall total must also equal the sum of customer totals. This example violates those rules. Customer HAL received an offer for products Toner 01 Cyan and Toner 02 Schwarz , totaling 6.33. Instead, that customer’s <preis> is 10.33.
The pattern checking this business rule is:
<pattern id="kundenpreis">
<rule context="kunde/preis">
<let name="kundeId" value="parent::kunde/kundeId"/>
<assert test="sum(//produkt[kundeId=$kundeId]/preis)=.">Die Summe stimmt an dieser Stelle nicht. Sie müsste <value-of select="sum(//produkt[kundeId=$kundeId]/preis)"/> lauten.</assert>
</rule>
</pattern>Another error concerns the order total, entered as 14.33 although the products actually total 11.66. This pattern rules out that error:
<pattern id="angebotspreis">
<rule context="angebot/preis">
<assert test="sum(//kunde/preis)=.">Die Summe stimmt an dieser Stelle nicht. Sie müsste <value-of select="sum(//kunde/preis)"/> lauten.</assert>
</rule>
</pattern>DTDs and XML Schema check ID–IDREF relationships and whether an offered product’s customer exists, but not whether that product also lists the customer.
This product was offered to two customers:
<produkt>
<produktId>PRO000003</produktId>
<name>Toner 01 Cyan</name>
<kundeId>KU000002</kundeId>
<kundeId>KU000001</kundeId>
<preis>1.00</preis>
</produkt>
But customer (<kundeId>KU000001</kundeId>) lacks the correct product (<produktId>PRO000003</produktId>).
<kunde>
<kundeId>KU000001</kundeId>
<name>data2type</name>
<produktId>PRO000001</produktId>
<produktId>PRO000002</produktId>
<preis>4.33</preis>
</kunde>Check every customer’s product-ID list for completeness:
<pattern id="verbindungProdukt">
<rule context="kunde">
<let name="missings" value="//produkt[kundeId = current()/kundeId]/produktId[not(.=current()/produktId)]"/>
<report test="count($missings) > 0">Hier fehlt/en die Produkt-ID(s): <value-of select="string-join($missings, ', ')"/>.</report>
</rule>
</pattern>
Copyright © dpunkt.verlag GmbH 2011
You may print the online version for private use. Otherwise, this chapter from "Schematron - Effiziente Business Rules für XML-Dokumente" is subject to the same terms as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
dpunkt.verlag GmbH, Ringstraße 19B, 69115 Heidelberg, phone 06221-14830, fax 06221-148399, hallo(at)dpunkt.de
back to top