159
XML and XPath with PHP TobiasSchlitt <[email protected]> IPC SE 2009 2009-05-29 Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 1 / 102

XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt IPC SE 2009 2009-05-29

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

XML and XPath with PHP

TobiasSchlitt <[email protected]>

IPC SE 2009

2009-05-29

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 1 / 102

Page 2: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

About me

Tobias Schlitt <[email protected]>

PHP since 2001

Freelancing consultant

Qualified IT Specialist

Studying CS at TU Dortmund(expect to finish this year)

OSS addicted

PHPeZ ComponentsPHPUnit

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 2 / 102

Page 3: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

And who are you?

What is your name?

Where are you from?

What is your business?

What are your experiences with XML / XPath?

What do you expect from this workshop?

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 3 / 102

Page 4: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Overview

1 XML

2 XML in PHP

3 XPath

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 4 / 102

Page 5: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline

1 XMLOverviewTerminologyThe XML tree

2 XML in PHP

3 XPath

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 5 / 102

Page 6: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - XML

1 XMLOverviewTerminologyThe XML tree

2 XML in PHP

3 XPath

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 6 / 102

Page 7: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

What is XML?

General specification for creating markup languages

Data exchange between computer systems

System independentHuman readableMost used on the webSuccessor of SGML

W3C recommendation

1.0 1998 (last update 2008-11-26)1.1 2004 (last update 2006-08-16)

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 7 / 102

Page 8: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

XML languages by example

XHTML XML variant of the popular HTML

RSS Really Simply SyndicationProvide news / updates / ... of websitesRead by special clientsAggregation on portals / planets

SVG Scalable Vector GraphicsDescribe vector graphics in XMLPotentially interactive / animated(via ECMAScript)

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 8 / 102

Page 9: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Related technologies - Schemas

Schema

A schema defines the structure XML instance documents.

XMLSchema Written in XMLW3C recommendationPopular

RelaxNG 2 syntax variants

XML basedShort plain text based

OASIS / ISO standardPopular

DTD Plain textW3C recommendationDeprecated

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 9 / 102

Page 10: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Related technologies - Querying

Query

A query extracts a sub-set of information from a data source.

XPath W3C recommendationNavigation in XML documentsmore on that later...

XQuery Functional programming languageAllows complex queries

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 10 / 102

Page 11: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - XML

1 XMLOverviewTerminologyThe XML tree

2 XML in PHP

3 XPath

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 11 / 102

Page 12: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

An XML document

<?xml version=”1.0” encoding=”UTF-8”?><bookshelf>

<book id=”1”><title lang=”en”>Beautiful code</title><author>A. Oram</author><author>G. Wilson</author><year>2007</year><price currency=”Euro”>35.95</price>

</book>

<book id=”2”><title lang=”de”>eZ Components - Das Entwicklerhandbuch</title><author>T. Schlitt</author><author>K. Nordmann</author><year>2007</year><price currency=”Euro”>39.95</price>

</book>

</bookshelf>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 12 / 102

Page 13: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Preamble

<?xml version=”1.0” encoding=”UTF-8”?><bookshelf>

<book id=”1”><title lang=”en”>Beautiful code</title><author>A. Oram</author><author>G. Wilson</author><year>2007</year><price currency=”Euro”>35.95</price>

</book>

<book id=”2”><title lang=”de”>eZ Components - Das Entwicklerhandbuch</title><author>T. Schlitt</author><author>K. Nordmann</author><year>2007</year><price currency=”Euro”>39.95</price>

</book>

</bookshelf>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 12 / 102

Page 14: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Element nodes

<?xml version=”1.0” encoding=”UTF-8”?><bookshelf>

<book id=”1”><title lang=”en”>Beautiful code</title><author>A. Oram</author><author>G. Wilson</author><year>2007</year><price currency=”Euro”>35.95</price>

</book>

<book id=”2”><title lang=”de”>eZ Components - Das Entwicklerhandbuch</title><author>T. Schlitt</author><author>K. Nordmann</author><year>2007</year><price currency=”Euro”>39.95</price>

</book>

</bookshelf>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 12 / 102

Page 15: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Attribute nodes

<?xml version=”1.0” encoding=”UTF-8”?><bookshelf>

<book id=”1”><title lang=”en”>Beautiful code</title><author>A. Oram</author><author>G. Wilson</author><year>2007</year><price currency=”Euro”>35.95</price>

</book>

<book id=”2”><title lang=”de”>eZ Components - Das Entwicklerhandbuch</title><author>T. Schlitt</author><author>K. Nordmann</author><year>2007</year><price currency=”Euro”>39.95</price>

</book>

</bookshelf>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 12 / 102

Page 16: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Atomic value nodes

<?xml version=”1.0” encoding=”UTF-8”?><bookshelf>

<book id=”1”><title lang=”en”>Beautiful code</title><author>A. Oram</author><author>G. Wilson</author><year>2007</year><price currency=”Euro”>35.95</price>

</book>

<book id=”2”><title lang=”de”>eZ Components - Das Entwicklerhandbuch</title><author>T. Schlitt</author><author>K. Nordmann</author><year>2007</year><price currency=”Euro”>39.95</price>

</book>

</bookshelf>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 12 / 102

Page 17: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Document element

<?xml version=”1.0” encoding=”UTF-8”?><bookshelf>

<book id=”1”><title lang=”en”>Beautiful code</title><author>A. Oram</author><author>G. Wilson</author><year>2007</year><price currency=”Euro”>35.95</price>

</book>

<book id=”2”><title lang=”de”>eZ Components - Das Entwicklerhandbuch</title><author>T. Schlitt</author><author>K. Nordmann</author><year>2007</year><price currency=”Euro”>39.95</price>

</book>

</bookshelf>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 12 / 102

Page 18: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - XML

1 XMLOverviewTerminologyThe XML tree

2 XML in PHP

3 XPath

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 13 / 102

Page 19: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

The XML tree

<bookshelf>

<book> <book>

id=

”1”

<title>

Beautifulcode

lang=

”en”

<author>

A. Oram

<author>

G. Wilson

<year>

2007

<price>

39.95 currency=

”Euro”

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 14 / 102

Page 20: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Children I

<bookshelf>

<book> <book>

id=

”1”

<title>

Beautifulcode

lang=

”en”

<author>

A. Oram

<author>

G. Wilson

<year>

2007

<price>

39.95 currency=

”Euro”

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 14 / 102

Page 21: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Children II

<bookshelf>

<book> <book>

id=

”1”

<title>

Beautifulcode

lang=

”en”

<author>

A. Oram

<author>

G. Wilson

<year>

2007

<price>

39.95 currency=

”Euro”

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 14 / 102

Page 22: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Parent

<bookshelf>

<book> <book>

id=

”1”

<title>

Beautifulcode

lang=

”en”

<author>

A. Oram

<author>

G. Wilson

<year>

2007

<price>

39.95 currency=

”Euro”

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 14 / 102

Page 23: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Descendants

<bookshelf>

<book> <book>

id=

”1”

<title>

Beautifulcode

lang=

”en”

<author>

A. Oram

<author>

G. Wilson

<year>

2007

<price>

39.95 currency=

”Euro”

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 14 / 102

Page 24: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Ascendants

<bookshelf>

<book> <book>

id=

”1”

<title>

Beautifulcode

lang=

”en”

<author>

A. Oram

<author>

G. Wilson

<year>

