XML technologies / XSLT / XSLT Cookbook / Recipes for vertical XSLT applications / SOAP documentation from WSDL
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You use SOAP and WSDL for a commercial service-oriented site and want developers to have complete, relevant information about available services.
Solution
Build a WSDL-based service-documentation server: a service describing services, or metaservice. This example uses a Perl CGI program invoking XSLT on one WSDL file describing every company service. It calls the XSLT processor using a system call. This crude approach suits prototypes, not production. A better solution uses Perl modules XML::LibXML and XML::LibXSLT. A still better architecture uses a sophisticated server-side XSLT solution such as Cocoon. We keep it simple to focus on XSLT and WSDL rather than CGI architecture.
A CGI program generates the home page, showing services and ports. These are explained in the discussion below. This Perl CGI frontend invokes Saxon:
#!/usr/bin/perl
print "Content-type: text/html\n\n" ;
system "saxon StockServices.wsdl wsdlServiceList.xslt" ;The transformation creates a form listing services, ports, bindings and port types:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" exclude-result-prefixes="wsdl soap http mime">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>Verfügbare Dienste von ACME Webservices GmbH</title>
<xsl:comment>WSDL-Dokumentation: Erzeugt mit wsdlServiceList.xslt</xsl:comment>
</head>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="wsdl:definitions">
<h1>Verfügbare Dienste von ACME Webservices GmbH</h1>
<br/>
<form name="QueryServiceForm" method="post" action="QueryService.pl">
<table>
<tbody>
<tr>
<td>Dienste</td>
<td>
<select name="service">
<option value="ALL">ALLE</option>
<xsl:apply-templates select="wsdl:service"/>
</select>
</td>
</tr>
<tr>
<td>Ports</td>
<td>
<select name="port">
<option value="ALL">ALLE</option>
<xsl:apply-templates select="wsdl:service/wsdl:port"/>
</select>
</td>
</tr>
<tr>
<td>Bindings</td>
<td>
<select name="binding">
<option value="ALL">ALLE</option>
<xsl:apply-templates select="wsdl:binding"/>
</select>
</td>
</tr>
<tr>
<td>Port-Typen</td>
<td>
<select name="portType">
<option value="ALL">ALLE</option>
<xsl:apply-templates select="wsdl:portType"/>
</select>
</td>
</tr>
</tbody>
</table>
<br/>
<button type="submit" name="submit">Dienste abfragen</button>
</form>
</xsl:template>
<xsl:template match="wsdl:service | wsdl:port | wsdl:binding | wsdl:portType">
<option value="{@name}"><xsl:value-of select="@name"/></option>
</xsl:template>
</xsl:stylesheet>The user selects a service, port, binding and port-type combination for details. On submission, another small Perl CGI extracts input and invokes a second transformation:
#!/usr/bin/perl
use warnings;
use strict;
use CGI qw(:standard);
my $query = new CGI ;
my $service = $query->param('service');
my $port = $query->param('port');
my $binding = $query->param('binding');
my $portType = $query->param('portType');
print $query->header('text/html');
system "saxon StockServices.wsdl QueryService.xslt service=$service port=$port binding=$binding portType=$portType"This selects matching services and displays details. Its first part normalizes parameters because a WSDL attribute reference may contain a namespace-prefix while the referenced element’s @name attribute does not. The same applies when creating keys:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xslt [
<!ENTITY TNSPREFIX "'acme:'">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2000/10/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/">
<!-- Abfrageparameter -->
<xsl:param name="service" select="'ALL'"/>
<xsl:param name="port" select="'ALL'"/>
<xsl:param name="binding" select="'ALL'"/>
<xsl:param name="portType" select="'ALL'"/>
<!-- Eine Technik (bzw. ein Hack), um Variablen zu leeren, wenn der entsprechende Parameter bzw. die Verkettung von &TNSPREFIX mit dem Parameter gleich ALL ist. Beispiel: number($service = 'ALL') * 99999 + 1 ergibt 1, wenn $service ungleich 'ALL' ist, aber 100000, wenn doch, und geht dann über die Länge des Strings hinaus, wodurch substring einen leeren Wert ergibt. All das hier dient der Vereinfachung von Kreuzverweisen. -->
<xsl:variable name="serviceRef" select="substring(concat(&TNSPREFIX;,$service),number($service = 'ALL') * 99999 + 1)"/>
<xsl:variable name="portRef" select="substring(concat(&TNSPREFIX;,$port),number($port = 'ALL') * 99999 + 1)"/>
<xsl:variable name="bindingRef" select="substring(concat(&TNSPREFIX;,$binding),number($binding = 'ALL') * 99999 + 1)"/>
<xsl:variable name="portTypeRef" select="substring(concat(&TNSPREFIX;,$portType),number($portType = 'ALL') * 99999 + 1)"/>
<!-- Diese Schlüssel vereinfachen und beschleunigen die Abfragen -->
<xsl:key name="bindings_key" match="wsdl:binding" use="concat(&TNSPREFIX;,@name)"/>
<xsl:key name="portType_key" match="wsdl:portType" use="concat(&TNSPREFIX;,@name)"/>
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
<title>ACME Web Services, Inc. Query Result</title>
<xsl:comment>WSDL Documentation: Generated by wsdlServiceList.xslt</xsl:comment>
</head>
<body>
<xsl:apply-templates select="wsdl:definitions"/>
</body>
</html>
</xsl:template>Store results in a variable and test its normalized value to check for matches. An empty value means no matches and displays a message. The query is tricky because the service’s portType requires two hops: from service to binding, then to its port type.
<xsl:template match="wsdl:definitions">
<xsl:variable name="result">
<!-- Abfrage von Diensten, die zu den Abfrageparametern passen. Die portType-Übereinstimmung ist der einzig komplizierte Teil. Wir müssen vom Dienst-Port zum Binding und dann zum Binding-Typ traversieren, um eine Übereinstimmung zu erzielen. Deswegen die verschachtelten Aufrufe von key( ).-->
<xsl:apply-templates select="wsdl:service[(not($serviceRef) or @name = $service) and (not($portRef) or wsdl:port/@name = $port) and (not($bindingRef) or wsdl:port/@binding = $bindingRef) and (not($portTypeRef) or key('portType_key',key('bindings_key',wsdl:port/@binding)/@type)/@name = $portType)]"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="normalize-space($result)">
<xsl:copy-of select="$result"/>
</xsl:when>
<xsl:otherwise>
<p><b>No Matching Services Found</b></p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>Most remaining instructions render WSDL as HTML table entries. A service may have several ports, so ensure the ports match the query parameters:
<xsl:template match="wsdl:service" mode="display">
<h1><xsl:value-of select="@name"/></h1>
<p><xsl:value-of select="wsdl:documentation"/></p>
<table border="1" cellpadding="5" cellspacing="0" width="600">
<tbody>
<xsl:apply-templates select="wsdl:port[(not($portRef) or @name = $port) and (not($bindingRef) or @binding = $bindingRef)]" mode="display"/>
</tbody>
</table>
</xsl:template>Join using these keys to link ports to bindings and bindings to port types:
<xsl:template match="wsdl:port" mode="display">
<tr>
<td style="font-weight:bold" colspan="2" align="center">Port</td>
</tr>
<tr>
<td colspan="2"><h3><xsl:value-of select="@name"/></h3></td>
</tr>
<tr>
<td style="font-weight:bold" width="50">Binding</td>
<td><xsl:value-of select="substring-after(@binding,':')"/></td>
</tr>
<tr>
<td style="font-weight:bold" width="50">Address</td>
<td><xsl:value-of select="soap:address/@location"/></td>
</tr>
<tr>
<th colspan="2" align="center">Operations</th>
</tr>
<xsl:apply-templates select="key('bindings_key',@binding)" mode="display"/>
</xsl:template>
<xsl:template match="wsdl:binding" mode="display">
<xsl:apply-templates select="key('portType_key',@type)" mode="display">
<xsl:with-param name="operation" select="wsdl:operation/@name"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="wsdl:portType" mode="display">
<xsl:param name="operation"/>
<xsl:for-each select="wsdl:operation[@name = $operation]">
<tr>
<td colspan="2"><h3><xsl:value-of select="@name"/></h3></td>
</tr>
<xsl:if test="wsdl:input">
<tr>
<td style="font-weight:bold" width="50">Input</td>
<td><xsl:value-of select="substring-after(wsdl:input/@message,':')"/></td>
</tr>
<xsl:variable name="msgName" select="substring-after(wsdl:input/@message,':')"/>
<xsl:apply-templates select="/*/wsdl:message[@name = $msgName]" mode="display"/>
</xsl:if>
<xsl:if test="wsdl:output">
<tr>
<td style="font-weight:bold" width="50">Output</td>
<td><xsl:value-of select="substring-after(wsdl:output/@message,':')"/></td>
</tr>
<xsl:variable name="msgName" select="substring-after(wsdl:output/@message,':')"/>
<xsl:apply-templates select="/*/wsdl:message[@name = $msgName]" mode="display"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="wsdl:message" mode="display">
<xsl:variable name="dataType" select="substring-after(wsdl:part[@name='body']/@element,':')"/>
<tr>
<td colspan="2">
<xsl:apply-templates select="/*/wsdl:types/*/*[@name=$dataType]" mode="display">
<xsl:with-param name="initial-newline" select="false( )"/>
</xsl:apply-templates>
</td>
</tr>
</xsl:template>This code displays message XSD schemas as XML within HTML. The schema is probably the most valuable information for documenting the data types associated with service input and output messages:
<xsl:template match="*" mode="display">
<xsl:param name="initial-newline" select="true( )"/>
<xsl:if test="$initial-newline">
<xsl:call-template name="newline"/>
</xsl:if>
<!-- öffnendes Tag -->
<a><</a>
<a><xsl:value-of select="name(.)" /> </a>
<!-- Ausgabeattribute -->
<xsl:for-each select="@*">
<a><xsl:text> </xsl:text><xsl:value-of select="name(.)" /> </a>
<a>="</a>
<xsl:value-of select="." />
<a>"</a>
</xsl:for-each>
<xsl:choose>
<xsl:when test="child::node( )">
<!-- schließe Start-Tag -->
<a>></a>
<xsl:apply-templates mode="display"/>
<xsl:call-template name="newline"/>
<!-- schließendes Tag -->
<a><</a>
<a>/<xsl:value-of select="name(.)" /></a>
</xsl:when>
<xsl:otherwise>
<a>/</a>
</xsl:otherwise>
</xsl:choose>
<a>></a>
</xsl:template>
<!-- Füge Zeilenende hinzu und rücke dann je nach Tiefe im Schema ein -->
<xsl:template name="newline">
<br/>
<xsl:for-each select="ancestor::xsd:*[not(self::xsd:schema)]">
<xsl:text>    </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>Discussion
The section Transforming XML to HTML mentioned a repository holding message metadata for client-server exchanges. WSDL provides such a syntax for web services. The same WSDL can generate code and documentation; this example focuses on documentation.
WSDL describes service/port relationships. A port is one endpoint combining a binding and network address. A binding specifies a concrete protocol and data format for a port type. A port type defines an abstract operation set supported by endpoints. An operation is a service action defined as a message with a payload, usually described by XSD. This WSDL excerpt describes services offered by Acme Corporation:
<definitions name="StockServices" targetNamespace="http://acme.com/services.wsdl" xmlns:acme="http://acme.com/services.wsdl" xmlns:xsd1="http://acme.com/stockquote.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/">WSDL uses XSD as the default way to specify exchanged message types:
<types>
<schema targetNamespace="http://acme.com/services.xsd" xmlns="http://www.w3.org/2000/10/XMLSchema">
<element name="Ticker">
<complexType>
<all>
<element name="tickerSymbol" type="string"/>
</all>
</complexType>
</element>
<element name="TradePrice">
<complexType>
<all>
<element name="price" type="float"/>
</all>
</complexType>
</element>
<element name="CompanyName">
<complexType>
<all>
<element name="company" type="string"/>
</all>
</complexType>
</element>
<element name="ServicesInfo">
<complexType>
<sequence>
<element name="Services">
<complexType>
<sequence>
<element name="Service" type="string" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<element name="Ports">
<complexType>
<sequence>
<element name="Port" type="string" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<element name="Bindings">
<complexType>
<sequence>
<element name="Binding" type="string" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<element name="PortTypes">
<complexType>
<sequence>
<element name="Port" type="string" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
<element name="ServicesQuery">
<complexType>
<all>
<element name="Service" type="string"/>
<element name="Port" type="string"/>
<element name="Binding" type="string"/>
<element name="PortType" type="string"/>
</all>
</complexType>
</element>
<element name="ServicesResponse">
<complexType>
<sequence>
<any/>
</sequence>
</complexType>
</element>
</schema>
</types>Messages associate names and types, describing what the architecture communicates:
<message name="GetLastTradePriceInput">
<part name="body" element="xsd1:Ticker"/>
</message>
<message name="GetLastTradePriceOutput">
<part name="body" element="xsd1:TradePrice"/>
</message>
<message name="GetCompanyInput">
<part name="body" element="xsd1:Ticker"/>
</message>
<message name="GetCompanyOutput">
<part name="body" element="xsd1:CompanyName"/>
</message>
<message name="GetTickerInput">
<part name="body" element="xsd1:CompanyName"/>
</message>
<message name="GetTickerOutput">
<part name="body" element="xsd1:Ticker"/>
</message>
<message name="GetServicesInput"/>
<message name="GetServicesOutput">
<part name="body" element="xsd1:ServicesInfo"/>
</message>
<message name="QueryServicesInput">
<part name="body" element="xsd1:ServicesQuery"/>
</message>
<message name="QueryServicesOutput">
<part name="body" element="xsd1:ServicesRespose"/>
</message>Port types specify an endpoint’s available operations. Here two types cover stock quotes and service discovery. This example implements discovery, though normally another program rather than a browser would use it:
<portType name="StockPortType">
<operation name="GetLastTradePrice">
<input message="acme:GetLastTradePriceInput"/>
<output message="acme:GetLastTradePriceOutput"/>
</operation>
<operation name="GetTickerFromCompany">
<input message="acme:GetTickerInput"/>
<output message="acme:GetCompanyOutput"/>
</operation>
<operation name="GetCompanyFromTicker">
<input message="acme:GetCompanyInput"/>
<output message="acme:GetTickerOutput"/>
</operation>
</portType>
<portType name="ServicePortType">
<operation name="GetServices">
<input message="acme:GetServicesInput"/>
<output message="acme:GetServicesOutput"/>
</operation>
<operation name="QueryServices">
<input message="acme:QueryServicesInput"/>
<output message="acme:QueryServicesOutput"/>
</operation>
</portType>Bindings connect operations to protocols, here SOAP:
<binding name="StockQuoteSoapBinding" type="acme:StockPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetLastTradePrice">
<soap:operation soapAction="http://acme.com/GetLastTradePrice"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<binding name="StockTickerSoapBinding" type="acme:StockPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetTickerFromCompany">
<soap:operation soapAction="http://acme.com/GetTickerSymbol"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<binding name="StockNameSoapBinding" type="acme:StockPortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetCompanyFromTicker">
<soap:operation soapAction="http://acme.com/GetCompanyName"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<binding name="ServiceListSoapBinding" type="acme:ServicePortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetServices">
<soap:operation soapAction="http://acme.com/GetServices"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>Two services expose stock quotes and discovery. Note how each organizes ports to state its functionality:
<service name="StockInfoService">
<documentation>Provides information about stocks.</documentation>
<port name="StockQuotePort" binding="acme:StockQuoteSoapBinding">
<soap:address location="http://acme.com/stockquote"/>
</port>
<port name="StockTickerToNamePort" binding="acme:StockTickerSoapBinding">
<soap:address location="http://acme.com/tickertoname"/>
</port>
<port name="StockNameToTickerPort" binding="acme:StockNameSoapBinding">
<soap:address location="http://acme.com/nametoticker"/>
</port>
</service>
<service name="ServiceInfoService">
<documentation>Provides information about avaialable services.</documentation>
<port name="ServiceListPort" binding="acme:ServiceListSoapBinding">
<soap:address location="http://acme.com/stockquote"/>
</port>
<port name="ServiceQueryPort" binding="acme:ServiceQuerySoapBinding">
<soap:address location="http://acme.com/tickertoname"/>
</port>
</service>
</definitions>Opening the CGI server displays a service-query screen where you set parameters, as shown below.
Figure: WSDL query screen.
Submitting the query produces the following result.
Figure: WSDL result screen.
See also
The WSDL specification is at Web Services Description Language (WSDL) 1.1. WSDL is not yet an official W3C Recommendation but is gaining industry support. (data2type editorial note: Web Services Description Language (WSDL) Version 2.0.) An official Web Services Description Working Group is working toward a future W3C Recommendation.
Recipe Using XSLT in Perl gives more ways to embed XSLT in Perl without forking a separate process.
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de