When you’re working with XML web services, you’ll often come across the terms WSDL and XSD. But do you know what they do, and what makes them different from each other? Read on to find out more.
What is XSD?
XSD, or XML Schema Definition, is a language for describing what an XML document should look like. XSD helps a computer system to validate an XML document.
You can use XSD to define the structure and rules for an XML document. XSD allows you to declare things like:1
The list of elements which are allowed in an XML document, and a description of each element
The order of those elements
What contents the elements can contain (e.g. string or date)
Whether elements can contain more (child) elements
XSD can be used to validate any XML document – whether it’s received by email, file transfer, or a web service call. So it’s often used in backend processes to make sure that data is OK before processing it. If an XML document passes validation, it’s sometimes called schema-valid.
An XML Schema Definition is itself just an XML document2, so it looks like a regular XML file. When a document is written in XML Schema Definition language, it’s often saved with the extension
XSD is a standard, agreed by the W3C. There are other ways to write rules for XML documents (like Document Type Definitions (DTDs), Relax-NG and Schematron) but XSD is probably the most popular way to validate an XML document.
What does it look like?
You begin an XSD document with the root element,
schema. You define each tag in the XML document using the tag
element. Each of these elements can contain simple content (which means something like a string or a date), or more complex data (like nested elements).
For example: You’re writing an XSD for an XML document to describe a Customer. In the XSD, you might declare that a Customer must have 1 Address, and that each Address must contain a State, but the Zip code is optional:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="Customer" type="CustomerType"/> <xsd:complexType name="CustomerType"> <xsd:sequence> <xsd:element name="Name" type="xsd:string"/> <xsd:element name="Address" type="AddressType"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="AddressType"> <xsd:sequence> <xsd:element name="Street" type="xsd:string"/> <xsd:element name="City" type="xsd:string"/> <xsd:element name="State" type="xsd:string"/> <xsd:element name="ZipCode" type="xsd:string" minOccurs="0"/> <xsd:element name="Country" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
We have an XML document that we’d like to validate against this XSD:
<Customer> <Name>Bob Carolgees</Name> <Address> <Street>1 Homer Street</Street> <City>Flanville</City> <State>BZ</State> <Country>DE</Country> </Address> </Customer>
We could now validate this with the xmllint tool on Linux:
xmllint --schema customer.xsd bob.xml
If you try it yourself, you should see that the message “customer.xml validates”. Success!
What can you do with XSD?
So how is XSD used in the real world?
Write an XSD. You can create an XSD document and write rules in it, using a text editor, or specialised software like XMLSpy.
Validate with a command-line tool. You can use a tool like xmllint to validate XML documents using your XML Schema definition.
Validate in your code. Most programming languages have libraries to validate XML documents using XSD, so you can add validation to your own applications. For example, in Python you might use the xmlschema package.
Validation in another app. When a software application receives some data in XML format (e.g. from a business partner), it might use XSD to validate the incoming XML document.
Web services. If a program exposes an XML web service, it might use an XSD to check that its inbound and outbound messages are valid.
What is WSDL?
WSDL, or Web Services Description Language, is an XML-based language for describing web services.3
WSDL defines a standard set of XML elements, which describe all the features of a web service. You write a WSDL document, using these elements to describe your web service.
You can use WSDL to describe a web service, so that potential consumers of the service know how to use it. A WSDL document declares things like:
The operations you can call on the service
How the input and output messages should look
Which protocol or data format you should use to access the service
The location of the service (e.g. its HTTP endpoint)
WSDL looks like a regular XML file, and it’s usually saved with the
WSDL is also a standard from the W3C. You don’t have to use WSDL, but since it documents a web service completely, it’s become a common way to share information about web services (especially SOAP web services). Because of its widespread adoption, there are many tools which can understand WSDL files and use them to connect to web services.
What does it look like?
There are two major versions of WSDL:
A WSDL 1.1 document starts with the root element,
A WSDL 2.0 document starts with the root element,
WSDL contains elements to define the web service operations, data types, protocol and data formats, and location of the service. Inside the WSDL document, you can use XML Schema (XSD) to define the messages which should be received and sent by the web service.
What can you do with WSDL?
WSDL has been around for a long time, although it’s still used for lots of internal systems. So what can you do with it?
Write a WSDL. You can write a WSDL document using any text editor. It’s written in XML.
Describe your web service. Inside the WSDL file you can describe all of the operations your web service has, and the structure of the input and output messages.
Generate a WSDL from code. In some programming languages, you can generate a WSDL document automatically from your code. This is much easier than writing a WSDL document manually.
For example… In Java, you can write code for a web service using the JAX-WS APIs, and add a library like CXF which implements the JAX-WS standard. When you deploy your code, a WSDL will be automatically generated.
Create a web service client. If a web service provides a WSDL, you can import the WSDL into your programming language, and use it to create a client. There are libraries to help you do this in most programming languages. It can save a lot of time and means you don’t have to write a bunch of XML yourself!
Share it. You can publish a WSDL into a repository, so that people can find it and discover your web service.
How is XSD related to WSDL?
WSDL describes the interface of a web service. One of the key things you need to know when connecting to a web service is how you should construct your request message. So, the WSDL defines what the input and output messages for the service should look like.
WSDL didn’t introduce a new language for describing these messages. Instead, it chose to adopt XML Schema Definition as its type system. 4 So WSDL uses the features of XSD to describe input and output messages – by defining the elements, their types, their sequence, and so on.
Most WSDL documents either include an XSD document, or they reference an XSD document located somewhere else.
So WSDL and XSD are closely related. You will often see XSD being used to describe the input and output messages of a web service.
Sometimes, you might also see a chain of documents, where a WSDL references an external XSD document, which itself references another XSD document.
You might see this when dealing with very complex web services, or many web services which share similar messages. So the messages might be defined in one place, and then referenced from many places.
XML Schema Definition, or XSD, is almost the de facto way to write rules for your XML documents. An XSD document describes which elements should be present in an XML document. It can be used to validate XML.
WSDL, or Web Services Definition Language, is a way to describe web services. It’s also written in XML. Many WSDL documents use XML Schema to describe the types of messages that a web service either consumes or produces.
There are so many ways to create a web service. But now you’re familiar with XSD and WSDL, you’ll be on your way to creating and consuming XML-based web services!