2007

<price>

39.95 currency=

”Euro”

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 14 / 102

Page 25: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Siblings

<bookshelf>

<book> <book>

id=

”1”

<title>

Beautifulcode

lang=

”en”

<author>

A. Oram

<author>

G. Wilson

<year>

2007

<price>

39.95 currency=

”Euro”

Attribute siblings

Attributes are not considered siblings to elements.

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 14 / 102

Page 26: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

CDATA

CDATA

Avoid the escaping hell in text content.

Without CDATA

<boo k s h e l f><book i d=”1”>

< t i t l e l ang=”en”>Be a u t i f u l code</ t i t l e>

<h i n t>Some examples make use o f &lt;xml&gt; .

</ h i n t>

</book></ bo o k s h e l f>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 15 / 102

Page 27: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

CDATA

CDATA

Avoid the escaping hell in text content.

With CDATA

<boo k s h e l f><book i d=”1”>

< t i t l e l ang=”en”>Be a u t i f u l code</ t i t l e>

<h i n t><![CDATA[Some examples make use o f <xml> .]]>

</ h i n t>

</book></ bo o k s h e l f>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 15 / 102

Page 28: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

CDATA

CDATA

Avoid the escaping hell in text content.

The CDATA dilemma

<boo k s h e l f><book i d=”1”>

< t i t l e l ang=”en”>Be a u t i f u l code</ t i t l e>

<h i n t><![CDATA[Some examples show the usage o f <![CDATA[ ]]>]]>

</ h i n t>

</book></ bo o k s h e l f>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 15 / 102

Page 29: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

CDATA

CDATA

Avoid the escaping hell in text content.

The CDATA dilemma workaround

<boo k s h e l f><book i d=”1”>

< t i t l e l ang=”en”>Be a u t i f u l code</ t i t l e>

<h i n t><![CDATA[Some examples show the usage o f <![CDATA[ ]]]]><![CDATA[>]]>

</ h i n t>

</book></ bo o k s h e l f>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 15 / 102

Page 30: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

CDATA

CDATA

Avoid the escaping hell in text content.

The CDATA dilemma solution

<boo k s h e l f><book i d=”1”>

< t i t l e l ang=”en”>Be a u t i f u l code</ t i t l e>

<h i n t>U29tZSBleGFtcGxlcyBzaG93IHRoZSB1c2FnZSBvZiA8IVtDREFUQVsgXV0+

</ h i n t>

</book></ bo o k s h e l f>

Base 64 in PHP

Use the built in functions base64 encode() and base64 decode().

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 15 / 102

Page 31: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Comments

Comments in XML

<boo k s h e l f>

<book i d=”1”>< t i t l e l ang=”en”>Be a u t i f u l code</ t i t l e><autho r>A. Oram</ autho r><autho r>G. Wi lson</ autho r><yea r>2007</ yea r><p r i c e c u r r e n c y=”Euro”>35 .95</ p r i c e>

</book>

<!−− . . . more books . . . −−>

</ bo o k s h e l f>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 16 / 102

Page 32: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Namespaces

Namespaces

Allow to avoid naming conflicts between different XML sources.

Single, default namespace

<boo k s h e l f xmlns=” h t t p : // example . com/book”>

<book i d=”1”>< t i t l e l ang=”en”>Be a u t i f u l code</ t i t l e><autho r>A. Oram</ autho r><autho r>G. Wi lson</ autho r><yea r>2007</ yea r><p r i c e c u r r e n c y=”Euro”>35 .95</ p r i c e>

</book>

</ bo o k s h e l f>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 17 / 102

Page 33: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Namespaces

Namespaces

Allow to avoid naming conflicts between different XML sources.

Multiple namespaces

<boo k s h e l fxmlns=” h t t p : // example . com/book”xmlns :book=” h t t p : // example . com/book”xmln s :dc=” h t t p : // p u r l . o rg /dc/ e l ement s /1 .1/ ”

>

<book i d=”1”><d c : t i t l e book : l a ng=”en”>Be a u t i f u l code</ d c : t i t l e><autho r>A. Oram</ autho r><autho r>G. Wi lson</ autho r><yea r>2007</ yea r><p r i c e c u r r e n c y=”Euro”>35 .95</ p r i c e>

</book>

</ bo o k s h e l f>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 17 / 102

Page 34: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline

1 XML

2 XML in PHPDOM

Introductional exampleEssential classesPractical DOM

XMLReader/-Writer (by example)SimpleXml (by example)

3 XPath

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 18 / 102

Page 35: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Overview

XML APIs

PHP has quite some XML APIs.

The most important are:

DOMXMLReader / XMLWriterSimpleXML

Deprecated are:

DOM XMLXML Parser

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 19 / 102

Page 36: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Overview - DOM

Document Object Model

Standardized API to access XML tree

W3C recommendationLevel 1 in 1999Currently: Level 3 (2004)

Available in many languages

CJavaPerlPython...

Represents XML nodes as objects

Loads full XML tree into memory

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 20 / 102

Page 37: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Overview - XMLReader/-Writer

Popular approach to access XML data

Similar implementations available in

JavaC#

Pull / push based

Does not load XML fully into memory

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 21 / 102

Page 38: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Overview - SimpleXml

Very simple access to XML data

Unique (?) to PHP

Represents XML structures as objects

Initial implementation hackish

Loads full XML tree into memory

You don’t want to use SimpleXML, seriously!

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 22 / 102

Page 39: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

APIs compared

DOM XMLReader/-Writer SimpleXML

Read ? ? •Write ? ? ◦Manipulate ? • •Full control ? ? -

Namespaces ? ? ◦XPath ? - ?

Validate DTD DTD -Schema SchemaRelaxNG RelaxNG

Comfort • ◦ ?

? Fully supported

• Supported but not nice

◦ Poorly supported

- Not supported at all

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 23 / 102

Page 40: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - XML in PHP

1 XML

2 XML in PHPDOM

Introductional exampleEssential classesPractical DOM

XMLReader/-Writer (by example)SimpleXml (by example)

3 XPath

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 24 / 102

Page 41: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - DOM

Introductional example

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 25 / 102

Page 42: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Getting started

Printing all authors

$dom = new DOMDocument ( ) ;$dom−>l o ad ( ’ s o u r c e s / example . xml ’ ) ;

$au tho r s = $dom−>getElementsByTagName ( ’ au tho r ’ ) ;

foreach ( $au tho r s as $author ){

echo ’ Author : ’ . $author−>nodeValue . ”\n” ;}

Output

Author : A . OramAuthor : G . Wi l sonAuthor : T . S c h l i t tAuthor : K. Nordmann

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 26 / 102

Page 43: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - DOM

Essential classes

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 27 / 102

Page 44: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

DOMNode

Purpose

Base class for all nodes types (elements, attributes, ...).

Typical tree operations

appendChild()removeChild()replaceChild()hasChildNodes()insertBefore()

DOM specific operations

cloneNode()lookupNamespaceURI()lookupPrefix()isDefaultNamespace()normalize()

Typical tree properties

$parent$childNodes$previousSibling$nextSibling

DOM specific properties

$nodeType$nodeValue$ownerDocument$namespaceURI$prefix$localName

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 28 / 102

Page 45: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

DOMNode

Purpose

Base class for all nodes types (elements, attributes, ...).

Typical tree operations

appendChild()removeChild()replaceChild()hasChildNodes()insertBefore()

