1 intermezzo xml good understanding of xml is vital to work with mom many people think they know xml...

34
1 Intermezzo XML • Good understanding of XML is vital to work with MOM • Many people think they know XML quite well, whilst they know not enough • It can be rather quite complex, don’t underestimate the complicity Teacher Marcel Grauwen

Upload: thomasina-griffith

Post on 17-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

1

Intermezzo XML

• Good understanding of XML is vital to work with MOM

• Many people think they know XML quite well, whilst they know not enough

• It can be rather quite complex, don’t underestimate the complicity

Teacher Marcel Grauwen

Page 2: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

2

XML Background

• Both XML and HTML origins from SGML

<?xml version=“1.0”?> -TAGS

<greeting style=“informal”> -Elements

<from>marcel grauwen</from> -Attributes

<to>you all</to>

<message>hello</message>

<signature/>

</greeting>

Looks simple right?

Page 3: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

3

XML

• XML is very important for MOM

• XML can be precisely described by (META definition)– Data Type Definition (DTD)– XML Schema (XSD)

• What are the two main differences between those two above?

Page 4: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

4

XML XSD

<?xml version = "1.0" encoding = "UTF-8"?><xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema" elementFormDefault = "qualified">

<xsd:element name = "greeting"><xsd:complexType>

<xsd:sequence minOccurs = "0"><xsd:element ref = "from"/><xsd:element ref = "message"/><xsd:element ref = "to"/><xsd:element ref = "signature"/>

</xsd:sequence><xsd:attribute name = "style" type = "xsd:string"/>

</xsd:complexType></xsd:element><xsd:element name = "from" type = "xsd:string"/><xsd:element name = "to" type = "xsd:string"/><xsd:element name = "message" type = "xsd:string"/><xsd:element name = "signature" type = "xsd:string"/>

</xsd:schema>

Looks simple right?

Page 5: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

5

XML XSD

• XML messages could be validated

• XML validation requires the XSD and sets requirements on format of the XML message (needs to be well formatted)

• XSDs used to create XSLTs

• Both XSD and XSLT rely on XPATH functions

Page 6: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

6

XML Namespacing<?xml version="1.0" encoding = "UTF-8"?><ThisGreetings xmlns:greet='urn:DEMO:www.glomidco.com/greetings‘

xmlns:MyName='urn:DEMO:www.glomidco.com/names‘ ><greet:greeting style="informal">

<greet:from><MyName:firstname>marcel</MyName:firstname><MyName:surname>grauwen</

MyName:surname></greet:from><greet:to>you all</greet:to><greet:message>hello</greet:message><greet:signature/>

</greet:greeting></ThisGreetings>

<?xml version="1.0" encoding = "UTF-8"?><ThisGreetings xmlns:NS0='urn:DEMO:www.glomidco.com/greetings'

xmlns:NS1='urn:DEMO:www.glomidco.com/names‘ ><NS0:greeting style="informal">

<NS0:from><NS1:firstname>marcel</NS1:firstname><NS1:surname>grauwen</NS1:surname>

</NS0:from><NS0:to>you all</NS0:to><NS0:message>hello</NS0:message><NS0:signature/>

</NS0:greeting></ThisGreetings>

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

<urn:DEMO:www.glomidco.com/greetings:greeting style="informal"><urn:DEMO:www.glomidco.com/greetings:from>

<urn:DEMO:www.glomidco.com/names:firstname>marcel</urn:DEMO:www.glomidco.com/names:firstname><urn:DEMO:www.glomidco.com/names:surname>grauwen</urn:DEMO:www.glomidco.com/names:surname>

</urn:DEMO:www.glomidco.com/greetings:from><urn:DEMO:www.glomidco.com/greetings:to>you all</urn:DEMO:www.glomidco.com/greetings:to><urn:DEMO:www.glomidco.com/greetings:message>hello</urn:DEMO:www.glomidco.com/greetings:message><urn:DEMO:www.glomidco.com/greetings:signature/>

</urn:DEMO:www.glomidco.com/greetings:greeting></ThisGreetings>

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

<ThisGreetings>

<greeting style="informal">

<from>

<firstname>marcel</firstname>

<surname>grauwen</surname>

</from>

<to>you all</to>

<message>hello</message>

<signature/>

</greeting>

</ThisGreetings>

Validation Possible?

Page 7: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

7

XML Namespacing & Validation

