XML Schema
An XML schema is a description of a type of XML document. SlashDB Data Discovery and SQL Pass-thru can return data in various formats, including XML and XSD (XML Schema). The schema data provides end users with several capabilities when working with XML documents:
- Validation of XML document against XML Schema – XML data can be checked to ensure it conforms to a format as specified in the XSD document before uploading via SlashDB or using the data in other workflows
- Documentation of XML document elements – provides information about:
- nesting of XML elements
- order and cardinality of elements
- XML element attributes like
href
- Documentation of resource properties – provides information about:
- names of resources
- names of resource attributes
- data types of resource attributes
- whether resource attributes can be
null
- Querying Support for XPath, XSLT and XQuery
- Data Binding for programmatic purposes by converting XML document to in-memory objects that are more accessible by applications
- It's even possible to map XML elements to cells in Excel
XML Schema for Data Discovery
In Data Discovery, a resource can be retrieved in XML format with a GET request. A corresponding XML schema can be retrieved by making a GET request to the same URL by using the .xsd
file extension, or including a text/xsd
Accept
header in the request.
A simple example of retrieving XML and XSD data:
# XML
curl "https://demo.slashdb.com/db/Chinook/Customer.xml"
# XML Schema
curl "https://demo.slashdb.com/db/Chinook/Customer.xsd"
It is also possible to retrieve XML/XSD documents using more complex URLs that include SlashDB features like filtering, relationship traversal, column selection, depth, etc.
For example:
# XML
curl -L -H "Accept: text/xml" "https://demo.slashdb.com/db/Chinook/Customer/City/Prague/Invoice?offset=2&llimit=1&sort=-Total&depth=1"
# XML Schema
curl -L -H "Accept: text/xsd" "https://demo.slashdb.com/db/Chinook/Customer/City/Prague/Invoice?offset=2&llimit=1&sort=-Total&depth=1"
Requests for XML schema accept additional modifiers e.g. cardinality
that change the returned schema.
Example
Let's take a single Customer
by making an HTTP request:
curl https://demo.slashdb.com/db/Chinook/Customer/CustomerId/10.xml
The response body contains XML with data from a single row – i.e. a single resource:
<?xml version="1.0" encoding="utf-8"?>
<SlashDB xmlns="http://www.vtenterprise.com/slashdb" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://www.vtenterprise.com/slashdb https://beta.slashdb.com/db/Chinook/Customer.xsd?cardinality=1">
<Customer href="/db/Chinook/Customer/CustomerId/10.xml">
<CustomerId>10</CustomerId>
<Invoice href="/db/Chinook/Customer/CustomerId/10/Invoice.xml" />
<FirstName>Eduardo</FirstName>
<LastName>Martins</LastName>
<Company>Woodstock Discos</Company>
<Address>Rua Dr. Falcão Filho, 155</Address>
<City>São Paulo</City>
<State>SP</State>
<Country>Brazil</Country>
<PostalCode>01007-010</PostalCode>
<Phone>+55 (11) 3033-5446</Phone>
<Fax>+55 (11) 3033-4564</Fax>
<Email>eduardo@woodstock.com.br</Email>
<SupportRepId>4</SupportRepId>
<Employee href="/db/Chinook/Customer/CustomerId/10/Employee.xml" />
</Customer>
</SlashDB>
In the above output:
SlashDB
is the root element in the XML document that wraps all other elementsCustomer
is an element representing a single resource; it has ahref
attribute that points to the resourceCustomerId
,FirstName
,LastName
,Company
,Address
,City
,State
,Country
,PostalCode
,Phone
,Fax
,Email
,SupportRepId
are elements whose names correspond to columns in the database and contain the values for the resourceInvoice
andEmployee
are elements that represent relationships to other tables; they havehref
attributes that point to resources related to thatCustomer
, i.e. invoices issued for the customer and employee
A corresponding XML schema that describes the resource Customer
can be retrieved by making an HTTP request to a URL by using the .xsd
file extension, or including a text/xsd
Accept
header in the request:
curl -L https://beta.slashdb.com/db/Chinook/Customer/CustomerId/10.xsd
This will redirect to the canonical location:
curl https://beta.slashdb.com/db/Chinook/Customer.xsd?cardinality=1
The final response contains an XSD document that describes the Customer
resource:
<?xml version='1.0' encoding='utf-8'?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.vtenterprise.com/slashdb" elementFormDefault="qualified" targetNamespace="http://www.vtenterprise.com/slashdb">
<xsd:element name="SlashDB">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Customer">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CustomerId" nillable="false" type="xsd:integer" minOccurs="0"/>
<xsd:element name="Invoice" minOccurs="0">
<xsd:complexType>
<xsd:attribute name="href"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="FirstName" nillable="false" type="xsd:string" minOccurs="0"/>
<xsd:element name="LastName" nillable="false" type="xsd:string" minOccurs="0"/>
<xsd:element name="Company" nillable="true" type="xsd:string" minOccurs="0"/>
<xsd:element name="Address" nillable="true" type="xsd:string" minOccurs="0"/>
<xsd:element name="City" nillable="true" type="xsd:string" minOccurs="0"/>
<xsd:element name="State" nillable="true" type="xsd:string" minOccurs="0"/>
<xsd:element name="Country" nillable="true" type="xsd:string" minOccurs="0"/>
<xsd:element name="PostalCode" nillable="true" type="xsd:string" minOccurs="0"/>
<xsd:element name="Phone" nillable="true" type="xsd:string" minOccurs="0"/>
<xsd:element name="Fax" nillable="true" type="xsd:string" minOccurs="0"/>
<xsd:element name="Email" nillable="false" type="xsd:string" minOccurs="0"/>
<xsd:element name="SupportRepId" nillable="true" type="xsd:integer" minOccurs="0"/>
<xsd:element name="Employee" minOccurs="0">
<xsd:complexType>
<xsd:sequence/>
<xsd:attribute name="href"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="href"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
The XSD contains information about the Customer
resource and its corresponding XML document:
- the root element is named
SlashDB
- it contains a complex type named
Customer
with attributehref
which is required becauseminOccurs
andmaxOccurs
are by default1
- Nested inside are:
- simple elements like:
CustomerId
,FirstName
,LastName
,Company
,Addrress
,City
,State
,Country
,PostalCode
,Phone
,Fax
,Email
,SupportRepId
. Each of them have attributes:nillable
defines if empty values are allowed,- data types (e.g.
xsd:string
) - cardinality, whether the element must appear in the XML document:
minOccurs="0"
means an element can be omittedmaxOccurs
is not present, so defaults to value1
which means that an element cannot appear more than once
- complex elements like:
Invoice
orEmployee
with attributehref
indicate that they are related resources
- simple elements like:
XML Schema for SQL Pass-thru
In SQL Pass-thru, a pre-defined query can be executed and retrieve data in XML format with a GET request. A corresponding XML schema can be retrieved by making a GET request to the same URL by using the .xsd
file extension, or including a text/xsd
Accept
header in the request.
Requests for an XML schema accept additional modifiers e.g. cardinality
that change the returned schema.
Important Note: XML Schema is only available for queries that return data.
When an XSD is requested, SlashDB executes the query and fetches one row to discover the returned data types, then creates the XML schema based on that response.
Example
Let's execute an SQL Pass-thru query by making an HTTP request:
curl https://demo.slashdb.com/query/sales-by-year.xml
The returned response contains XML data:
<?xml version="1.0" encoding="utf-8"?>
<SlashDB xmlns="http://www.vtenterprise.com/slashdb" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://www.vtenterprise.com/slashdb https://demo.slashdb.com/query/sales-by-year.xsd">
<row>
<Year>2009</Year>
<Total>451.44000000000034</Total>
</row>
<row>
<Year>2010</Year>
<Total>481.45000000000033</Total>
</row>
<row>
<Year>2011</Year>
<Total>469.5800000000003</Total>
</row>
<row>
<Year>2012</Year>
<Total>477.53000000000026</Total>
</row>
<row>
<Year>2013</Year>
<Total>450.58000000000027</Total>
</row>
</SlashDB>
In the above output:
SlashDB
is the root element in the XML document that wraps all other elements- Nested inside are repeated
row
elements that represent each row returned by the SQL query - Each
row
element contains an element for each column returned:Year
andTotal
A corresponding XML schema that describes the query results can be retrieved by making HTTP request to the same query but modifying the URL to use extension .xsd
, or by using the or by using the text/xsd
Accept
header:
curl https://demo.slashdb.com/query/sales-by-year.xsd
The response contains an XSD document that describes the data structure that the SQL query previously returned:
<?xml version='1.0' encoding='utf-8'?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.vtenterprise.com/slashdb" elementFormDefault="qualified" targetNamespace="http://www.vtenterprise.com/slashdb">
<xsd:element name="SlashDB">
<xsd:complexType>
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="row">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Year" type="xsd:integer" minOccurs="0" nillable="true"/>
<xsd:element name="Total" type="xsd:float" minOccurs="0" nillable="true"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
from the schema, it can be seen that:
- the root element is named
SlashDB
- nested inside are an unlimited number of
row
elements - each
row
has two elements:Year
with data type integerTotal
with data type float
minOccurs=0
andnillable
indicate that the values may be empty