DOM specific operations

cloneNode()lookupNamespaceURI()lookupPrefix()isDefaultNamespace()normalize()

Typical tree properties

$parent$childNodes$previousSibling$nextSibling

DOM specific properties

$nodeType$nodeValue$ownerDocument$namespaceURI$prefix$localName

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 28 / 102

Page 46: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

DOMNode

Purpose

Base class for all nodes types (elements, attributes, ...).

Typical tree operations

appendChild()removeChild()replaceChild()hasChildNodes()insertBefore()

DOM specific operations

cloneNode()lookupNamespaceURI()lookupPrefix()isDefaultNamespace()normalize()

Typical tree properties

$parent$childNodes$previousSibling$nextSibling

DOM specific properties

$nodeType$nodeValue$ownerDocument$namespaceURI$prefix$localName

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 28 / 102

Page 47: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

DOMNode

Purpose

Base class for all nodes types (elements, attributes, ...).

Typical tree operations

appendChild()removeChild()replaceChild()hasChildNodes()insertBefore()

DOM specific operations

cloneNode()lookupNamespaceURI()lookupPrefix()isDefaultNamespace()normalize()

Typical tree properties

$parent$childNodes$previousSibling$nextSibling

DOM specific properties

$nodeType$nodeValue$ownerDocument$namespaceURI$prefix$localName

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 28 / 102

Page 48: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

DOMElement

Purpose

Representation of an element (extends node)

Attribute related operations

hasAttribute[NS]()getAttribute[NS]()getAttributeNode[NS]()setAttribute[NS]()removeAttribute[NS]()

Element related operations

getElementsByTagName[NS]()appendChild() (inherited)removeChild() (inherited)replaceChild() (inherited)

Properties

$tagName

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 29 / 102

Page 49: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

DOMElement

Purpose

Representation of an element (extends node)

Attribute related operations

hasAttribute[NS]()getAttribute[NS]()getAttributeNode[NS]()setAttribute[NS]()removeAttribute[NS]()

Element related operations

getElementsByTagName[NS]()appendChild() (inherited)removeChild() (inherited)replaceChild() (inherited)

Properties

$tagName

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 29 / 102

Page 50: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

DOMElement

Purpose

Representation of an element (extends node)

Attribute related operations

hasAttribute[NS]()getAttribute[NS]()getAttributeNode[NS]()setAttribute[NS]()removeAttribute[NS]()

Element related operations

getElementsByTagName[NS]()appendChild() (inherited)removeChild() (inherited)replaceChild() (inherited)

Properties

$tagName

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 29 / 102

Page 51: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

DOMDocument

Purpose

Representation of a XML document

Creation methods

createAttribute[NS]()createElement[NS]()

Element retrieval methods

getElementsByTagName[NS]()getElementById()

Misc operations

registerNodeClass()validate()schemaValidate()relaxNGValidate()

Load /save methods

load/save()load/saveHTMLFile()

Properties

$documentElement$documentURI$preserveWhitespace$formatOutput$doctype$xmlVersion$xmlEncoding$recover (not DOM!)

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 30 / 102

Page 52: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

DOMDocument

Purpose

Representation of a XML document

Creation methods

createAttribute[NS]()createElement[NS]()

Element retrieval methods

getElementsByTagName[NS]()getElementById()

Misc operations

registerNodeClass()validate()schemaValidate()relaxNGValidate()

Load /save methods

load/save()load/saveHTMLFile()

Properties

$documentElement$documentURI$preserveWhitespace$formatOutput$doctype$xmlVersion$xmlEncoding$recover (not DOM!)

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 30 / 102

Page 53: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

DOMDocument

Purpose

Representation of a XML document

Creation methods

createAttribute[NS]()createElement[NS]()

Element retrieval methods

getElementsByTagName[NS]()getElementById()

Misc operations

registerNodeClass()validate()schemaValidate()relaxNGValidate()

Load /save methods

load/save()load/saveHTMLFile()

Properties

$documentElement$documentURI$preserveWhitespace$formatOutput$doctype$xmlVersion$xmlEncoding$recover (not DOM!)

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 30 / 102

Page 54: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

DOMDocument

Purpose

Representation of a XML document

Creation methods

createAttribute[NS]()createElement[NS]()

Element retrieval methods

getElementsByTagName[NS]()getElementById()

Misc operations

registerNodeClass()validate()schemaValidate()relaxNGValidate()

Load /save methods

load/save()load/saveHTMLFile()

Properties

$documentElement$documentURI$preserveWhitespace$formatOutput$doctype$xmlVersion$xmlEncoding$recover (not DOM!)

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 30 / 102

Page 55: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

DOMDocument

Purpose

Representation of a XML document

Creation methods

createAttribute[NS]()createElement[NS]()

Element retrieval methods

getElementsByTagName[NS]()getElementById()

Misc operations

registerNodeClass()validate()schemaValidate()relaxNGValidate()

Load /save methods

load/save()load/saveHTMLFile()

Properties

$documentElement$documentURI$preserveWhitespace$formatOutput$doctype$xmlVersion$xmlEncoding$recover (not DOM!)

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 30 / 102

Page 56: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

DOMNodeList

Purpose

Collection of DOMNodes (elements, attributes, ...). Iteratable.

Operations

item()

Properties

$length

Standard

f o r ( $ i = 0 ; $ i < $author s−>l e n g t h ; ++$ i ){

echo ’ Author : ’. $author s−>i t em ( $ i )−>nodeValue . ”\n” ;

}

Foreach

foreach ( $au tho r s as $author ){

echo ’ Author : ’. $author−>nodeValue . ”\n” ;

}

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 31 / 102

Page 57: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

DOMNodeList

Purpose

Collection of DOMNodes (elements, attributes, ...). Iteratable.

Operations

item()

Properties

$length

Standard

f o r ( $ i = 0 ; $ i < $author s−>l e n g t h ; ++$ i ){

echo ’ Author : ’. $author s−>i t em ( $ i )−>nodeValue . ”\n” ;

}

Foreach

foreach ( $au tho r s as $author ){

echo ’ Author : ’. $author−>nodeValue . ”\n” ;

}

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 31 / 102

Page 58: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - DOM

Practical DOM

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 32 / 102

Page 59: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Reading XML I

Reading

$dom = new DOMDocument ( ) ;$dom−>l o ad ( ’ s o u r c e s / example . xml ’ ) ;

$ r oo t = $dom−>documentElement ;

echo ’ Document i s a ’ . $ root−>tagName . ”\n” ;

$books = $root−>getElementsByTagName ( ’ book ’ ) ;

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 33 / 102

Page 60: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Reading XML II

Reading II

foreach ( $books as $book ){

echo ’ Book wi th ID ’. $book−>g e tA t t r i b u t e ( ’ i d ’ ) ;

foreach ( $book−>getElementsByTagName ( ’ p r i c e ’ ) as$ p r i c e )

{echo ’ c o s t s ’

. $ p r i c e−>nodeValue . ’ ’

. $ p r i c e−>g e tA t t r i b u t e ( ’ c u r r e n c y ’ )

. ”\n” ;}

}

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 34 / 102

Page 61: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Reading XML III

Output

Document i s a b o o k s h e l fBook wi th ID 1 c o s t s 35 .95 EuroBook wi th ID 2 c o s t s 39 .95 Euro

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 35 / 102

Page 62: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Create XML I