• Namespacing & validation capabilities often tightly linked• Namespacing not very well understood by most people• Validation is applying a XSD to a input XML and see if it

complies to it.• An XML namespace is simply a group of names, usually

with a related purpose or context, where the group has a globally unique name (“namespace name”). Often ensured by using a DNS name as the first part of the namespace name. (they do not physically exist!)

• URI is called the namespace name• Alias for the Uri are known as namespace prefix, which

can be used as prefixes in our XML.

Page 8: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

8

Practicum (I/ VIII)

Altova XML Spy

Teacher demo’s some elements

• Install

• Practicum

Page 9: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

9

Practicum (II/VIII )

Install Altova XMLspy

see http://www.Altova.com/XMLSpy

see http://software.altova.com/download/2008/MissionKitXMLDevEnt2008.exe

accept use locally

complete

Page 10: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

10

Practicum (III/VIII )

Start ->Programs -> Altova Missionkit -> XMLspy

Request evaluation key

Fill in detail and valid email address

Check mailFill in license received Save

You need other licenses too!!

Page 11: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

11

Practicum (IV/ )

• See hand-out for exercises• Goal is to be able to make:

– XSD’s, – XML sample messages, – nested XSD’s, – XSLT’s (mapping)– Optional: Running a transformation from command

line – Get experienced with a tool such as XMLspy– Optional: learn to create a WSDL

Page 12: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

12

Practicum (V/III )

Make exercises:

•Exercises 1-A : All messages

•Exercises 1-B: 1-B-1

Page 13: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

13

Common errors:

XSD:• No root element set• Constraints (length, mandotory|optional etc)• dateTime (UTC)• …..

XML:• Namespacing & validation• <emptytag></emptytag> ……<emptytag/>• …..

Page 14: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

14

Nested XSD’s: Include versus import<?xml version = "1.0" encoding = "UTF-8"?><!--Generated by Turbo XML 2.4.2.7. Conforms to w3c http://www.w3.org/2001/XMLSchema--><xsd:schema xmlns:xsd = "http://www.w3.org/2001/XMLSchema"

elementFormDefault = "qualified">

<xsd:include schemaLocation = "AJaXall.xsd"/><xsd:element name = "root">

<xsd:complexType><xsd:sequence>

<xsd:element ref = "SessionID"/><xsd:element ref = "ProductCode"/>

</xsd:sequence></xsd:complexType>

</xsd:element></xsd:schema>

<?xml version = "1.0" encoding = "UTF-8"?><!--Generated by Turbo XML 2.4.0.100. Conforms to w3c http://www.w3.org/2001/XMLSchema--><xsd:schema xmlns = "http://www.glomidco.com/webshop/getInfo"

targetNamespace = "http://www.glomidco.com/webshop/getInfo" xmlns:attr = "http://www.glomidco.com/webshop/AJaXall"

xmlns:xsd = "http://www.w3.org/2001/XMLSchema" elementFormDefault = "qualified">

<xsd:import namespace = "http://www.glomidco.com/webshop/AJaXall" schemaLocation = "AJaXall.xsd"/>

<xsd:element name = "root"><xsd:complexType>

<xsd:sequence><xsd:element ref = "attr:SessionID"/><xsd:element ref = "attr:ProductCode"/>

</xsd:sequence></xsd:complexType>

</xsd:element></xsd:schema>

Page 15: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

15

XML XSL(T)

• XSLT stylesheet consist of a series of one or more templates, together with instructions based on XPath expressions that an XSLT processor how to match the templates against nodes in an XML input document.

• For each template, the processor looks through the input document until it find a node matching the pattern specified in the template, and applies the template to the date it finds.

• The result of applying XSLT is a document called output document• Complex because it relies heavily on namespaces and XPath• Describes the transformation from XML to another XML (or HTML

page) which is the basis of most mapping tools in EAI.• Transforming a XML also means styling a page, hence CSS

(cascading style sheets) are XSLTs too

Page 16: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

16

XML Xpath

• The XML patch language (XPath is cpmplex and huge)• Location path (relative and absolute)• Context node• Parent axis, the ancestor axis, child axis and descendant

axis, … (total 13 axes)• Location step: axis | node-test | predicate• Node types: root node | element node | attribute node |

processing instruction node | comment node | text node | namespace node

• Namespace URI - often people think its just a URL which is a common mistake! – It is a reference

• Many XPath (basic) functions (even more when Xpath 2.0 will be excepted, e.g. 2.0 includes regexp)

