Virtual Enterprises 2023/2024
VE-TLab8- e-Market / e-Commerce for Collaborative Networks
Annex
Ana Inês Oliveira & Filipa Ferrada
NOVA School of Sciences and Technology (FCT NOVA)
[email protected],
[email protected]The Lab8 work is composed of several levels whose objective is to learn how to model digital electronic
documents. At the different levels we will proceed with the modeling with XML, XSD and Schematron.
Level 0 – Installation of necessary tools.
1- Extract the VE-TLab8_base.zip package in the c:\ev\lab8 directory
2- In case you haven’t’ it yet, install “Visual Studio Code”, see slides Lab3.
3- Install the extension “XML Language Support by Red Hat”
Virtual Enterprises 2023/2024
Level 1- XML Modeling
General information of XML can be found for example in https://whatis.techtarget.com/definition/XML-
Extensible-Markup-Language , where: “Extensible Markup Language (XML) is used to describe data. The
XML standard is a flexible way to create information formats and electronically share structured data via
the public Internet, as well as via corporate networks.
XML code, a formal recommendation from the World Wide Web Consortium (W3C), is similar to Hypertext
Markup Language (HTML). Both XML and HTML contain markup symbols to describe page or file contents.
HTML code describes Web page content (mainly text and graphic images) only in terms of how it is to be
displayed and interacted with.
XML data is known as self-describing or self-defining, meaning that the structure of the data is embedded
with the data, thus when the data arrives there is no need to pre-build the structure to store the data; it
is dynamically understood within the XML. The XML format can be used by any individual or group of
individuals or companies that want to share information in a consistent way.
Many aspects about XML documents will be omitted from this work. The concepts can be further explored
at https://www.tutorialspoint.com/xml/index.htm and at https://www.w3schools.com/xml/ . Regarding
the VS Code XML extension by Red Hat, check docs at: https://github.com/redhat-developer/vscode-
xml/blob/master/docs/Validation.md#validation-with-xsd-grammar
For this level, to create an XML document, create a directory, for example c:\ev\lab8\documents (if you
extracted the zip file for c:\ev\lab8, the directory documents already exist.
Open VS Code, choose the option File > New Text File > Select a Language > XML (xml), and create it as
illustrated in the figures below.
Virtual Enterprises 2023/2024
Edit the following XML document in the created file:
As you edit, check the tool's behavior in terms of document validation, carried out as you edit.
Open the lab8 folder and save the xml file, in folder documents, as “example_01”.
Virtual Enterprises 2023/2024
After having edited the example above, create a new document example_02.xml so that it can contain
several contacts, namely, “professional”, “residential”, “holidays”, etc. In structural terms, the name,
gender, birthdate, etc. must first appear. Then the various address contacts should appear, as illustrated
in the figure below.
Check that below there is an attribute called “category”, which stipulates the type of contact. Also add
additional elements, for example the street, the city, the zip code, etc. Get creative by adding additional
elements and multiple address categories.
Delivery material for level 1:
- Do research, identify, and list what are the typical uses of XML.
- Include your XML example and describe the different components that exist.
Virtual Enterprises 2023/2024
Level 2 - Validation of XML documents with XSD schemas
Validation is the process of verifying that the document is well written and that its content satisfies a
structure (Document Type Definition: DTD) or validation schema (XML Schema Definition: XSD).
XSD is used to describe and validate the structure and content of XML data. The XML schema defines the
elements, attributes and data types. The Schema element supports namespaces. It is similar to a database
schema that describes the data in a database. Therefore, the main purpose of an XML Schema is to define
the legal building blocks of an XML document, namely:
- The elements and attributes that can appear in a document
- The number (and order of) of the child elements
- Data types for elements and attributes
- Predefined and fixed values for elements and attributes
Creation of a XSD schema. Within the c:\ev\lab8 directory, create a new directory named
c:\ev\lab8\schemas\. (again, if you extracted the zip file for c:\ev\lab8, the directory schemas already
exist). In schemas, create a XSD document named contact-info.xsd, as illustrated in the figure below.
Add the xml declaration, and then add the “New XML Schema” as illustrated above. The result is depicted
below:
Edit the new document with the content shown in the figure below. While this is being edited, take time
to get familiarized with the XSD validation rules.
Virtual Enterprises 2023/2024
Now create a new XML document named example_03.xml. Copy the contents of the example_02.xml
document into this one. Link the XSD schema created earlier to this document, as described in the figure
below.
Virtual Enterprises 2023/2024
The result of this association should be the <contact-info> element, now associated with the validation
scheme defined before, as illustrated in the figure below.
As you can see, the xml file is being validated while you are editing it. Check the problems windows to
see the description of errors:
Can you find out how to solve this problem?
Make changes to the file imposing validation errors and correct them so that you can check some error
types.
Add more elements to the XSD schema. Make the consequent modifications to the XML document so
that it conforms to its schema again.
Virtual Enterprises 2023/2024
Delivery material for level 2:
- Description of how XSD validation works and its usefulness
- Characterize the various elements used within the provided XSD schema
- Include your XML and XSD documents
Level 3 - Validation of XML documents with Schematron schemas
What is it? Allows you to express restrictions and rules that other validation schemes (e.g., DTD or XSD)
cannot. For example, it can check data interdependence, cardinality, and “algorithmic” checks. It is
typically used in finance, insurance, government, etc. Schematron has six basic elements: assert, report,
rule, pattern, schema, ns.
For an in-depth study of this type of validation, and given that Schematron works with xPath expressions,
we suggest reading the xPath.pdf and schematronTutorial.pdf documents provided in VE-TLab8_base.
To understand how Schematron validation works, we are going to use a simplified digital version of an
invoice (factura_2021_325.xml), in a strategy based on examples. The invoice is shown in the figure below
(not complete).
Virtual Enterprises 2023/2024
As it turns out, an invoice needs to follow the typical rules established in commercial transactions, for
example, an invoice number, the customer and the lines (or table) of the items sold. Each item sold must
in turn have a reference (or barcode number), description, quantity, unit price and percentage of tax, e.g.:
VAT 23%.
In order to guarantee these restrictions, we could use a XSD validation, as described in the previous level.
However, XSD cannot guarantee full validation of invoice elements. For example, it is necessary to ensure
that the Net Total of the invoice is equal to the sum of the Net Total of each line, or that the Total VAT
corresponds to the sum of the VAT for each line.
To carry out this type of validation, a Schematron model built for this purpose will be used. However, this
model needs to be completely specified, which is one of the objectives of this level.
Using VS Code, open the invoice_2021_325.xml document, provided in the annex, which represents the
invoice described above. Then open the validation document factura.sch, as illustrated in the figure
below.
Now open the Free Online XML Validator (Schematron) (https://www.liquid-technologies.com/online-
schematron-validator) and copy-paste the invoice_2021_325.xml document to the “XML data to
validate” (delete the existing comment), and then copy-paste the factura.sch to the “Schematron data”
(also deleting the existing comment). Then click on “Validate”.
Virtual Enterprises 2023/2024
What is the validation result? How to solve the error?
Constraints of this type are defined in the Schematron model as illustrated in the figure below. In the
example given, the rule was defined within the “context” Cliente, or within the tag <Cliente>. Other rules
check, for example, the existence of the invoice and customer number. As it turns out, the model is
incomplete, requiring, for example, the restriction that indicates that the invoice number must start with
FA, followed by a space, 4 digits, followed by the '/' character and finally followed by 4 digits (this is
necessary use a “Regular Expression”).
Virtual Enterprises 2023/2024
Other verification examples provided by Schematron consist of verifying that the total values (net, VAT,
gross total) are consistent with the values placed in the invoice lines. Some of these rules are illustrated
in the figure below.
In the invoice document, in Free Online XML Validator, try modifying the tax amounts by a few cents.
After validating, alert messages appear indicating that the model is not valid.
Expand the Schematron-based validation approach. Complete the validation document, with the rules
that you can specify, in order to have the most complete validation possible.
Delivery material for level 3:
-Include your XML and SCH documents
-Describe which rules are added to the invoice template in order to have the most
complete/comprehensive validation possible.