Creating

$dom = new DOMDocument( ’ 1 . 0 ’ , ’UTF−8 ’ ) ;$dom−>formatOutput = t rue ;

$ r oo t = $dom−>appendCh i ld ($dom−>c r e a t eE l emen t ( ’ d v d s h e l f ’ )

) ;

$dvd = $root−>appendCh i ld ($dom−>c r e a t eE l emen t ( ’ dvd ’ )

) ;

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 36 / 102

Page 63: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Create XML II

Creating II

$dvd−>s e t A t t r i b u t e ( ’ i d ’ , 1 ) ;

$dvd−>appendCh i ld ($dom−>c r e a t eE l emen t ( ’ t i t l e ’ , ’ S t a r Trek ’ )

) ;

$dom−>save ( ’ s o u r c e s / dom create . xml ’ ) ;

Output

<?xml ve r s i on=” 1 .0 ” encod ing=”UTF−8”?><d v d s h e l f>

<dvd i d=”1”>< t i t l e>Sta r Trek</ t i t l e>

</dvd></ d v d s h e l f>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 37 / 102

Page 64: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Manipulate XML I

Source XML

<?xml ve r s i on=” 1 .0 ” encod ing=”UTF−8”?><boo k s h e l f> <book i d=”1”>< t i t l e l ang=”en”>Be a u t i f u l code</

t i t l e><autho r>A. Oram</ autho r> <autho r>G. Wi lson</ autho r>

<yea r>2007</ yea r><p r i c e

c u r r e n c y=”Euro”>35 .95</ p r i c e> </book></ bo o k s h e l f>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 38 / 102

Page 65: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Manipulate XML II

Manipulating

$dom = new DOMDocument ( ) ;$dom−>formatOutput = t rue ;

$dom−>l o ad (’ s o u r c e s / examp l e we i r d f o rmat . xml ’ ,LIBXML NOBLANKS

) ;

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 39 / 102

Page 66: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Manipulate XML III

Manipulating II

$ roo t = $dom−>documentElement ;

$newBook = $root−>appendCh i ld ($dom−>c r e a t eE l emen t ( ’ book ’ )

) ;

$newBook−>s e t A t t r i b u t e ( ’ i d ’ , 2 ) ;

$newBook−>appendCh i ld ($dom−>c r e a t eE l emen t (

’ t i t l e ’ , ’ eZ Components − Das Entw i ck l e rhandbuch ’)

) ;

$dom−>save ( ’ s o u r c e s / dom manipu late . xml ’ ) ;

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 40 / 102

Page 67: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Manipulate XML IV

Output

<?xml ve r s i on=” 1 .0 ” encod ing=”UTF−8”?><boo k s h e l f>

<book i d=”1”>< t i t l e l ang=”en”>Be a u t i f u l code</ t i t l e><autho r>A. Oram</ autho r><autho r>G. Wi lson</ autho r><yea r>2007</ yea r><p r i c e c u r r e n c y=”Euro”>35 .95</ p r i c e>

</book><book i d=”2”>

< t i t l e>eZ Components − Das Entw i ck l e rhandbuch</ t i t l e></book>

</ bo o k s h e l f>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 41 / 102

Page 68: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - XML in PHP

1 XML

2 XML in PHPDOM

Introductional exampleEssential classesPractical DOM

XMLReader/-Writer (by example)SimpleXml (by example)

3 XPath

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 42 / 102

Page 69: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Reading XML I

Reading

$ r e ad e r = new XMLReader ( ) ;$ r eade r−>open ( ’ s o u r c e s / example . xml ’ ) ;

whi le ( $ r eade r−>r ead ( ) ){

i f ( $ r eade r−>nodeType !== XMLReader : : ELEMENT ){

cont inue ;}

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 43 / 102

Page 70: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Reading XML II

Reading

i f ( $ r eade r−>loca lName === ’ book ’ ){

echo ’ Book wi th ID ’. $ r eade r−>g e tA t t r i b u t e ( ’ i d ’ ) ;

}

i f ( $ r eade r−>loca lName === ’ p r i c e ’ ){

echo ’ c o s t s ’. $ r eade r−>r e a d S t r i n g ( ) . ’ ’. $ r eade r−>g e tA t t r i b u t e ( ’ c u r r e n c y ’ ). ”\n” ;

}}

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 44 / 102

Page 71: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Reading XML III

Output

Book wi th ID 1 c o s t s 35 .95 EuroBook wi th ID 2 c o s t s 39 .95 Euro

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 45 / 102

Page 72: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Create XML I

Creating

$w r i t e r = new XMLWriter ( ) ;$w r i t e r−>openUr i ( ’ s o u r c e s / xm lw r i t e r c r e a t e . xml ’ ) ;

$w r i t e r−>s e t I n d e n t S t r i n g ( ’ ’ ) ;$w r i t e r−>s e t I n d e n t ( t rue ) ;

$w r i t e r−>s tar tDocument ( ’ 1 . 0 ’ , ’UTF−8 ’ ) ;$w r i t e r−>s t a r tE l emen t ( ’ d v d s h e l f ’ ) ;

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 46 / 102

Page 73: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Create XML II

Creating II

$w r i t e r−>s t a r tE l emen t ( ’ dvd ’ ) ;$w r i t e r−>w r i t e A t t r i b u t e ( ’ i d ’ , ’ 1 ’ ) ;

$w r i t e r−>wr i t eE l emen t ( ’ t i t l e ’ , ’ S t a r Trek ’ ) ;

$w r i t e r−>endElement ( ) ; // <dvd>

$w r i t e r−>f l u s h ( ) ;

$w r i t e r−>endElement ( ) ; // <dvd sh e l f >$w r i t e r−>endDocument ( ) ;

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 47 / 102

Page 74: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Create XML III

Output

<?xml ve r s i on=” 1 .0 ” encod ing=”UTF−8”?><boo k s h e l f>

<book i d=”1”>< t i t l e>Be a u t i f u l code</ t i t l e>

</book></ bo o k s h e l f>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 48 / 102

Page 75: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - XML in PHP

1 XML

2 XML in PHPDOM

Introductional exampleEssential classesPractical DOM

XMLReader/-Writer (by example)SimpleXml (by example)

3 XPath

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 49 / 102

Page 76: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Reading XML I

Reading

$xml = s i m p l e x m l l o a d f i l e ( ’ s o u r c e s / example . xml ’ ) ;

foreach ( $xml−>book as $book ){

echo ’ Book wi th ID ’. $book [ ’ i d ’ ]. ’ c o s t s ’. $book−>p r i c e . ’ ’. $book−>p r i c e [ ’ c u r r e n c y ’ ]. ”\n” ;

}

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 50 / 102

Page 77: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Reading XML II

Output

Book wi th ID 1 c o s t s 35 .95 EuroBook wi th ID 2 c o s t s 39 .95 Euro

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 51 / 102

Page 78: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Create XML I

Creating

$xml = new SimpleXmlElement ( ’<dvd sh e l f ></dvd sh e l f > ’ ) ;

$xml−>addCh i l d ( ’ dvd ’ ) ;$xml−>dvd [0]−> addA t t r i b u t e ( ’ i d ’ , 1 ) ;

$xml−>dvd [0]−> addCh i l d ( ’ t i t l e ’ , ’ S t a r Trek ’ ) ;

$xml−>asXML( ’ s o u r c e s / s imp l e xm l c r e a t e . xml ’ ) ;