Page 17: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

17

XML XPath derived technlogies

• XPointer (extends Xpath including points and ranges)

• XLink (linking functionality e.g. coordinate based linking)

• XQuery – XQuery is becoming quite important as alternative for XPath within MOM. Hence Xquery delivers an outout XML from a input XML, which is an alternative way of mapping

Page 18: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

18

DOM <-> SAX

• Document Object Model (DOM)

• DOM expresses a document as a tree which is held in memory

• Less efficient and relatively slow

• Usages a lot of memory

• Validation of unknown elements in an XML (extension to the XSD) will fail

• SAX (simple API for XML)• Fast• Less memory consuming• No in-memory model• Processing in response to event• More complicated to use• Differences between V1 & v2• Validation of unknown elements

in an XML (extensions to the XSD) discards unknown XML elements

Page 19: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

19

Some popular XML tools

• XML SAX parsers– Xerces– Crimson

• XSLT processor– Xalan (Apache)– MSXML3 (microsoft)– Saxon

• XML tools– Altova XML Spy– Sonic Stylus Studio– Tibco Turbo XML

Page 20: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

20

XML, XSD, XSLTXML Input

XML Output

XSD Input

XSD Output

XSLTDesign

XML Input

XSLT

XSLT processor

XML Output

Runtime

Page 21: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

21

<?xml version="1.0" encoding="UTF-8"?><xsl:template xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <greeting> <from> <xsl:value-of select="concat($Parse-XML/greeting/from/firstname, $Parse-XML/greeting/from/surname)"/> </from> <message> <xsl:value-of select="$Parse-XML/greeting/message"/> </message> <to> <xsl:value-of select="$Parse-XML/greeting/to"/> </to> <signature> <xsl:value-of select="$Parse-XML/greeting/signature"/> </signature> </greeting></xsl:template>

XSLT using XPathConcat(firstname,surname)

Looks simple right?

Page 22: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

22

Practicum (VI/VIII )Make exercises:

1-B-4

Mapforce

Map XML data Altova Mapforce installation now required!

Page 23: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

23

Practicum (VII/VIII )

Mapping

• In Altova Mapforce select an input XSD

• In Altova Mapforce select an output XSD

• Start mapping

• Note that an XSLT will be generated (finally)

Page 24: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

24

JAXB

Sun’s JAXB https://jaxb.dev.java.net/ is a very powerful tool to use when programming in java and want to use XML.

• It is capable of generating java code from an XSD or DTD

• It can generate XML from Java objects• It uses the concept of marshalling (generate

XML) and un-marshalling (render XML) within java

Page 25: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

25

Apache XML

• Xerces: a XML-parser in Java, C++ and Perl • Xalan: a XSL-stylesheet-processor in Java and C++

(uses XML-query language XPath) • SOAP: Based on IBM's SOAP4J-implementation.• XML-RPC: Apache XML-RPC is a Java-implementation

of XML-RPC, a protocol which uses XML over HTTP to implement remote procedure calls.

• Axis: Apache Axis is the current implementation of SOAP for Java en C++.

• WSIF: Web Services Invocation Framework: a simple Java API to invoke webservices

Page 26: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

26

SOAP

Soap is the fundamental messaging framework for Web Services.

With soap you can access web-services through loosely coupled infrastructure that provides significant resilience, scalability, and flexability in deployment using different implementation technologies and network transports.

SOAP provides four main capabilities:1. A standardized message structure based on the XML

Info-set2. A processing Model that describes how a service should

process a message3. A mechanism to bind SOAP messages to different

network transport protocols4. A way to attach non-XML encoded information to SOAP

messages

SOAP envelop

SOAP Header

Header Block 1

Header Block M

…………………

SOAP Body

Body sub-element 1

Body sub-element N

…………………

Page 27: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

27

SOAP exampleTaken from http://www.w3schools.com/soap/default.asp

The SOAP request:POST /InStock HTTP/1.1 Host: www.example.org Content-Type: application/soap+xml; charset=utf-8

Content-Length: nnn <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock">    <m:GetStockPrice> <m:StockName>IBM</m:StockName> </m:GetStockPrice>

</soap:Body></soap:Envelope>

A SOAP response:HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: nnn

<?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope“ soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Body xmlns:m="http://www.example.org/stock"> <m:GetStockPriceResponse> <m:Price>34.5</m:Price> </m:GetStockPriceResponse>

