Validating XML - Avoiding the pain

33
Validating XML - Avoiding the pain Arne Blankerts <[email protected]>, TobiasSchlitt <[email protected]> IPC 2009 2009-11-17 Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 1 / 18

description

Slides from the DTD / XSD tutorial by Arne Blankerts and me. We did a lot of live hacking in this session, so the slides are quite thin.

Transcript of Validating XML - Avoiding the pain

Page 1: Validating XML - Avoiding the pain

Validating XML - Avoiding the pain

Arne Blankerts <[email protected]>, TobiasSchlitt <[email protected]>

IPC 2009

2009-11-17

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 1 / 18

Page 2: Validating XML - Avoiding the pain

Outline

1 Welcome

2 Introduction

3 Schema formats

4 The end

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 2 / 18

Page 3: Validating XML - Avoiding the pain

Arne Blankerts

Arne Blankerts <[email protected]>

PHP since 1999 (10 years of PHP!)

Co-Founder of thePHP.cc

ballyhoo. werbeagentur.

Open source addicted

Inventor and lead developer of fCMS sitesystemContributor and translator for the PHP manual

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 3 / 18

Page 4: Validating XML - Avoiding the pain

Tobias Schlitt

Tobias Schlitt <[email protected]>

PHP since 2001

Freelancing consultant

Qualified IT Specialist

Studying CS at TU Dortmund(finishing 2010)

OSS addicted

PHPeZ ComponentsPHPUnit

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 4 / 18

Page 5: Validating XML - Avoiding the pain

Outline

1 Welcome

2 IntroductionWhy the hell validate?Validation basics

3 Schema formats

4 The end

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 5 / 18

Page 6: Validating XML - Avoiding the pain

XML is everywhere

On your HD

On the web

In your app

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 6 / 18

Page 7: Validating XML - Avoiding the pain

XML is everywhere

On your HD

On the web

In your app

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 6 / 18

Page 8: Validating XML - Avoiding the pain

XML is everywhere

On your HD

On the web

In your app

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 6 / 18

Page 9: Validating XML - Avoiding the pain

XML comes from everywhere

From the web

From your app

From your users

From 3rd parties

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 7 / 18

Page 10: Validating XML - Avoiding the pain

XML comes from everywhere

From the web

From your app

From your users

From 3rd parties

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 7 / 18

Page 11: Validating XML - Avoiding the pain

XML comes from everywhere

From the web

From your app

From your users

From 3rd parties

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 7 / 18

Page 12: Validating XML - Avoiding the pain

XML comes from everywhere

From the web

From your app

From your users

From 3rd parties

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 7 / 18

Page 13: Validating XML - Avoiding the pain

So why validate?

Broken incoming data breaks your app

Broken outgoing data breaks other apps

You test code, why not test XML?

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 8 / 18

Page 14: Validating XML - Avoiding the pain

So why validate?

Broken incoming data breaks your app

Broken outgoing data breaks other apps

You test code, why not test XML?

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 8 / 18

Page 15: Validating XML - Avoiding the pain

So why validate?

Broken incoming data breaks your app

Broken outgoing data breaks other apps

You test code, why not test XML?

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 8 / 18

Page 16: Validating XML - Avoiding the pain

So why validate?

Broken incoming data breaks your app

Broken outgoing data breaks other apps

You test code, why not test XML?

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 8 / 18

Page 17: Validating XML - Avoiding the pain

What is a XML schema?

Defines the structure of data

Possibly defines data types

Used to validate correctness

Helpful as documentation

Similar to database schemas!

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 9 / 18

Page 18: Validating XML - Avoiding the pain

When to use it?

On XML generation in your app

Before your app consumes XML

In your tests

Give to your XML consumers

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 10 / 18

Page 19: Validating XML - Avoiding the pain

Outline

1 Welcome

2 Introduction

3 Schema formatsOverviewDTDXML Schema

4 The end

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 11 / 18

Page 20: Validating XML - Avoiding the pain

XML schema formats

DTD

XSD

RELAX NG

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 12 / 18

Page 21: Validating XML - Avoiding the pain

XML schema formats

DTD Document Type DefinitionPart of the XML specificationAllows definition of entitiesNo advanced type supportDoes not support different types for same element name

XSD

RELAX NG

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 12 / 18

Page 22: Validating XML - Avoiding the pain

XML schema formats

DTD

XSD XML SchemaW3C recommendation since May 2001Advanced type supportSupport for keys and key referencesNo support for entities

RELAX NG

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 12 / 18

Page 23: Validating XML - Avoiding the pain

XML schema formats

DTD

XSD

RELAX NG Regular Language for XML Next GenerationDefined by OASIS, ISO/IEC 19757Generally more powerful than XSDNo support for entitiesNot (yet?) as popular as XSD

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 12 / 18

Page 24: Validating XML - Avoiding the pain

DTD

Let’s dig into the code. . .

Schema definitions

Entities

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 13 / 18

Page 25: Validating XML - Avoiding the pain

DTD

Let’s dig into the code. . .

Schema definitions

Advanced types

Key and key-ref

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 14 / 18

Page 26: Validating XML - Avoiding the pain

XSD goodies

Qualified / unqualified

<any> namespace handling

Validation of external elements/attributes

Abstract types and inheritance

Nillable

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 15 / 18

Page 27: Validating XML - Avoiding the pain

XSD goodies

Qualified / unqualified

<any> namespace handling

##any##other##local

Validation of external elements/attributes

Abstract types and inheritance

Nillable

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 15 / 18

Page 28: Validating XML - Avoiding the pain

XSD goodies

Qualified / unqualified

<any> namespace handling

Validation of external elements/attributes

strictskiplax

Abstract types and inheritance

Nillable

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 15 / 18

Page 29: Validating XML - Avoiding the pain

XSD goodies

Qualified / unqualified

<any> namespace handling

Validation of external elements/attributes

Abstract types and inheritance

Nillable

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 15 / 18

Page 30: Validating XML - Avoiding the pain

XSD goodies

Qualified / unqualified

<any> namespace handling

Validation of external elements/attributes

Abstract types and inheritance

Nillable

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 15 / 18

Page 31: Validating XML - Avoiding the pain

Outline

1 Welcome

2 Introduction

3 Schema formats

4 The end

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 16 / 18

Page 32: Validating XML - Avoiding the pain

Q/A

Are there any questions left?

Please give us some feedback!

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 17 / 18

Page 33: Validating XML - Avoiding the pain

The end

We hope you enjoyed the session!

Slides and material:

Delivered by Software & Supporthttp://schlitt.info/opensourceOn Slideshare (http://slideshare.net)

Contact us:

Arne Blankerts <[email protected]>Tobias Schlitt <[email protected]>

Give us feedback on http://joind.in/1043

Arne Blankerts, Tobias Schlitt (IPC 2009) Validating XML - Avoiding the pain 2009-11-17 18 / 18