Output

<?xml ve r s i on=” 1 .0 ”?><d v d s h e l f><dvd i d=”1”>< t i t l e>Sta r Trek</ t i t l e></dvd></

d v d s h e l f>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 52 / 102

Page 79: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline

1 XML

2 XML in PHP

3 XPathOverviewBasicsIn depth

AxisPredicates

XPath in actionNamespaces in RDFpQuery

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 53 / 102

Page 80: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - XPath

1 XML

2 XML in PHP

3 XPathOverviewBasicsIn depth

AxisPredicates

XPath in actionNamespaces in RDFpQuery

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 54 / 102

Page 81: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Overview

XPath

Enables you to select information parts from XML documents.

Traverse the XML tree

Select XML nodes

W3C recommendation

Version 1: November 1999

Version 2: January 2007

Fields of application

XSLT (XML Stylesheet Language Transformations)Fetching XML nodes within programming languages

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 55 / 102

Page 82: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Overview

XPath

Enables you to select information parts from XML documents.

Traverse the XML tree

Select XML nodes

W3C recommendation

Version 1: November 1999

Version 2: January 2007

Fields of application

XSLT (XML Stylesheet Language Transformations)Fetching XML nodes within programming languages

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 55 / 102

Page 83: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

XML example reminder

<boo k s h e l f><book id=”1”>

< t i t l e l ang=”en”>Be a u t i f u l code</ t i t l e><author>A. Oram</author><author>G. Wilson</author><yea r>2007</ yea r><p r i c e c u r r e n c y=”Euro”>35 .95</ p r i c e>

</book>

<book id=”2”>< t i t l e l ang=”de”>eZ Components − Das Entw i ck l e rhandbuch</

t i t l e><author>T. Schlitt</author><author>K. Nordmann</author><yea r>2007</ yea r><p r i c e c u r r e n c y=”Euro”>39 .95</ p r i c e>

</book></ bo o k s h e l f>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 56 / 102

Page 84: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Introductory example

<boo k s h e l f><book id=”1”>

< t i t l e l ang=”en”>Be a u t i f u l code</ t i t l e><author>A. Oram</author><author>G. Wilson</author><yea r>2007</ yea r><p r i c e c u r r e n c y=”Euro”>35 .95</ p r i c e>

</book>

<book id=”2”>< t i t l e l ang=”de”>eZ Components − Das Entw i ck l e rhandbuch</

t i t l e><author>T. Schlitt</author><author>K. Nordmann</author><yea r>2007</ yea r><p r i c e c u r r e n c y=”Euro”>39 .95</ p r i c e>

</book></ bo o k s h e l f>

2 variants to fetch all books

/ bo o k s h e l f /book//book

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 56 / 102

Page 85: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Introductory example

<boo k s h e l f><book id=”1”>

< t i t l e l ang=”en”>Be a u t i f u l code</ t i t l e><author>A. Oram</author><author>G. Wilson</author><yea r>2007</ yea r><p r i c e c u r r e n c y=”Euro”>35 .95</ p r i c e>

</book>

<book id=”2”>< t i t l e l ang=”de”>eZ Components − Das Entw i ck l e rhandbuch</

t i t l e><author>T. Schlitt</author><author>K. Nordmann</author><yea r>2007</ yea r><p r i c e c u r r e n c y=”Euro”>39 .95</ p r i c e>

</book></ bo o k s h e l f>

2 variants to fetch all authors

/ bo o k s h e l f /book/ autho r// autho r

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 56 / 102

Page 86: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - XPath

1 XML

2 XML in PHP

3 XPathOverviewBasicsIn depth

AxisPredicates

XPath in actionNamespaces in RDFpQuery

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 57 / 102

Page 87: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Addressing

Every XPath expression matches a set of nodes (0..n)

It encodes an “address” for the selected nodes

Simple XPath expressions look similar to Unix file system addresses

Two generally different ways of addressing are supported

Absolute addressing

/ bo o k s h e l f /book/ t i t l e/ b o o k s h e l f /book/ autho r

Relative addressing

book/ autho r. . / t i t l e

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 58 / 102

Page 88: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Contexts

Every expression step creates a new context

The next is evaluated in the context created by the previous one

Contexts

/ bo o k s h e l f /book/ t i t l e

/ resets the context to global

bookshelf selects all <bookshelf> elements in the global context

/ creates a new context, all children of <bookshelf>

book selects all <book> elements in this context

/ creates a new context, all children of selected <book>s

title selects all < title> elements in this context

→ A set of all title element nodes.

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 59 / 102

Page 89: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Attributes

Select attributes

Prepend the attribute name with an @.

Select all currency attributes

/ bo o k s h e l f /book/ p r i c e / @cur rency

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 60 / 102

Page 90: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Steps

Navigation

Navigation is not only possible in parent → child direction.

Navigate to parent

. . // b o o k s h e l f /book/ t i t l e / . . / au tho r

Navigate to descendants

// t i t l e/ b o o k s h e l f /book// @cur rency

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 61 / 102

Page 91: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Indexing

Access nodes by position

It is possible to access a specific node in a set by its position.

Indexing

/ bo o k s h e l f /book [ 2 ]/ b o o k s h e l f /book/ autho r [ 1 ]

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 62 / 102

Page 92: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Indexing

Access nodes by position

It is possible to access a specific node in a set by its position.

Indexing

/ bo o k s h e l f /book [ 2 ]/ b o o k s h e l f /book/ autho r [ 1 ]

Start index

Indexing generally 1 based

Some Internet Explorer versions start with 0

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 63 / 102

Page 93: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Wildcards

Wildcard search

A wildcard represents a node of a certain type with arbitrary name.

Wildcards

/ bo o k s h e l f /∗/ t i t l e/ b o o k s h e l f /book/@∗

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 64 / 102

Page 94: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Union

Union

Union the node sets selected by multiple XPath expressions.

Union

/ bo o k s h e l f /book/ t i t l e |/ bo o k s h e l f /book/ autho r

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 65 / 102

Page 95: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

A first PHP example

Querying first book title

$dom = new DOMDocument ( ) ;$dom−>l o ad ( ’ s o u r c e s / example . xml ’ ) ;

$xpath = new DOMXPath( $dom ) ;

$ t i t l e s = $xpath−>query ( ’ / b o o k s h e l f /book [ 1 ] / t i t l e ’ ) ;

echo ’ T i t l e o f f i r s t book i s ’. $ t i t l e s −>i t em ( 0 )−>nodeValue . ”\n” ;

Output

T i t l e o f f i r s t book i s B e a u t i f u l code

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 66 / 102

Page 96: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Second PHP example

Querying all currencies

// . . .