</soap:Body></soap:Envelope>

Page 28: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

28

Web Service Description Language(WSDL)

• WSDL is what everyone uses to tell others what they can do• Is a web vocubalary to describe webservices.• Is highly extendable and adaptable to enable description of

services using different type systems (not only XML schema)

• Two parts: reusable abstract part and a concrete part• The abstract describes the operational behavior of

webservices by recounting the messages that go in and out the services.

• The concrete part describes how and where to access a service implementation.

• It does describe semantics, it does give syntactic or structural terms what goes in and what goes out.

Page 29: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

29

WSDL

Used in two distinct scenario’s• Describing a service for its clients

( a published service for its clients to enable a client to use that service effectively)

• Describing a standard service for service implementors (Document describes a standard service)

Page 30: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

30

WSDL syntax

• <types> element is to contain data structure declarations. When using XML schema you can embed a schema directly by including an <xsd:schema>element inside <types>

• <messages> construct allows you to describe the messages that the webservice is exchanging. Messages can consist of one or more parts, where a part represents a single item that is to be sent or received.

• <portTypes> construct defines a set of related <operation> that a webservice supports. An “operation” is a grouping of related set of messages that are exchanged. WSDL 1.1. has four types of operations:

– One-Way. A message comes to a service and the service produces nothing in response

– Request-Response: A message comes to a service and the service produces a message in response

– Solicit-response: The service sends a message and gets a response back– Notifcation: The service sends a message and receives nothing back

<definitions>

<types>

</types>

<message name=“..”>

</message>

</definitions>

<portType name=“..”>

</portType>

<service name=“..”>

</service>

Page 31: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

31

Sample WDSL (I/II)<definitions xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:s0="http://www.Nanonull.com/TimeService/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://www.Nanonull.com/TimeService/">

<types><s:schema elementFormDefault="qualified" targetNamespace="http://www.Nanonull.com/TimeService/">

<s:element name="getUTCTime"><s:complexType/>

</s:element>……..

</s:schema></types>

<message name="getUTCTimeSoapIn"><part name="parameters" element="s0:getUTCTime"/>

</message>…..<message name="errorHttpGetOut">

<part name="Error" element="s0:errorResponse"/></message>

<portType name="TimeServiceSoap"><operation name="getUTCTime">

<input message="s0:getUTCTimeSoapIn"/><output message="s0:getUTCTimeSoapOut"/>

</operation>….<operation name="getTimeZoneTime">

<input message="s0:getTimeZoneTimeSoapIn"/><output message="s0:getTimeZoneTimeSoapOut"/><fault name="FaultTimeZoneNotFound" message="s0:errorSoapOut"/>

</operation></portType>….<portType name="TimeServiceHttpPost">

…….</portType>

Page 32: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

32

Sample WDSL (II/II)<binding name="TimeServiceSoap" type="s0:TimeServiceSoap">

<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/><operation name="getUTCTime">

<soap:operation soapAction="http://www.Nanonull.com/TimeService/getUTCTime" style="document"/><input>

<soap:body use="literal"/></input><output>

<soap:body use="literal"/></output>

</operation><operation name="getServerTime">

….</operation>….

</binding><binding name="TimeServiceHttpGet" type="s0:TimeServiceHttpGet">…..</binding>……

<service name="TimeService"><documentation>A sample Time service</documentation><port name="TimeServiceSoap" binding="s0:TimeServiceSoap">

<soap:address location="http://www.nanonull.com/TimeService/TimeService.asmx"/></port><port name="TimeServiceHttpGet" binding="s0:TimeServiceHttpGet">

<http:address location="http://www.nanonull.com/TimeService/TimeService.asmx"/></port><port name="TimeServiceHttpPost" binding="s0:TimeServiceHttpPost">

<http:address location="http://www.nanonull.com/TimeService/TimeService.asmx"/></port>

</service>

</definitions>

Page 33: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

33

XML practical thoughts

• In general XML parsing or XML rendering is costly

• Memory usage: thumb rule: memory usage internally is approximately 100 times the size of the XML message. Size does matter!!

Page 34: 1 Intermezzo XML Good understanding of XML is vital to work with MOM Many people think they know XML quite well, whilst they know not enough It can be

34

Practicum (VIII/VIII )

Homework:

• Everything left from 1-B

• 1-c Optional, but very interesting

• Exercise II-A

• Exercise II –B