XML

XSL

  • XSL = Xml Style Sheet Language for XML
  • XSL consists of three parts:
    • XSLT - a language for transforming XML documents
    • XPath - a language for navigating in XML documents
    • XSL-FO - a language for formatting XML documents

XSLT

  • XSLT is a style sheet language for XML.
  • XSLT (eXtensible Stylesheet Language Transformations) is for xml what css is for html!
  • XSLT is good for transforming xml into other forms such as plain text, csv, another xml, xhtml, html, etc.
  • All you need for start is a xml and an xsl file.

Example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="my.xsl"?>
<list>
    <offerings>
        <promotion>promo2</promotion>
        <plans>
            <plan>p3</plan>
            <plan>p2</plan>
            <plan>p1</plan>
            <plan>p11</plan>
        </plans>
    </offerings>
 
    <offerings>
        <promotion>promo1</promotion>
        <plans>
            <plan>p2</plan>
            <plan>p3</plan>
            <plan>p7</plan>
        </plans>
    </offerings>
    <offerings>
        <promotion>promo3</promotion>
        <plans>
            <plan>p9</plan>
            <plan>p8</plan>
            <plan>p0</plan>
        </plans>
    </offerings>
    <offerings>
        <promotion>promo1</promotion>
        <plans>
            <plan>p7</plan>
            <plan>p3</plan>
            <plan>p2</plan>
        </plans>
    </offerings>
</list>

The XSL file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
 
    <xsl:template match="/">
        <list>
        <xsl:apply-templates/>
        </list>
    </xsl:template>
 
    <xsl:template match="list">
        <xsl:for-each select="offerings">
            <xsl:sort select="promotion"/>
            <promotion><xsl:value-of select="promotion"/></promotion>
            <xsl:apply-templates select ="plans"/>
        </xsl:for-each>
    </xsl:template>
 
    <xsl:template match="plans">
        <xsl:for-each select="plan">
            <xsl:sort select="."/>
            <plan><xsl:value-of select="."/></plan>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

XQuery

  • is a query language for xml.
  • At the heart of XQuery is the FLWOR expression which is an acronym for "For, Let, Where, Order by, Return".
  • XQuery for XML is like SQL for databases
  • XQuery is built on XPath expressions
  • There is a difference between Let and For that you have to remember. For is an ITERATOR and iterates over items but let is an assignment and can hold one or a collection of values.
The following just returns one number; the number of xx elements.

let $h :=  collection('xx')/xx 
return count($h) 

But this one ITERATES and returns 1 n times where n is the number of xx elements.

for $h in collection('xx')/xx
return count($h) 

If the number of xx elements is 8 then the result is a sequence of numbers: 1 1 1 1 1 1 1 1.
  • Simple Example:
declare function local:sort_plans( $ss as node() ) as node()
{
 $ss   (:does not need return statement :)
};

for $ofrns in //offerings
order by 
 $ofrns/promotion
return local:sort_plans($ofrns)
  • Another Example:
declare function local:sort_plans( $product as node()* ) as node()*
{
for $y in $x/y/x
 order by 
 $y/productName
 return $y/productName
};

for $product in //x
order by 
 $product/id, $product/type

return local:sort_plans($product)

Reference:

XSD

  • XML Schema Definition (XSD) is a type system (language?) for xml
  • An xml document that conforms to an XSD is said to be an instance document which is similar to object of a class in OO languages.

XSD Structure

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.org/publishing"
   xmlns:tns="http://example.org/publishing">
 
   <!-- type definitions -->
   <xsd:simpleType name="AuthorId">
      ...
   </xsd:simpleType>
 
   <xsd:complexType name="AuthorType">
      ...
   </xsd:complexType>
 
   <xsd:element name="author" type="tns:AuthorType"/>
   <xsd:attribute name="authorId" type="tns:AuthorId"/>
   ...
</xsd:schema>
  • To use a namespace-qualified name we need another namespace declaration (tns) that maps to the schema's targetNamespace.
  • Notice defining simple and complex type in the XSD. Also elements and attributes.
  • XML instances as examples:
<!-- Using author element -->
<x:author xmlns:x="http://example.org/publishing">
  ...
</x:author>
 
<!-- Using authorId attribute -->
<publication xmlns:x="http://example.org/publishing"  x:authorId="333-33-3333"/>

XSD data types

  • XSD has a couple of simple types such as byte, short, int, …
  • In XSD we can define subsets of simple types unlike most programming languages. Facets such as restriction, minInclusive, maxInclusive, length, pattern, totalDigits, etc help doing that.
  • There are other facets that do not make subsets of data types but expand them such as: union, list, enumeration, etc
   <xsd:simpleType name="RoyaltyRate">
      <xsd:restriction base="xsd:double">
         <xsd:minInclusive value="0"/>
         <xsd:maxInclusive value="100"/>
      </xsd:restriction>
   </xsd:simpleType>
 
   <xsd:simpleType name="Pubs2003">
      <xsd:restriction base="xsd:date">
         <xsd:minInclusive value="2003-01-01"/>
         <xsd:maxInclusive value="2003-12-31"/>
      </xsd:restriction>
   </xsd:simpleType>
 
   <xsd:simpleType name="Phone">
      <xsd:restriction base="xsd:string">
         <xsd:pattern value="\(\d{3}\)\d{3}-\d{4}"/>
      </xsd:restriction>
   </xsd:simpleType>
 
   <xsd:element name="rate" type="tns:RoyaltyRate"/>
   <xsd:element name="publicationDate" type="tns:Pubs2003"/>
  • Examples:
<x:phone xmlns:x="http://example.org/publishing">(801)390-4552</x:phone>
<x:publicationDate xmlns:x="http://example.org/publishing">2003-06-01</x:publicationDate>

Complex Types

  • complexType element uses compositors (=sequence, choice and all). Compositors contain particles which include other compositors, element declarations, wildcards, etc.
  • Elements and attributes within the complexType element are considered local and do not need to be namespace qualified by default.
<xsd:complexType name="AuthorType">
      <xsd:sequence>
         <xsd:element name="name" type="xsd:string"/>
         <xsd:element name="phone" type="tns:Phone"/>
      </xsd:sequence>
      <xsd:attribute name="id" type="tns:AuthorId"/>
   </xsd:complexType>
   <xsd:element name="author" type="tns:AuthorType"/>
  • Example
<x:author xmlns:x="http://example.org/publishing"   id="333-33-3333">
   <name>Aaron Skonnard</name>
   <phone>(801)390-4552</phone>
</x:author>
  • You can reference global elements/attributes from within a complex type using the ref attribute.
   <xsd:attribute name="id" type="tns:AuthorId"/>
   <xsd:element name="name" type="xsd:string"/>
   <xsd:element name="author" type="tns:AuthorType"/>
 
<xsd:complexType name="AuthorType">
      <xsd:sequence>
         <xsd:element ref="tns:name"/>
         <xsd:element name="phone" type="tns:Phone"/>
      </xsd:sequence>
      <!-- reference to global attribute -->
      <xsd:attribute ref="tns:id"/>
</xsd:complexType>

Wildcard

By default complex types have closed content models. This means that only the specified particles are allowed to appear in an instance. XML Schema makes it possible, however, to define an open content model using what are known as wildcards. Using xsd:any within a complex type means that any element can appear at that location, effectively making it a placeholder for things that you cannot predict ahead of time. You can also use xsd:anyAttribute to define placeholders for attributes.

References:

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License