$ c u r r e n c i e s = $xpath−>query ( ’ // p r i c e / @cur rency ’ ) ;

echo ” Fo l l ow i ng c u r r e n c i e s occu r :\ n” ;

foreach ( $ c u r r e n c i e s as $ cu r r en c y ){

echo $cu r r ency−>nodeValue . ”\n” ;}

Output

Fo l l ow i ng c u r r e n c i e s occu r :EuroEuro

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 67 / 102

Page 97: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - XPath

1 XML

2 XML in PHP

3 XPathOverviewBasicsIn depth

AxisPredicates

XPath in actionNamespaces in RDFpQuery

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 68 / 102

Page 98: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

XPath syntax

An XPath query consists ofsteps

Each step consists of:

1 an axis2 a node test3 a predicate

4 axis:

child (default)attribute (@)descendant-or-self (//)parent (..)

Node tests:

name of the node (default)wildcard (*)

Predicate:

accessing a node by index

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 69 / 102

Page 99: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

XPath syntax

An XPath query consists ofsteps

Each step consists of:

1 an axis2 a node test3 a predicate

You already saw examples

4 axis:

child (default)attribute (@)descendant-or-self (//)parent (..)

Node tests:

name of the node (default)wildcard (*)

Predicate:

accessing a node by index

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 69 / 102

Page 100: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

XPath syntax

An XPath query consists ofsteps

Each step consists of:

1 an axis2 a node test3 a predicate

You already saw examples

4 axis:

child (default)attribute (@)descendant-or-self (//)parent (..)

Node tests:

name of the node (default)wildcard (*)

Predicate:

accessing a node by index

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 69 / 102

Page 101: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

XPath syntax

An XPath query consists ofsteps

Each step consists of:

1 an axis2 a node test3 a predicate

You already saw examples

4 axis:

child (default)attribute (@)descendant-or-self (//)parent (..)

Node tests:

name of the node (default)wildcard (*)

Predicate:

accessing a node by index

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 69 / 102

Page 102: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

XPath syntax

An XPath query consists ofsteps

Each step consists of:

1 an axis2 a node test3 a predicate

You already saw examples

4 axis:

child (default)attribute (@)descendant-or-self (//)parent (..)

Node tests:

name of the node (default)wildcard (*)

Predicate:

accessing a node by index

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 69 / 102

Page 103: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

XPath syntax

An XPath query consists ofsteps

Each step consists of:

1 an axis2 a node test3 a predicate

You already saw examples

4 axis:

child (default)attribute (@)descendant-or-self (//)parent (..)

Node tests:

name of the node (default)wildcard (*)

Predicate:

accessing a node by index

Step syntax

<ax i s >::< node te s t >[<p r e d i c a t e >]

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 69 / 102

Page 104: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - In depth

Axis

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 70 / 102

Page 105: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Axis

13 dimensions

Imagine an XML document to be a 13 dimensional space...

Axis Shortcut

ancestor

ancestor-or-self

attribute @child -

descendant

descendant-or-self //following

following-sibling

namespace

parent ..preceding

preceding-sibling

self .

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 71 / 102

Page 106: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Axis

Axis syntax

<axisname >::< node te s t >

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 72 / 102

Page 107: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Axis

Axis syntax

<axisname >::< node te s t >

Child axis

/ bo o k s h e l f /bookc h i l d : : b o o k s h e l f / c h i l d : : book

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 72 / 102

Page 108: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Axis

Axis syntax

<axisname >::< node te s t >

Descendant-or-self axis

//bookdescendant−or− s e l f : : book

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 72 / 102

Page 109: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Axis

Axis syntax

<axisname >::< node te s t >

Attribute axis

//book/@iddescendant−or− s e l f : : book/ a t t r i b u t e : : i d

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 72 / 102

Page 110: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Axis

Axis syntax

<axisname >::< node te s t >

Parent axis

//book/@id / . .descendant−or− s e l f : : book/ a t t r i b u t e : : i d / pa r en t : : node ( )

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 72 / 102

Page 111: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Ancestor axis

XPath with ancestor axis

// t i t l e / a n c e s t o r : : ∗

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 73 / 102

Page 112: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Ancestor axis

XPath with ancestor axis

// t i t l e / a n c e s t o r : : ∗

Selected XML nodes

<?xml ve r s i on=” 1 .0 ” encod ing=”UTF−8”?><bookshelf>

<book id=”1”>< t i t l e l ang=”en”>Be a u t i f u l code</ t i t l e><autho r>A. Oram</ autho r><autho r>G. Wi lson</ autho r><yea r>2007</ yea r><p r i c e c u r r e n c y=”Euro”>35 .95</ p r i c e>

</book><!−− . . . −−>

</bookshelf>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 73 / 102

Page 113: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Following axis

XPath with ancestor axis

//book/ f o l l o w i n g : : ∗

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 74 / 102

Page 114: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Following axis

XPath with ancestor axis

//book/ f o l l o w i n g : : ∗

Selected XML nodes

<?xml ve r s i on=” 1 .0 ” encod ing=”UTF−8”?><boo k s h e l f>

<book i d=”1”>< t i t l e l ang=”en”>Be a u t i f u l code</ t i t l e><!−− . . . −−></book>

<book id=”2”><title lang=”de”>eZ Components - Das Entwicklerhandbuch</title><!– ... –>

</book></ bo o k s h e l f>

Preceding axis works the same.Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 74 / 102

Page 115: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Following-sibling axis

XPath with ancestor axis

//book/ f o l l ow i n g−s i b l i n g : : ∗

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 75 / 102

Page 116: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Following-sibling axis

XPath with ancestor axis

//book/ f o l l ow i n g−s i b l i n g : : ∗

Selected XML nodes

<?xml ve r s i on=” 1 .0 ” encod ing=”UTF−8”?><boo k s h e l f>

<book i d=”1”>< t i t l e l ang=”en”>Be a u t i f u l code</ t i t l e><!−− . . . −−></book>

<book id=”2”>< t i t l e l ang=”de”>eZ Components − Das Entw i ck l e rhandbuch

</ t i t l e><!−− . . . −−>

</book></ bo o k s h e l f>

Preceding-sibling axis works the same.

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 75 / 102

Page 117: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Namespace axis

XPath with namespace axis

namespace : : ∗

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 76 / 102

Page 118: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Namespace axis

XPath with namespace axis

namespace : : ∗

XML with namespaces

<boo k s h e l fxmlns=”http://example.com/book”xmlns:book=”http://example.com/book”xmlns:dc=”http://purl.org/dc/elements/1.1/”

><book i d=”1”>

<d c : t i t l e book : l a ng=”en”>Be a u t i f u l code</ d c : t i t l e><autho r>A. Oram</ autho r><autho r>G. Wi lson</ autho r><yea r>2007</ yea r><p r i c e c u r r e n c y=”Euro”>35 .95</ p r i c e>

</book></ bo o k s h e l f>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 76 / 102

Page 119: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - In depth

Predicates

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 77 / 102

Page 120: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Predicate syntax

You already saw indexing with numeric predicates

Predicates can also be booleans

Functions and operators allow fine grained tests

Predicate syntax

<node te s t >[<p r e d i c a t e >]

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 78 / 102

Page 121: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Predicate examples

Select all books with ID 1

//book [ @id = ’ 1 ’ ]

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 79 / 102

Page 122: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Predicate examples

Select all books that have any attribute at all

//book [@∗ ]// book/@∗ / . .

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 79 / 102

Page 123: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Predicate examples

Select all books with price round 40

//book [ round ( p r i c e ) = 40 ]

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 79 / 102

Page 124: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Predicate examples

Select all authors with first name initial T

//book/ autho r [ s u b s t r i n g ( . , 1 , 1) = ’T ’ ]

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 79 / 102

Page 125: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Operator overview

Mathematical operators

+, −, ∗ Addition, subtraction, multiplication

div Division

mod Modulo operation

Comparison operators

= Check for equality

! = Check for inequality

<, <= Less than and less than or equal

>, >= Greater than and greater than or equal

Logical operators

or Logical or

and Logical and

Logical negation

not() is a function in XPath!

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 80 / 102

Page 126: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Operator overview

Mathematical operators

+, −, ∗ Addition, subtraction, multiplication

div Division

mod Modulo operation

Comparison operators

= Check for equality

! = Check for inequality

<, <= Less than and less than or equal

>, >= Greater than and greater than or equal

Logical operators

or Logical or

and Logical and

Logical negation

not() is a function in XPath!

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 80 / 102

Page 127: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Operator overview

Mathematical operators

+, −, ∗ Addition, subtraction, multiplication

div Division

mod Modulo operation

Comparison operators

= Check for equality

! = Check for inequality

<, <= Less than and less than or equal

>, >= Greater than and greater than or equal

Logical operators

or Logical or

and Logical and

Logical negation

not() is a function in XPath!

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 80 / 102

Page 128: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Operator overview

Mathematical operators

+, −, ∗ Addition, subtraction, multiplication

div Division

mod Modulo operation

Comparison operators

= Check for equality

! = Check for inequality

<, <= Less than and less than or equal

>, >= Greater than and greater than or equal

Logical operators

or Logical or

and Logical and

Logical negation

not() is a function in XPath!

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 80 / 102

Page 129: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Functions by example

String functions

string-join() Concatenates 2 stringssubstring() Extracts a part from a string

Node set functions

count() Returns number of nodes in a setposition() Returns the position index of each node

Boolean functions

not() Negates the received boolean expressiontrue() Boolean true

Mathematical functions

round() Rounds the given number to the next integerfloor() Returns the next integer smaller than the given number

Function overview

An overview on all functions can be found onhttp://www.w3.org/TR/xpath-functions/

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 81 / 102

Page 130: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Functions by example

String functions

string-join() Concatenates 2 stringssubstring() Extracts a part from a string

Node set functions

count() Returns number of nodes in a setposition() Returns the position index of each node

Boolean functions

not() Negates the received boolean expressiontrue() Boolean true

Mathematical functions

round() Rounds the given number to the next integerfloor() Returns the next integer smaller than the given number

Function overview

An overview on all functions can be found onhttp://www.w3.org/TR/xpath-functions/

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 81 / 102

Page 131: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Functions by example

String functions

string-join() Concatenates 2 stringssubstring() Extracts a part from a string

Node set functions

count() Returns number of nodes in a setposition() Returns the position index of each node

Boolean functions

not() Negates the received boolean expressiontrue() Boolean true

Mathematical functions

round() Rounds the given number to the next integerfloor() Returns the next integer smaller than the given number

Function overview

An overview on all functions can be found onhttp://www.w3.org/TR/xpath-functions/

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 81 / 102

Page 132: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Functions by example

String functions

string-join() Concatenates 2 stringssubstring() Extracts a part from a string

Node set functions

count() Returns number of nodes in a setposition() Returns the position index of each node

Boolean functions

not() Negates the received boolean expressiontrue() Boolean true

Mathematical functions

round() Rounds the given number to the next integerfloor() Returns the next integer smaller than the given number

Function overview

An overview on all functions can be found onhttp://www.w3.org/TR/xpath-functions/

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 81 / 102

Page 133: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Functions by example

String functions

string-join() Concatenates 2 stringssubstring() Extracts a part from a string

Node set functions

count() Returns number of nodes in a setposition() Returns the position index of each node

Boolean functions

not() Negates the received boolean expressiontrue() Boolean true

Mathematical functions

round() Rounds the given number to the next integerfloor() Returns the next integer smaller than the given number

Function overview

An overview on all functions can be found onhttp://www.w3.org/TR/xpath-functions/

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 81 / 102

Page 134: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - XPath

1 XML

2 XML in PHP

3 XPathOverviewBasicsIn depth

AxisPredicates

XPath in actionNamespaces in RDFpQuery

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 82 / 102

Page 135: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - XPath in action

Namespaces in RDF

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 83 / 102

Page 136: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Find all namespaces in an RDF document

RDF Resource Description Framework

Semantic web

Makes heavy usage of different namespaces

Complex variant

//∗ [ name ( . ) = ’ r d f : D e s c r i p t i o n ’] / ∗ [ namespace−u r i ( . ) != namespace−u r i ( . . ) and

namespace−u r i ( . ) != ’ ’ andnamespace−u r i ( . ) != namespace−u r i ( p r e ced i ng−

s i b l i n g : : ∗ ) ]

Simpler variant

//∗ [ name ( . ) = ’ r d f : D e s c r i p t i o n ’ ] / namespaces : : ∗

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 84 / 102

Page 137: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Find all namespaces in an RDF document

RDF Resource Description Framework

Semantic web

Makes heavy usage of different namespaces

Complex variant

//∗ [ name ( . ) = ’ r d f : D e s c r i p t i o n ’] / ∗ [ namespace−u r i ( . ) != namespace−u r i ( . . ) and

namespace−u r i ( . ) != ’ ’ andnamespace−u r i ( . ) != namespace−u r i ( p r e ced i ng−

s i b l i n g : : ∗ ) ]

Simpler variant

//∗ [ name ( . ) = ’ r d f : D e s c r i p t i o n ’ ] / namespaces : : ∗

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 84 / 102

Page 138: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Outline - XPath in action

pQuery

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 85 / 102

Page 139: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

jQuery

Cool JavaScript framework

Allows selecting HTML elements by CSS selectors

http://jquery.com/

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 86 / 102

Page 140: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

pQuery

Little example class

Models a tiny bits of jQuery, using

DOMXPath

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 87 / 102

Page 141: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Example HTML

<html><head>

< t i t l e>Some web s i t e</ t i t l e></head>

<body><h1>Some h e a d l i n e</h1><p>

Some n i c e <a hre f=” t e s t . html ” c l a s s=” i n t e r n a l ”>con t en t</a> .

</p><h1 id=” second ”>Second h e a d l i n e</h1><p>

More n i c e <a hre f=” t e s t . html ”>con t en t</a> .</p>

</body></html>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 88 / 102

Page 142: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Usage

r equ i r e ’ pquery / pquery . php ’ ;

$dom = new DOMDocument ( ) ;$dom−>formatOutput = t rue ;$dom−>loadHTMLFile ( ’ . . / example . html ’ ) ;

$q = new pQuery ( $dom ) ;

$q−>query ( ’ h1#second ’ ) ;$q−>addC la s s ( ’ s omec l a s s ’ ) ;

$dom−>saveHTMLFile ( ’ . . / pqu e r y s imp l e . html ’ ) ;

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 89 / 102

Page 143: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

The pQuery class

c l a s s pQuery{

p r o t e c t e d $dom ;

p r o t e c t e d $con t e x t ;

p r o t e c t e d $xpath ;

p u b l i c f u n c t i o n c o n s t r u c t ( DOMDocument $dom ){

$ t h i s−>dom = $dom ;$ t h i s−>con t e x t = ar ray ( $dom−>documentElement ) ;$ t h i s−>xpath = new DOMXPath( $dom ) ;

}

// . . .

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 90 / 102

Page 144: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Issueing a query

// . . .

p u b l i c f u n c t i o n query ( $ s t r i n g ){

$xpath = $ th i s−>createXPath ( $ s t r i n g ) ;

echo ”Query ing wi th XPath ’ $xpath ’ .\ n” ;

$newContext = ar ray ( ) ;foreach ( $ t h i s−>con t e x t as $node ){

$nodeL i s t = $ th i s−>xpath−>query ( $xpath , $node ) ;foreach ( $nodeL i s t as $newNode ){

$newContext [ ] = $newNode ;}

}$ t h i s−>con t e x t = $newContext ;

}

// . . .

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 91 / 102

Page 145: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Creating XPath

// . . .

p r o t e c t e d f u n c t i o n createXPath ( $ s t r i n g ){

$pa r t s = explode ( ’ ’ , $ s t r i n g ) ;

$xpath = ar ray ( ) ;foreach ( $ p a r t s as $pa r t ){

$xpath [ ] = $ th i s−>c r e a t eS i ng l eXPa th ( $pa r t ) ;}

r e t u r n implode ( ’ | ’ , $xpath ) ;}

// . . .

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 92 / 102

Page 146: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Creating XPath part I

// . . .

p r o t e c t e d f u n c t i o n c r e a t eS i ng l eXPa th ( $ s t r i n g ){

$pa r t s = p r e g s p l i t (’ ( (# | \ . ) ) ’ ,$ s t r i n g ,−1,PREG SPLIT DELIM CAPTURE

) ;

$xpath = ’ // ’ ;

// . . .

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 93 / 102

Page 147: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Creating XPath part II

// . . .

i f ( count ( $ p a r t s ) < 3 ){

$xpath .= $ s t r i n g ;}e l s e{

$xpath .= $ th i s−>c r e a t e S e l e c t o r ( $ p a r t s ) ;}

r e t u r n $xpath ;}

// . . .

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 94 / 102

Page 148: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Creating selector

// . . .

p r o t e c t e d f u n c t i o n c r e a t e S e l e c t o r ( ar ray $pa r t s ){

switch ( $ p a r t s [ 1 ] ){

case ’ . ’ :r e t u r n $pa r t s [ 0 ]

. ’ [ c o n t a i n s ( @c la s s , ” ’ . $ p a r t s [ 2 ] . ’ ” )] ’ ;

break ;

case ’#’ :r e t u r n $pa r t s [ 0 ]

. ’ [ @id = ” ’ . $ p a r t s [ 2 ] . ’ ” ] ’ ;break ;

}}

}

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 95 / 102

Page 149: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Adding a class I

// . . .

p u b l i c f u n c t i o n addC la s s ( $ c l a s s ){

foreach ( $ t h i s−>con t e x t as $node ){

i f ( $node−>nodeType !== XML ELEMENT NODE ){

cont inue ;}

i f ( ! $node−>h a sA t t r i b u t e ( ’ c l a s s ’ ) ){

$ c l a s s e s = ar ray ( ) ;}e l s e{

$ c l a s s e s = explode (’ ’ ,$node−>g e tA t t r i b u t e ( ’ c l a s s ’ )

) ;}

// . . .

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 96 / 102

Page 150: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Adding a class II

// . . .

i f ( ! i n a r r a y ( $ c l a s s , $ c l a s s e s ) ){

$ c l a s s e s [ ] = $ c l a s s ;}

$node−>s e t A t t r i b u t e (’ c l a s s ’ ,implode ( ’ ’ , $ c l a s s e s )

) ;}

}

// . . .

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 97 / 102

Page 151: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Simple example

r equ i r e ’ pquery / pquery . php ’ ;

$dom = new DOMDocument ( ) ;$dom−>formatOutput = t rue ;$dom−>loadHTMLFile ( ’ . . / example . html ’ ) ;

$q = new pQuery ( $dom ) ;

$q−>query ( ’ h1#second ’ ) ;$q−>addC la s s ( ’ s omec l a s s ’ ) ;

$dom−>saveHTMLFile ( ’ . . / pqu e r y s imp l e . html ’ ) ;

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 98 / 102

Page 152: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Simple example result

<!DOCTYPE html PUBLIC ”−//W3C//DTD HTML 4 .0 T r a n s i t i o n a l //EN”” ht tp : //www.w3 . org /TR/REC−html40 / l o o s e . dtd ”>

<html><head><meta http−equ i v=”Content−Type” content=” t e x t / html ; c h a r s e t=

UTF−8”>< t i t l e>Some web s i t e</ t i t l e></head><body>

<h1>Some h e a d l i n e</h1><p>

Some n i c e <a hre f=” t e s t . html ” c l a s s=” i n t e r n a l ”>con t en t</a> .

</p><h1 id=” second ” c l a s s=” somec l a s s ”>Second h e a d l i n e</h1><p>

More n i c e <a hre f=” t e s t . html ”>con t en t</a> .</p>

</body></html>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 99 / 102

Page 153: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Advanced example

r equ i r e ’ pquery / pquery . php ’ ;

$dom = new DOMDocument ( ) ;$dom−>formatOutput = t rue ;$dom−>loadHTMLFile ( ’ . . / example . html ’ ) ;

$q = new pQuery ( $dom ) ;

$q−>query ( ’ h1 a . i n t e r n a l ’ ) ;$q−>addC la s s ( ’ c o o l c l a s s ’ ) ;

$dom−>saveHTMLFile ( ’ . . / pque ry advanced . html ’ ) ;

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 100 / 102

Page 154: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

Advanced example result

<!DOCTYPE html PUBLIC ”−//W3C//DTD HTML 4 .0 T r a n s i t i o n a l //EN”” ht tp : //www.w3 . org /TR/REC−html40 / l o o s e . dtd ”>

<html><head><meta http−equ i v=”Content−Type” content=” t e x t / html ; c h a r s e t=

UTF−8”>< t i t l e>Some web s i t e</ t i t l e></head><body>

<h1 c l a s s=” c o o l c l a s s ”>Some h e a d l i n e</h1><p>

Some n i c e <a hre f=” t e s t . html ” c l a s s=” i n t e r n a l c o o l c l a s s ”>con t en t</a> .

</p><h1 id=” second ” c l a s s=” c o o l c l a s s ”>Second h e a d l i n e</h1><p>

More n i c e <a hre f=” t e s t . html ”>con t en t</a> .</p>

</body></html>

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 101 / 102

Page 155: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

The end

Thank you for listening!

Are there any questions left?

I hope you learned what you expected?

Contact me: Tobias Schlitt <[email protected]>

Enjoy the conference!

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 102 / 102

Page 156: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

The end

Thank you for listening!

Are there any questions left?

I hope you learned what you expected?

Contact me: Tobias Schlitt <[email protected]>

Enjoy the conference!

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 102 / 102

Page 157: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

The end

Thank you for listening!

Are there any questions left?

I hope you learned what you expected?

Contact me: Tobias Schlitt <[email protected]>

Enjoy the conference!

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 102 / 102

Page 158: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

The end

Thank you for listening!

Are there any questions left?

I hope you learned what you expected?

Contact me: Tobias Schlitt <[email protected]>

Enjoy the conference!

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 102 / 102

Page 159: XML and XPath with PHP - Schlittfiles.schlitt.info/slides/ipc_2009_se_xml_and_xpath_with_php_schlitt.pdfXML and XPath with PHP TobiasSchlitt  IPC SE 2009 2009-05-29

The end

Thank you for listening!

Are there any questions left?

I hope you learned what you expected?

Contact me: Tobias Schlitt <[email protected]>

Enjoy the conference!

Tobias Schlitt (IPC SE 2009) XML and XPath with PHP 2009-05-29 102 / 102