Modern Markup Languages (aka PB138)

Lab 2

Hey, welcome to 2. lab!

Today we will play with XML schema and XSLT transformations.

And we will refresh our GIT knowledge

XML Schema

An XML Schema describes the structure of an XML document.

The XML Schema language is also referred to as XML Schema Definition (XSD).

 

 

XML Schema

The purpose of an XML Schema is to define the legal building blocks of an XML document:

  • the elements and attributes that can appear in a document
  • the number of (and order of) child elements
  • data types for elements and attributes
  • default and fixed values for elements and attributes
 
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="note">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="to" type="xs:string"/>
      <xs:element name="from" type="xs:string"/>
      <xs:element name="heading" type="xs:string"/>
      <xs:element name="body" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema>

Start with basic structure

<?xml version="1.0"?>

<xs:schema>
...
...
</xs:schema>

You can define element

A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes.

 

<xs:element name="xxx" type="yyy"/>

Types

xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time

Attributes

<xs:attribute name="lang" type="xs:string" default="EN"/>

can have also default value

<xs:attribute name="lang" type="xs:string" use="required"/>

and can be marked as required

There are restrictions

<xs:element name="age">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="120"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>

on value

There are restrictions

<xs:element name="car" type="carType"/>

<xs:simpleType name="carType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="Audi"/>
    <xs:enumeration value="Golf"/>
    <xs:enumeration value="BMW"/>
  </xs:restriction>
</xs:simpleType>

or as enum

We can define complex type

<xs:element name="employee" type="personinfo"/>
<xs:element name="student" type="personinfo"/>
<xs:element name="member" type="personinfo"/>

<xs:complexType name="personinfo">
  <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

one way

We can define complex type

<xs:element name="employee">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="firstname" type="xs:string"/>
      <xs:element name="lastname" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

or another

using max occurs we can include multiple elements

<xs:element name="employee" maxOccurs="unbounded">
  ...
</xs:element>

this slide was left intentionaly blank

XSLT

With XSLT you can transform an XML document into HTML.

Stands for XSL Transformations

XSLT is the most important part of XSL

XSLT uses XPath to navigate in XML documents

Looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Title</th>
      <th>Artist</th>
    </tr>
    <xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <td><xsl:value-of select="artist"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

Starts with stylesheet

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

...

Inside stylesheet lives template

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Title</th>
      <th>Artist</th>
    </tr>
    <tr>
      <td>.</td>
      <td>.</td>
    </tr>
  </table>
  </body>
  </html>
</xsl:template>

You can get one value

<xsl:value-of select="catalog/cd/title"/>

Or even sort it inside of for-each

<xsl:sort select="artist"/>

this slide was left intentionally blank

Quick GIT refresh

git clone https://gitlab.fi.muni.cz/pb138/seminars/seminar-02

Don't forget your login!

or use ssh

Let's try it :) 

PB138 Lab 2

By Lukáš Grolig

PB138 Lab 2

  • 512