Advanced XML and Web Services in PHP - PowerPoint PPT Presentation

About This Presentation
Title:

Advanced XML and Web Services in PHP

Description:

DOM. Tree based parser. Allows for creation and editing of XML documents. W3C Specification with DOM Level 2/3 compliancy. Provides XPath support ... – PowerPoint PPT presentation

Number of Views:1060
Avg rating:3.0/5.0
Slides: 152
Provided by: robertr156
Category:
Tags: php | xml | advanced | dom | services | web

less

Transcript and Presenter's Notes

Title: Advanced XML and Web Services in PHP


1
Advanced XML and Web Services in PHP
  • March 29, 2006
  • Robert Richards

2
Agenda
  • Introduction to Terms and Concepts
  • Libxml
  • DOM
  • SimpleXML
  • SAX (ext/xml)
  • XMLReader
  • XSL
  • XMLWriter
  • SOAP (ext/soap)

3
XML Namespaces
  • An XML Namespace is a collection of names
    identified by a URI.
  • They are applicable to elements and attributes.
  • Namespaces may or may not be associated with a
    prefix.
  • xmlnsrob"urnrob"
  • xmlnshttp//www.example.com/rob
  • Attributes never reside within a default
    namespace.
  • It is illegal to have two attributes with the
    same localname and same namespace on the same
    element.

4
XML Namespace Example
ltorder num"1001"gt ltshippinggt ltname
type"care_of"gtJohn Smithlt/namegt
ltaddressgt123 Herelt/addressgt lt/shippinggt
ltbillinggt ltname type"legal"gtJane
Doelt/namegt ltaddressgt456 Somewhere
elselt/addressgt lt/billinggt lt/ordergt
5
XML Namespace Example
ltorder num"1001" xmlns"urnorder"
xmlnsship"urnshipping"

xmlnsbill"urnbilling"gt ltshipshippinggt
ltshipname type"care_of"gtJohn
Smithlt/shipnamegt ltshipaddressgt123
Herelt/shipaddressgt lt/shipshippinggt
ltbillbillinggt ltbillname type"legal"gtJane
Doelt/billnamegt ltbilladdressgt456 Somewhere
elselt/billaddressgt lt/billbillinggt lt/ordergt
6
Illegal Namespace Usage
  • ltorder num"1001" xmlns"urnorder"

  • xmlnsorder"urnorder"

  • xmlnsship"urnorder"gt
  • ltshipping shiptype"fed_ex" type"fed_ex"gt
  • ltname shiptype"care_of"
  • ordertype"legal"gtJohn
    Smithlt/shipnamegt
  • lt/shipshippinggt
  • lt/ordergt

7
Illegal Namespace Usage
  • ltorder num"1001" xmlns"urnorder"

  • xmlnsorder"urnorder"

  • xmlnsship"urnorder"gt
  • ltshipping shiptype"fed_ex" type"fed_ex"gt
  • ltname shiptype"care_of"
  • ordertype"legal"gtJohn
    Smithlt/shipnamegt
  • lt/shipshippinggt
  • lt/ordergt
  • lt!-- attributes on shipping element are valid !
    --gt

8
Reserved Namespaces and Prefixes
  • The prefix xml is bound to http//www.w3.org/XML/1
    998/namespace.
  • The prefix xmlns is bound to http//www.w3.org/200
    0/xmlns/.
  • Prefixes should also not begin with the
    characters xml.

9
Schemas and Validation
  • Validation insures an XML document conforms to a
    set of defined rules.
  • Multiple mechanisms exist to write document rule
    sets
  • Document Type Definition (DTD)
  • XML Schema
  • RelaxNG

10
Document Type Definition (DTD)validation/courses-
dtd.xml
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
courses lt!ELEMENT courses (course)gt
lt!ELEMENT course (title, description, credits,
lastmodified)gt lt!ATTLIST course cid ID
REQUIREDgt lt!ELEMENT title (PCDATA)gt
lt!ELEMENT description (PCDATA)gt lt!ELEMENT
credits (PCDATA)gt lt!ELEMENT lastmodified
(PCDATA)gt gt ltcoursesgt ltcourse cid"c1"gt
lttitlegtBasic Languageslt/titlegt
ltdescriptiongtIntroduction to Languageslt/descriptio
ngt ltcreditsgt1.5lt/creditsgt
ltlastmodifiedgt2004-09-01T111301lt/lastmodifiedgt
lt/coursegt ltcourse cid"c2"gt .
. . lt/coursegt lt/coursesgt
11
DTD and IDsvalidation/course-id.xml
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
courses lt!ATTLIST course cid ID
REQUIREDgt gt ltcoursesgt ltcourse cid"c1"gt
lttitle xmlid"t1"gtBasic
Languageslt/titlegt ltdescriptiongtIntroduct
ion to Languageslt/descriptiongt lt/coursegt
ltcourse cid"c2"gt lttitle
xmlid"t3"gtFrench Ilt/titlegt
ltdescriptiongtIntroduction to Frenchlt/descriptiongt
lt/coursegt ltcourse cid"c3"gt
lttitle xmlid"t3"gtFrench IIlt/titlegt
ltdescriptiongtIntermediate Frenchlt/descriptiongt
lt/coursegt lt/coursesgt
12
XML Schemavalidation/course.xsd
lt?xml version"1.0"?gt ltxsdschema
xmlnsxsd"http//www.w3.org/2001/XMLSchema"gt
ltxsdelement name"courses"gt
ltxsdcomplexTypegt ltxsdsequencegt
ltxsdelement name"course" minOccurs"1"
maxOccurs"unbounded"gt
ltxsdcomplexTypegt
ltxsdsequencegt ltxsdelement
name"title" type"xsdstring"/gt
ltxsdelement name"description"
type"xsdstring"/gt
ltxsdelement name"credits" type"xsddecimal"/gt
ltxsdelement
name"lastmodified" type"xsddateTime"/gt
lt/xsdsequencegt
ltxsdattribute name"cid" type"xsdID"/gt
lt/xsdcomplexTypegt
lt/xsdelementgt lt/xsdsequencegt
lt/xsdcomplexTypegt lt/xsdelementgt lt/xsdschemagt
13
RelaxNGvalidation/course.rng
ltgrammar xmlns"http//relaxng.org/ns/structure/1.
0" datatypeLibrary"http//www.w3.org/200
1/XMLSchema-datatypes"gt ltstartgt
ltelement name"courses"gt ltzeroOrMoregt
ltelement name"course"gt
ltattribute name"cid"gtltdata type"ID"/gtlt/attribute
gt ltelement name"title"gtlttext/gtlt/el
ementgt ltelement name"description"gt
lttext/gtlt/elementgt ltelement
name"credits"gtltdata type"decimal"/gtlt/elementgt
ltelement name"lastmodified"gtltdata
type"dateTime"/gtlt/elementgt
lt/elementgt lt/zeroOrMoregt
lt/elementgt lt/startgt lt/grammargt
14
XPath
  • Language to locate and retrieve information from
    an XML document
  • A foundation for XSLT
  • An XML document is a tree containing nodes
  • The XML document is the root node
  • Locations are addressable similar to the syntax
    for a filesystem

15
XPath Reference Documentxpath/courses.xml
ltcourses xmlnst"http//www.example.com/title"gt
ltcourse xmlid"c1"gt ltttitlegtBasic
Languageslt/ttitlegt ltdescriptiongtIntrodu
ction to Languageslt/descriptiongt lt/coursegt
ltcourse xmlid"c2"gt ltttitlegtFrench
Ilt/ttitlegt ltdescriptiongtIntroduction
to Frenchlt/descriptiongt lt/coursegt
ltcourse xmlid"c3"gt ltttitlegtFrench
IIlt/ttitlegt ltdescriptiongtIntermediate
Frenchlt/descriptiongt ltpre-requisite
cref"c2" /gt lt?phpx A PI Node ?gt
ltdefns xmlns"urndefault"gtcontentlt/defnsgt
lt/coursegt lt/coursesgt
16
XPath Location Examplexpath/location.php
Expression
/courses/course/description //description /courses
//description //descriptionancestorcourse
Resulting Nodset
ltdescriptiongtIntroduction to Languageslt/descriptio
ngt ltdescriptiongtIntroduction to
Frenchlt/descriptiongt ltdescriptiongtIntermediate
Frenchlt/descriptiongt
17
XPath Function Examplexpath/function.php
  • string(/courses/course/pre-requisite_at_cref"c2"/.
    .)
  • French II
  • Intermediate French
  • content

18
XPath and Namespacesxpath/namespaces.php
//title Empty NodeSet //ttitle ltttitlegtBasic
Languageslt/ttitlegt ltttitlegtFrench
Ilt/ttitlegt ltttitlegtFrench IIlt/ttitlegt //defns
Empty NodeSet //local-name()"defns" ltdefns
xmlns"urndefault"gtcontentlt/defnsgt
19
PHP and XML
  • PHP 5 introduced numerous interfaces for working
    with XML
  • The libxml2 library (http//www.xmlsoft.org/) was
    chosen to provide XML support
  • The sister library libxslt provides XSLT support
  • I/O is handled via PHP streams

20
XML Entensions for PHP 5
  • ext/libxml
  • ext/xml (SAX push parser)
  • ext/dom
  • ext/simplexml
  • ext/xmlreader (pull parser)
  • ext/xmlwriter
  • ext/xsl
  • ext/wddx
  • ext/soap

21
Libxml
  • Contains common functionality shared across
    extensions.
  • Defines constants to modify parse time behavior.
  • Provides access to streams context.
  • Allows modification of error handling behavior
    for XML based extensions.

22
Libxml Parser Options
LIBXML_NOENT Substitute entities with replacement content
LIBXML_DTDLOAD Load subsets but do not perform validation
LIBXML_DTDATTR Create defaulted attributes defined in DTD
LIBXML_DTDVALID Loads subsets and perform validation
LIBXML_NOERROR Suppress parsing errors from libxml2
LIBXML_NOWARNING Suppress parser warnings from libxml2
LIBXML_NOBLANKS Remove insignificant whitespace on parsing
LIBXML_XINCLUDE Perform XIncludes during parsing
LIBXML_NOCDATA Merge CDATA nodes in Text nodes
LIBXML_NONET Disable network access when loading
23
Libxml Error Handling
  • bool libxml_use_internal_errors (bool
    use_errors)
  • void libxml_clear_errors ( void )
  • LibXMLError libxml_get_last_error ( void )
  • array libxml_get_errors ( void )

24
Libxml LibXMLError
Class LibXMLError Properties (Read-Only) (int
) level (int) code (int) column (string)
message (string) file (int)
line LibXMLErrorcode Values LIBXML_ERR_NONE
LIBXML_ERR_WARNING LIBXML_ERR_ERROR LIBXML_ERR_F
ATAL
25
LibXMLError Examplelibxml/error.php
lt?php / Regular Error Handling / dom new
DOMDocument() dom-gtloadXML('ltrootgt') / New
Error Handling / libxml_use_internal_errors(TRUE)
if (! dom-gtloadXML('root')) arrError
libxml_get_errors() foreach (arrError AS
xmlError) var_dump(xmlError)
else print "Document Loaded" ?gt
26
LibXMLError Result
  • PHP Warning DOMDocumentloadXML() Premature
    end of data in tag root line 1 in Entity, line 1
    in /home/rrichards/workshop/libxml/error.php on
    line 4
  • Warning DOMDocumentloadXML() Premature end of
    data in tag root line 1 in Entity, line 1 in
    /home/rrichards/workshop/libxml/error.php on line
    4
  • New Error Handling
  • object(LibXMLError)2 (6)
  • "level"gt int(3)
  • "code"gt int(4)
  • "column"gt int(1)
  • "message"gt string(34) "Start tag expected,
    'lt' not found"
  • "file"gt string(0) ""
  • "line"gt int(1)

27
DOM
  • Tree based parser
  • Allows for creation and editing of XML documents
  • W3C Specification with DOM Level 2/3 compliancy
  • Provides XPath support
  • Provides XInclude Support
  • Ability to work with HTML documents
  • Zero copy interoperability with SimpleXML
  • Replacement for ext/domxml from PHP 4

28
DOMNode Classes
  • DOMDocument
  • DOMElement
  • DOMAttr
  • DOMComment
  • DOMDocumentType
  • DOMNotation
  • DOMEntity
  • DOMEntityReference
  • DOMProcessingInstruction
  • DOMNameSpaceNode
  • DOMDocumentFragment
  • DOMCharacterData
  • DOMText
  • DOMCdataSection

29
Additional DOM Classes
  • DOMException
  • DOMImplementation
  • DOMNodeList
  • DOMNamedNodeMap
  • DOMXPath

30
DOM Document Navigationdom/navigate.php
  • / Find first description element in subtrees /
  • function locateDescription(nodeset)
  • foreach (nodeset AS node)
  • if (node-gtnodeType XML_ELEMENT_NODE
    node-gtnodeName 'description')
  • GLOBALS'arNodeSet' node
  • return
  • if (node-gthasChildNodes())
    locateDescription(node-gtchildNodes)
  • dom new DOMDocument()
  • dom-gtload('course.xml')
  • root dom-gtdocumentElement
  • arNodeSet array()
  • if (root-gthasChildNodes())
    locateDescription(root-gtchildNodes)

31
DOM Document Navigation Results
  • 0 Introduction to Languages
  • 1 Introduction to French
  • 2 Intermediate French

32
DOMDocument Navigation 2dom/navigate-2.php
lt?php dom new DOMDocument() dom-gtload('course
.xml') nodelist dom-gtgetElementsByTagName('d
escription') foreach (nodelist AS keygtnode)
print "key ".node-gtnodeValue."\n" ?gt
Results 0 Introduction to Languages 1
Introduction to French 2 Intermediate French
33
DOM Navigation Optimizeddom/navigate-optimized.p
hp
  • function locateDescription(node)
  • while(node)
  • if (node-gtnodeType XML_ELEMENT_NODE
    node-gtnodeName 'description')
  • GLOBALS'arNodeSet' node
  • return
  • locateDescription(node-gtfirstChild)
  • node node-gtnextSibling
  • dom new DOMDocument()
  • dom-gtload('course.xml')
  • root dom-gtdocumentElement
  • arNodeSet array()
  • locateDescription(root-gtfirstChild)
  • foreach (arNodeSet AS keygtnode)

34
DOM Creating a Simple Treedom/create_simple_tree
.php
  • doc new DOMDocument()
  • root doc-gtcreateElement("tree")
  • doc-gtappendChild(root)
  • root-gtsetAttribute("att1", "att1 value")
  • attr2 doc-gtcreateAttribute("att2")
  • attr2-gtappendChild(doc-gtcreateTextNode("att2
    value"))
  • root-gtsetAttributeNode(attr2)
  • child root-gtappendChild(doc-gtcreateElement("c
    hild"))
  • comment doc-gtcreateComment("My first
    Document")
  • doc-gtinsertBefore(comment, root)
  • pi doc-gtcreateProcessingInstruction("php",
    'echo "Hello World!"')
  • root-gtappendChild(pi)

35
DOM Simple Tree Output
lt?xml version"1.0"?gt lt!--My first
Document--gt lttree att1"att1 value" att2"att2
value"gt ltchildgtlt!CDATAspecial chars lt gt
'gtlt/childgt lt?php echo "Hello World!"?gt lt/treegt
36
DOM Creating an Atom Feeddom/atom_feed_creation.
php
  • define('ATOMNS', 'http//www.w3.org/2005/Atom')
  • feed_title "Example Atom Feed"
  • alt_url "http//www.example.org/"
  • feed "http//www.example.org/atom/"
  • doc new DOMDocument("1.0", "UTF-8")
  • function create_append_Atom_elements(doc, name,
    valueNULL, parentNULL)
  • if (value)
  • newelem doc-gtcreateElementNS(ATOMNS,
    name, value)
  • else
  • newelem doc-gtcreateElementNS(ATOMNS,
    name)
  • if (parent) return parent-gtappendChild(
    newelem)
  • feed create_append_Atom_elements(doc, 'feed',
    NULL, doc)
  • create_append_Atom_elements(doc, 'title',
    feed_title, feed)
  • create_append_Atom_elements(doc, 'subtitle',
    feed_title, feed)
  • create_append_Atom_elements(doc, 'id', alt_url,
    feed)

37
DOM Creating an Atom Feed Result (initial
structure)
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • ltfeed xmlns"http//www.w3.org/2005/Atom"gt
  • lttitlegtExample Atom Feedlt/titlegt
  • ltsubtitlegtExample Atom Feedlt/subtitlegt
  • ltidgthttp//www.example.org/lt/idgt
  • ltupdatedgt2006-03-23T013940-0500lt/updatedgt
  • lt/feedgt

38
DOM Creating an Atom Feeddom/atom_feed_creation.
php
  • entry create_append_Atom_elements(doc,
    'entry', NULL, feed)
  • title create_append_Atom_elements(doc,
    'title', 'My first entry', entry)
  • title-gtsetAttribute('type', 'text')
  • link create_append_Atom_elements(doc,
    'link', NULL, entry)
  • link-gtsetAttribute('type', 'text/html')
  • link-gtsetAttribute('rel', 'alternate')
  • link-gtsetAttribute('href', 'http//www.example
    .org/entry-url')
  • link-gtsetAttribute('title', 'My first
    entry')
  • author create_append_Atom_elements(doc,
    'author', NULL, entry)
  • create_append_Atom_elements(doc, 'name',
    'Rob', author)
  • create_append_Atom_elements(doc, 'id',
    'http//www.example.org/entry-guid', entry)
  • create_append_Atom_elements(doc, 'updated',
    date('c'), entry)
  • create_append_Atom_elements(doc, 'published',
    date('c'), entry)
  • content create_append_Atom_elements(doc,
    'content', NULL, entry)

39
DOM Creating an Atom FeedResultdom/atomoutput.x
ml
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • ltfeed xmlns"http//www.w3.org/2005/Atom"gt
  • lttitlegtExample Atom Feedlt/titlegt
  • ltsubtitlegtExample Atom Feedlt/subtitlegt
  • ltidgthttp//www.example.org/lt/idgt
  • ltupdatedgt2006-03-23T015359-0500lt/updatedgt
  • ltentrygt
  • lttitle type"text"gtMy first entrylt/titlegt
  • ltlink type"text/html" rel"alternate"
  • href"http//www.example.org/entry-ur
    l" title"My first entry"/gt
  • ltauthorgt
  • ltnamegtRoblt/namegt
  • lt/authorgt
  • ltidgthttp//www.example.org/entry-guidlt/idgt
  • ltupdatedgt2006-03-23T015359-0500lt/updatedgt
  • ltpublishedgt2006-03-23T015359-0500lt/publishe
    dgt
  • ltcontentgtlt!CDATAThis is my first Atom
    entry!ltbr /gtMore to followgtlt/contentgt
  • lt/entrygt
  • lt/feedgt

40
DOM Document Editingdom/editing.php
  • dom-gtload('atomoutput.xml')
  • child dom-gtdocumentElement-gtfirstChild
  • while(child child-gtnodeName ! "entry")
    child child-gtnextSibling
  • if (child (child child-gtfirstChild))
  • while(child child-gtnodeName ! "title")
    child child-gtnextSibling
  • if (child)
  • child-gtsetAttribute('type', 'html')
  • text child-gtfirstChild
  • text-gtnodeValue "ltemgtMy first
    entrylt/emgt"
  • while(child)
  • if (child-gtnodeName "updated")
  • text child-gtfirstChild
  • text-gtnodeValue date('c')
  • break
  • child child-gtnextSibling

41
DOM Editingdom/new_atomoutput.xml
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • ltfeed xmlns"http//www.w3.org/2005/Atom"gt
  • lttitlegtExample Atom Feedlt/titlegt
  • ltsubtitlegtExample Atom Feedlt/subtitlegt
  • ltidgthttp//www.example.org/lt/idgt
  • ltupdatedgt2006-03-23T015359-0500lt/updatedgt
  • ltentrygt
  • lttitle type"html"gtltemgtMy first
    entrylt/emgtlt/titlegt
  • ltlink type"text/html" rel"alternate"
  • href"http//www.example.org/entry-ur
    l" title"My first entry"/gt
  • ltauthorgt
  • ltnamegtRoblt/namegt
  • lt/authorgt
  • ltidgthttp//www.example.org/entry-guidlt/idgt
  • ltupdatedgt2006-03-23T022922-0500lt/updatedgt
  • ltpublishedgt2006-03-23T015359-0500lt/publishe
    dgt
  • ltcontentgtlt!CDATAThis is my first Atom
    entry!ltbr /gtMore to followgtlt/contentgt
  • lt/entrygt
  • lt/feedgt

42
DOM Document Modificationdom/modify.php
  • / Assume entry refers to the first entry
    element within the Atom document /
  • while (entry-gthasChildNodes())
  • entry-gtremoveChild(entry-gtfirstChild)
  • OR
  • node entry-gtlastChild
  • while(node)
  • prev node-gtpreviousSibling
  • entry-gtremoveChild(node)
  • node prev
  • / This Will Not Work! /
  • foreach(entry-gtchildNodes AS node)
  • entry-gtremoveChild(node)

/ These will work / children
entry-gtchildNodes length children-gtlength -
1 for (xlength x gt0 x--)
entry-gtremoveChild(children-gtitem(x)) OR
elem entry-gtcloneNode(FALSE) entry-gtparentNod
e-gtreplaceChild(elem,
entry)
43
DOM and Namespaces
  • ltxsdcomplexType
  • xmlnsxsd"http//www.w3.org/2001/XM
    LSchema"
  • xmlnswsdl"http//schemas.xmlsoap.o
    rg/wsdl/"
  • name"ArrayOfint"gt
  • ltxsdcomplexContentgt
  • ltxsdrestriction base"soapencArray"gt
  • ltxsdattribute ref"soapencarrayType"

  • wsdlarrayType"xsdint "/gt
  • lt/xsdrestrictiongt
  • lt/xsdcomplexContentgt
  • lt/xsdcomplexTypegt

44
Dom and Namepsacesdom/namespace.php
  • define("SCHEMA_NS", "http//www.w3.org/2001/XMLSch
    ema")
  • define("WSDL_NS", "http//schemas.xmlsoap.org/wsdl
    /")
  • dom new DOMDocument()
  • root dom-gtcreateElementNS(SCHEMA_NS,
    "xsdcomplexType")
  • dom-gtappendChild(root)
  • root-gtsetAttributeNS("http//www.w3.org/2000/xmln
    s/", "xmlnswsdl", WSDL_NS)
  • root-gtsetAttribute("name", "ArrayOfint")
  • content root-gtappendChild(new
    DOMElement("xsdcomplexContent", NULL,
    SCHEMA_NS))
  • restriction content-gtappendChild(new
    DOMElement("xsdrestriction", NULL, SCHEMA_NS))
  • restriction-gtsetAttribute("base",
    "soapencArray")
  • attribute restriction-gtappendChild(new
    DOMElement("xsdattribute", NULL, SCHEMA_NS))
  • attribute-gtsetAttribute("ref",
    "soapencarrayType")
  • attribute-gtsetAttributeNS(WSDL_NS,
    "wsdlarrayType", "xsdint")

45
DOM and Xpathdom/xpath/dom-xpath.xml
  • ltstoregt
  • ltbooksgt
  • ltraregt
  • ltbook qty"4"gt
  • ltnamegtCannery Rowlt/namegt
  • ltpricegt400.00lt/pricegt
  • lteditiongt1lt/editiongt
  • lt/bookgt
  • lt/raregt
  • ltclassicsgt
  • ltbook qty"25"gt
  • ltnamegtGrapes of Wrathlt/namegt
  • ltpricegt12.99lt/pricegt
  • lt/bookgt
  • ltbook qty"25"gt
  • ltnamegtOf Mice and Menlt/namegt
  • ltpricegt9.99lt/pricegt
  • lt/bookgt
  • lt/classicsgt

46
DOM and Xpathdom/xpath/dom-xpath.php
  • doc new DOMDocument()
  • doc-gtload('dom-xpath.xml')
  • xpath new DOMXPath(doc)
  • nodelist xpath-gtquery("//name")
  • print "Last Book Title ".nodelist-gtitem(nodelis
    t-gtlength - 1)-gttextContent."\n"
  • nodelist xpath-gtquery("//nameancestorrare"
    )
  • print "Last Rare Book Title ".nodelist-gtitem(no
    delist-gtlength - 1)-gtnodeValue."\n"
  • inventory xpath-gtevaluate("sum(//book/_at_qty)")
  • print "Total Books ".inventory."\n"
  • inventory xpath-gtevaluate("sum(//classics/book
    /_at_qty)")
  • print "Total Classic Books ".inventory."\n"
  • inventory xpath-gtevaluate("count(//bookparent
    classics)")
  • print "Distinct Classic Book Titles
    ".inventory."\n"

47
DOM and Xpath Results
  • / nodelist xpath-gtquery("//name")
  • nodelist-gtitem(nodelist-gtlength -
    1)-gttextContent /
  • Last Book Title Of Mice and Men
  • / xpath-gtquery("//nameancestorrare")
  • nodelist-gtitem(nodelist-gtlength - 1)-gtnodeValue
    /
  • Last Rare Book Title Cannery Row
  • / xpath-gtevaluate("sum(//book/_at_qty)") /
  • Total Books 54
  • / xpath-gtevaluate("sum(//classics/book/_at_qty)")
    /
  • Total Classic Books 50
  • / xpath-gtevaluate("count(//bookparentclassics
    )") /
  • Distinct Classic Book Titles 2

48
DOM and Xpath w/Namespaces dom/xpath/dom-xpathns.
xml
  • ltstore xmlns"http//www.example.com/store"
    xmlnsbk"http//www.example.com/book"gt
  • ltbooksgt
  • ltraregt
  • ltbkbook qty"4"gt
  • ltbknamegtCannery Rowlt/bknamegt
  • ltbkpricegt400.00lt/bkpricegt
  • ltbkeditiongt1lt/bkeditiongt
  • lt/bkbookgt
  • lt/raregt
  • ltclassicsgt
  • ltbkbook qty"25"gt
  • ltbknamegtGrapes of Wrathlt/bknamegt
  • ltbkpricegt12.99lt/bkpricegt
  • lt/bkbookgt
  • ltbkbook qty"25" xmlnsbk"http//www.ex
    ample.com/classicbook"gt
  • ltbknamegtOf Mice and Menlt/bknamegt
  • ltbkpricegt9.99lt/bkpricegt
  • lt/bkbookgt
  • lt/classicsgt

49
DOM and Xpath w/Namespacesdom/xpath/dom-xpathns.p
hp
  • nodelist xpath-gtquery("//name")
  • print "Last Book Title ".nodelist-gtitem(nodelis
    t-gtlength - 1)-gttextContent."\n"
  • // Last Book Title / Why empty? /
  • nodelist xpath-gtquery("//bkname")
  • print "Last Book Title ".nodelist-gtitem(nodelis
    t-gtlength - 1)-gttextContent."\n"
  • // Last Book Title Grapes of Wrath / Why not
    "Of Mice and Men" /
  • nodelist xpath-gtquery("//bknameancestorrar
    e")
  • print "Last Rare Book Title ".nodelist-gtitem(no
    delist-gtlength - 1)-gtnodeValue."\n"
  • // Last Rare Book Title / Why empty? /
  • xpath-gtregisterNamespace("rt",
    "http//www.example.com/store")
  • nodelist xpath-gtquery("//bknameancestorrt
    rare")
  • print "Last Rare Book Title ".nodelist-gtitem(no
    delist-gtlength - 1)-gtnodeValue."\n"
  • // Last Rare Book Title Cannery Row
  • xpath-gtregisterNamespace("ext",
    "http//www.example.com/ExteralClassics")
  • nodelist xpath-gtquery("(//bkname)
    (//extname)")

50
DOM and Xpath w/Namespacesdom/xpath/dom-xpathns.p
hp
  • xpath-gtregisterNamespace("bk2",
    "http//www.example.com/classicbook")
  • nodelist xpath-gtquery("//bk2name")
  • print "Last Book Title (bk2) "
  • print nodelist-gtitem(nodelist-gtlength -
    1)-gttextContent."\n"
  • // Last Book Title (bk2) Of Mice and Men
  • Complete Results
  • Last Book Title
  • Last Book Title Grapes of Wrath
  • Last Rare Book Title
  • Last Rare Book Title Cannery Row
  • Last Book Title To Kill a Mockingbird
  • Last Book Title (bk2) Of Mice and Men

51
Performing Validationdom/validation/validate.php
  • doc new DOMDocument()
  • print "DTD Validation\n"
  • doc-gtload('courses-dtd.xml', LIBXML_DTDVALID)
  • / No errors means document is valid /
  • if (doc-gtvalidate()) print " Document Is
    Valid\n"
  • print "DTD Validation FAILURE\n"
  • doc-gtload('course-id.xml')
  • if (doc-gtvalidate()) print " Document Is
    Valid\n"
  • doc-gtload('course.xml')
  • print "\nXML Schema Validation\n"
  • if (doc-gtschemaValidate('course.xsd'))
    print " Document is valid\n"
  • doc-gtload('course.xml')
  • print "\nRelaxNG Validation\n"
  • if (doc-gtrelaxNGValidate('course.rng'))
    print " Document is valid\n"

52
Performing Validation Results
  • DTD Validation
  • Document Is Valid
  • DTD Validation FAILURE
  • Warning DOMDocumentvalidate() No declaration
    for element courses in /home/rrichards/workshop/do
    m/validation/validate.php on line 11
  • Warning DOMDocumentvalidate() No declaration
    for element course in /home/rrichards/workshop/dom
    /validation/validate.php on line 11
  • Warning DOMDocumentvalidate() No declaration
    for element title in /home/rrichards/workshop/dom/
    validation/validate.php on line 11
  • . . .
  • XML Schema Validation
  • Document is valid
  • RelaxNG Validation
  • Document is valid

53
Extending DOM Classes
  • Overriding the constructor requires the parent
    constructor to be called.
  • Properties built into the DOM classes cannot be
    overridden.
  • Methods built into the DOM classes may can be
    overridden.
  • The lifespan of an extended object is that of the
    object itself.

54
Extending DOM Classes dom/extending/extending.php
  • class customElement extends DOMElement
  • class customDoc extends DOMDocument
  • public nodeName "customDoc"
  • function __construct(rootName)
  • parent__construct()
  • if (! empty(rootName))
  • element this-gtappendChild(new
    DOMElement(rootName))
  • function createElement(name, value,
    parentNULL)
  • custom new customElement(name, value)
  • if (parent (parent instanceof
    DOMElement))
  • parent-gtappendChild(custom)
  • return custom

55
DOM Object Scopedom/extending/object_scope.php
  • class customElement extends DOMElement
  • function changeit(doc)
  • myelement new customElement("custom",
    "element2")
  • doc-gtreplaceChild(myelement,
    doc-gtdocumentElement)
  • print "Within changeit function
    ".get_class(doc-gtdocumentElement)."\n"
  • doc new DOMDocument()
  • myelement doc-gtappendChild(new
    customElement("custom", "element"))
  • print "After Append ".get_class(myelement)."\n"
  • unset(myelement)
  • print "After unset ".get_class(doc-gtdocumentElem
    ent)."\n"
  • changeit(doc)
  • print "Outside changeit() ".get_class(doc-gtdocum
    entElement)."\n"

56
DOMCommon Issues
  • DOM Objects and Sessions
  • Removing Nodes while iterating a Nodeset skips
    nodes
  • XML Tree contains garbled characters
  • Extended class is not returned from property or
    method
  • Elements not being returned by ID
  • Entity errors are issues when loading a document
  • New DTD is not recognized by document

57
SimpleXML
  • Provides simple access to XML documents
  • Operates only on elements and attributes
  • Contains XPath support
  • Allows for modifications to the XML
  • Zero copy interoperability with DOM
  • New in PHP 5.1.3
  • Elements and attributes can be added using
    addChild() and addAttribute() methods.
  • Node names can be retrieved by calling getName().

58
SimpleXML Consuming Yahoo WebSearch
simplexml/yahoo_rest_results.xml
  • ltResultSet xmlnsxsi"http//www.w3.org/2001/XMLSc
    hema-instance"
  • xmlns"urnyahoosrch"
  • xsischemaLocation"urnyahoosrc
    h
  • http//api.search.yahoo.com/WebSearchService/V1/W
    ebSearchResponse.xsd"
  • totalResultsAvailable"374000"
    totalResultsReturned"5" firstResultPosition"1"gt
  • ltResultgt
  • ltTitlegtZend Technologies - PHP 5 In Depth -
    XML in PHP 5 - What's New?lt/Titlegt
  • ltSummarygtXML in PHP 5 - What's New? By
    Christian Stocker. March . . . lt/Summarygt
  • ltUrlgthttp//www.zend.com/php5/articles/php5-
    xmlphp.phplt/Urlgt
  • ltClickUrlgthttp//uk.wrs.yahoo.com/_ylt. .
    .lt/ClickUrlgt
  • ltModificationDategt1143014400lt/ModificationDa
    tegt
  • ltMimeTypegttext/htmllt/MimeTypegt
  • ltCachegt
  • ltUrlgthttp//uk.wrs.yahoo.com/...lt/Urlgt
  • ltSizegt112625lt/Sizegt
  • lt/Cachegt
  • lt/Resultgt
  • . . .
  • lt/Resultsgt

59
SimpleXML Consuming Yahoo WebSearch
simplexml/reading_rest.php
  • / URL to Web Search service /
  • url 'http//api.search.yahoo.com/WebSearchServi
    ce/V1/webSearch'
  • / The query is separate here as the terms must
    be encoded. /
  • url . '?query'.rawurlencode('php5 xml')
  • / Complete the URL adding App ID, limit to 5
    results and only English results /
  • url . "appidzzzresults5languageen"
  • sxe simplexml_load_file(url)
  • / Check for number of results returned /
  • if ((int)sxe'totalResultsReturned' gt 0)
  • / Loop through each result and output title,
    url and modification date /
  • foreach (sxe-gtResult AS result)
  • print 'Title '.result-gtTitle."\n"
  • print 'Url '.result-gtUrl."\n"
  • print 'Mod Date '.date ('M d Y',
    (int)result-gtModificationDate)."\n\n"

60
SimpleXML Consuming Yahoo WebSearch RESULTS
  • Title Zend Technologies - PHP 5 In Depth - XML
    in PHP 5 - What's New?
  • Url http//www.zend.com/php5/articles/php5-xmlphp
    .php
  • Mod Date Mar 22 2006
  • Title ONLamp.com -- Using PHP 5's SimpleXML
  • Url http//www.onlamp.com/pub/a/php/2004/01/15/si
    mplexml.html
  • Mod Date Mar 19 2006
  • Title Zend Technologies - PHP 5 In Depth -
    SimpleXML
  • Url http//www.zend.com/php5/articles/php5-simple
    xml.php
  • Mod Date Mar 22 2006
  • Title Workshop XML in PHP5
  • Url http//php5.bitflux.org/phpconf2004
  • Mod Date Mar 05 2005
  • Title PHP Hypertext Preprocessor
  • Url http//www.php.net/
  • Mod Date Mar 13 2006

61
SimpleXML Namespacessimplexml/simplexml-namespac
e.php
  • store simplexml_load_file('simplexml-xpathns.xm
    l')
  • books store-gtbooks
  • foreach (books-gtclassics AS classic)
  • if (classic-gtbook)
  • print classic-gtbook-gtname."\n\n"
  • / Why only one result? To Kill a Mockingbird /
  • x 0
  • foreach (books-gtclassics AS classics)
  • if (x 0)
  • children classics-gtchildren("http//
    www.example.com/classicbook")
  • / Print name for the books where book
    element resides in a prefixed namespace /
  • print classics-gtchildren("http//www.ex
    ample.com/book")-gtbook-gtname."\n"
  • print children-gtbook-gtname."\n"
  • else
  • print classic-gtbook-gtname."\n"

62
SimpleXML Namespaces Results
  • To Kill a Mockingbird
  • Grapes of Wrath
  • Of Mice and Men
  • To Kill a Mockingbird

63
SimpleXML Xpathsimplexml/simplexml-xpathns.php
  • sxe simplexml_load_file('simplexml-xpathns.xml'
    )
  • nodelist sxe-gtxpath("//bkname")
  • print "Last Book Title ".nodelistcount(nodelis
    t) - 1."\n"
  • sxe-gtregisterXPathNamespace("rt",
    "http//www.example.com/store")
  • nodelist sxe-gtxpath("//bknameancestorrtra
    re")
  • print "Last Rare Book Title ".nodelistcount(no
    delist) - 1."\n"
  • sxe-gtregisterXPathNamespace("ext",
    "http//www.example.com/ExteralClassics")
  • nodelist sxe-gtxpath("(//bkname)
    (//extname)")
  • print "Last Book Title ".nodelistcount(nodelis
    t) - 1."\n"
  • sxe-gtregisterXPathNamespace("bk2",
    "http//www.example.com/classicbook")
  • nodelist sxe-gtxpath("//bk2name")
  • print "Last Book Title (bk2) ".nodelistcount(n
    odelist) - 1."\n"

64
SimpleXML XPath Results
  • Last Book Title Grapes of Wrath
  • Last Rare Book Title Cannery Row
  • Last Book Title To Kill a Mockingbird
  • Last Book Title (bk2) Of Mice and Men

65
SimpleXML Advanced Editingsimplexml/editing.php
  • data array(array('title'gt'Result 1',
    'descript'gt'Res1 description'),
  • array('title'gt'Result 2',
    'descript'gt'description of Res2'),
  • array('title'gt'Result 3',
    'descript'gt'This is result 3'))
  • class webservice extends simpleXMLElement
  • public function appendElement(name,
    valueNULL)
  • node dom_import_simplexml(this)
  • newnode value ? new DOMElement(name,
    value) new DOMElement(name)
  • node-gtappendChild(newnode)
  • return simplexml_import_dom(newnode,
    'webservice')
  • rest simplexml_load_string('ltresults num"0"
    /gt', 'webservice')
  • rest'num' count(data)
  • foreach (data AS result_item)
  • result rest-gtappendElement('result')
  • result-gtappendElement('title',
    result_item'title')
  • result-gtappendElement('description')

66
SimpleXML Advanced Editing Results
  • lt?xml version"1.0"?gt
  • ltresults num"3"gt
  • ltresultgt
  • lttitlegtResult 1lt/titlegt
  • ltdescriptiongtRes1 descriptionlt/descripti
    ongt
  • lt/resultgt
  • ltresultgt
  • lttitlegtResult 2lt/titlegt
  • ltdescriptiongtdescription of
    Res2lt/descriptiongt
  • lt/resultgt
  • ltresultgt
  • lttitlegtResult 3lt/titlegt
  • ltdescriptiongtThis is result
    3lt/descriptiongt
  • lt/resultgt
  • lt/resultsgt

67
SimpleXML Advanced Editing PHP
5.1.3simplexml/editing_php513.php
  • data array(array('title'gt'Result 1',
    'descript'gt'Res1 description'),
  • array('title'gt'Result 2',
    'descript'gt'description of Res2'),
  • array('title'gt'Result 3',
    'descript'gt'This is result 3'))
  • rest simplexml_load_string('ltresults num"0"
    /gt')
  • rest'num' count(data)
  • foreach (data AS result_item)
  • result rest-gtaddChild('result')
  • result-gtaddChild('title', result_item'title'
    )
  • result-gtaddChild('description')
  • result-gtdescription result_item'descript'
  • rest-gtasXML('editing_php513.xml')

68
SimpleXML Removing data remove_data.php
  • lt?php
  • results simplexml_load_file('editing_php513.xml
    ')
  • / Delete title from first result element /
  • unset(results-gtresult-gttitle)
  • / Delete the 2nd result element - ONLY WORKS in
    PHP 5.1.3 /
  • unset(results-gtresult1)
  • print results-gtasXML()
  • ?gt

69
SimpleXML Removing data RESULTS
  • lt?xml version"1.0"?gt
  • ltresults num"3"gt
  • ltresultgt
  • ltdescriptiongtRes1 descriptionlt/descriptiongt
  • lt/resultgt
  • ltresultgt
  • lttitlegtResult 3lt/titlegt
  • ltdescriptiongtThis is result 3lt/descriptiongt
  • lt/resultgt
  • lt/resultsgt

70
Simple API for XML (SAX)
  • Event based push parser
  • Low memory usage
  • Works using function callbacks
  • Almost completely compatible with ext/xml from
    PHP 4
  • Default encoding is UTF-8 rather than
    ISO-8859-1 as it was in PHP 4

71
SAX Source Document xml/xml_simple.xml
  • lt?xml version'1.0'?gt
  • ltchapter xmlnsa"http//www.example.com/namespace
    -a"
  • xmlns"http//www.example.com/defau
    lt"gt
  • ltatitlegtext/xmllt/atitlegt
  • ltparagt
  • First Paragraph
  • lt/paragt
  • ltasection aid"about"gt
  • lttitlegtAbout this Documentlt/titlegt
  • ltparagt
  • lt!-- this is a comment --gt
  • lt?php echo 'Hi! This is PHP version ' .
    phpversion() ?gt
  • lt/paragt
  • lt/asectiongt
  • lt/chaptergt

72
SAX Simple Example xml/xml_simple.php
  • lt?php
  • function startElement(parser, elementname,
    attributes)
  • print " Start Element elementname \n"
  • foreach (attributes as attname gt attvalue)
  • print " attname gt attvalue \n"
  • function endElement(parser, elementname)
  • print " End Element elementname\n"
  • function charDataHandler(parser,data)
  • if (trim(data) ! "") print data."\n"
  • function PIhandler (parser, target, data)
  • print "PI target -gt data\n"

73
SAX Simple Example xml/xml_simple.php
  • parser xml_parser_create()
  • / Disable as case is significant in XML /
  • xml_parser_set_option(parser, XML_OPTION_CASE_FOL
    DING, false)
  • xml_set_element_handler(parser,"startElement","en
    dElement")
  • xml_set_character_data_handler(parser,
    "charDataHandler")
  • xml_set_processing_instruction_handler (parser,
    "PIhandler")
  • xml_set_default_handler (parser,
    "DefaultHandler")
  • if ((fp fopen("xml_simple.xml", "r")))
  • while (data fread(fp, 4096))
  • xml_parse(parser, data, feof(fp))
  • ?gt

74
SAX Simple Example RESULTS
  • Start Element chapter
  • xmlnsa gt http//www.example.com/namespace-a
  • xmlns gt http//www.example.com/default
  • Start Element atitle
  • ext/xml
  • End Element atitle
  • Start Element para
  • First Paragraph
  • End Element para
  • Start Element asection
  • aid gt about
  • Start Element title
  • About this Document
  • End Element title
  • Start Element para
  • Default lt!-- this is a comment --gt
  • PI php -gt echo 'Hi! This is PHP version ' .
    phpversion()
  • End Element para

75
SAX Error Handling xml/xml_error.php
  • lt?php
  • / Malformed document /
  • data "ltrootgt"
  • parser xml_parser_create()
  • if(! xml_parse(parser, data, TRUE))
  • / Normally die is or some other escape
    mechanism is also called/
  • printf("XML error s in line d, column
    d\n\n",
  • xml_error_string(xml_get_error_code(par
    ser)),
  • xml_get_current_line_number(parser),
  • xml_get_current_column_number(parser))
  • / Magically you can also get a structured error
    /
  • xmlError libxml_get_last_error()
  • var_dump(xmlError)
  • ?gt

76
SAX Error Handling RESULTS
  • XML error Invalid document end in line 1, column
    7
  • object(LibXMLError)1 (6)
  • "level"gt
  • int(3)
  • "code"gt
  • int(5)
  • "column"gt
  • int(7)
  • "message"gt
  • string(41) "Extra content at the end of the
    document
  • "
  • "file"gt
  • string(0) ""
  • "line"gt
  • int(1)

77
SAX Advanced Example xml/xml_advanced.php
  • class cSax
  • function startElement(parser, elementname,
    attributes)
  • list(namespaceURI,localName)
    split("_at_",elementname)
  • if (! localName)
  • localName namespaceURI
  • namespaceURI ""
  • print " Start Element localName".
  • (namespaceURI ? " in
    namespaceURI" "")."\n"
  • foreach (attributes as attname gt
    attvalue)
  • print " attname gt attvalue \n"
  • function endElement(parser, elementname)
  • list(namespaceURI,localName)
    split("_at_",elementname)
  • if (! localName)
  • localName namespaceURI
  • namespaceURI ""

78
SAX Advanced Example xml/xml_advanced.php
  • objcSax new cSax()
  • parser xml_parser_create_ns("ISO-8859-1","_at_")
  • / Disable as case is significant in XML /
  • xml_parser_set_option(parser, XML_OPTION_CASE_FOL
    DING,
  • false)
  • xml_set_object(parser, objcSax)
  • xml_set_element_handler(parser,"startElement","en
    dElement")
  • if ((fp fopen("xml_simple.xml", "r")))
  • while (data fread(fp, 4096))
  • if (! xml_parse(parser, data,
    feof(fp)))
  • xmlError libxml_get_last_error()
  • var_dump(xmlError)
  • exit

79
SAX Advanced Example RESULTS
  • Start Element chapter in http//www.example.com
    /default
  • Start Element title in http//www.example.com/n
    amespace-a
  • End Element title in http//www.example.com/nam
    espace-a
  • Start Element para in http//www.example.com/de
    fault
  • End Element para in http//www.example.com/defa
    ult
  • Start Element section in http//www.example.com
    /namespace-a
  • http//www.example.com/namespace-a_at_id gt about
  • Start Element title in http//www.example.com/d
    efault
  • End Element title in http//www.example.com/def
    ault
  • Start Element para in http//www.example.com/de
    fault
  • End Element para in http//www.example.com/defa
    ult
  • End Element section in http//www.example.com/n
    amespace-a
  • End Element chapter in http//www.example.com/d
    efault

80
XMLReader
  • Forward moving stream based parser
  • It is a Pull parser
  • Based on the C XmlTextReader API
  • Advantages
  • Low memory footprint
  • Namespace support
  • Simple API
  • Validation support
  • Advanced Feature Set
  • Faster Processing

81
XMLReader Simple Examplexmlreader/reader_simple.
xml
  • lt?xml version'1.0'?gt
  • ltchapter xmlnsa"http//www.example.com/namespace
    -a"
  • xmlns"http//www.example.com/defau
    lt"gt
  • ltatitlegtXMLReaderlt/atitlegt
  • ltparagt
  • First Paragraph
  • lt/paragt
  • ltasection aid"about"gt
  • lttitlegtAbout this Documentlt/titlegt
  • ltparagt
  • lt!-- this is a comment --gt
  • lt?php echo 'Hi! This is PHP version ' .
    phpversion() ?gt
  • lt/paragt
  • lt/asectiongt
  • lt/chaptergt

82
XMLReader Simple Examplexmlreader/reader_simple.
php
  • reader new XMLReader()
  • reader-gtopen('reader_simple.xml')
  • reader-gtread()
  • print "xmlns Attribute value ".reader-gtgetAttrib
    uteNo(0)."\n\n"
  • while (reader-gtread() reader-gtname !
    "atitle")
  • print "Local Name for Element ".reader-gtlocalNam
    e."\n"
  • print "Namespace URI for Element
    ".reader-gtnamespaceURI."\n"
  • while(reader-gtread())
  • switch (reader-gtnodeType)
  • case XMLReaderELEMENT
  • print "Element ".reader-gtname."\n"
  • if (reader-gthasAttributes
    reader-gtmoveToFirstAttribute())
  • do
  • print " ".reader-gtname."".
    reader-gtvalue."\n"
  • while(reader-gtmoveToNextAttribut
    e())
  • break

83
XMLReader Simple ExampleRESULTS
  • Local Name for Element title
  • Namespace URI for Element http//www.example.co
    m/namespace-a
  • Element para
  • Element asection
  • aidabout
  • Element title
  • Element para
  • PI Target php
  • PI Data echo 'Hi! This is PHP version ' .
    phpversion()

84
XMLReader Consuming Yahoo Shopping
  • lt?xml version"1.0" encoding"ISO-8859-1"?gt
  • ltResultSet xmlnsxsi"http//www.w3.org/2001/XMLSc
    hema-instance" xmlns"urnyahooprods"
  • xsischemaLocation"urnyahooprods
    http//api.shopping.yahoo.com/shoppingservice/v1/p
    roductsearch.xsd"
  • totalResultsAvailable"8850"
    firstResultPosition"2" totalResultsReturned"2"gt
  • ltResultgt
  • ltCatalog ID"1991433722"gt
  • ltUrlgtlt!CDATAhttp//shopping.yahoo.com/pLi
    nksys. . .2gtlt/Urlgt
  • ltProductNamegtlt!CDATALinksys WRT5. . .r
    Broadband Routergtlt/ProductNamegt
  • ltPriceFromgt59.99lt/PriceFromgt
  • ltPriceTogt100.00lt/PriceTogt
  • ltThumbnail /gtlt!-- child elements Url
    (CDATA), Height, Width --gt
  • ltDescriptiongtlt!CDATAThe Wireless-G . .
    .ces.gtlt/Descriptiongt
  • ltSummarygtlt!CDATAIEEE 802.3,
    ...gtlt/Summarygt
  • ltUserRating /gtlt!-- Rating sub elements --gt
  • ltSpecificationList /gtlt!-- 0 Specification
    child elements --gt
  • lt/SpecificationListgt
  • lt/Cataloggt
  • lt/Resultgt
  • lt/ResultSetgt

85
XMLReader Consuming Yahoo Shoppingxmlreader/rest
_yahoo_shopping.php
  • function getTextValue(reader) ...
  • function processCatalog(reader) ...
  • function processResult(reader) ...
  • / URL to Product Search service /
  • url 'http//api.shopping.yahoo.com/ShoppingServ
    ice/V1/productSearch'
  • / The query is separate here as the terms must
    be encoded. /
  • url . '?query'.rawurlencode('linksys')
  • / Complete the URL with App ID, limit to 1
    result and start at second record /
  • url . "appidzzzresults2start2"
  • reader new XMLReader()
  • if (! reader-gtopen(url)) print "Cannot access
    Webservice\n" exit
  • while(reader-gtname ! "Result")
    reader-gtread()
  • do

86
XMLReader Consuming Yahoo Shoppingxmlreader/rest
_yahoo_shopping.php
  • function getTextValue(reader)
  • if (reader-gtnodeType ! XMLReaderELEMENT
    reader-gtisEmptyElement
  • (reader-gtread()
    reader-gtnodeType XMLReaderEND_ELEMENT))
  • return
  • retVal reader-gtvalue
  • reader-gtread()
  • return retVal
  • function processResult(reader)
  • depth reader-gtdepth
  • if (reader-gtisEmptyElement
    (reader-gtread()
  • reader-gtnodeType
    XMLReaderEND_ELEMENT))
  • return
  • while(depth lt reader-gtdepth
    reader-gtname ! "Catalog") reader-gtread()
  • processCatalog(reader)
  • / Read until lt/Resultgt is encountered /
  • while(depth lt reader-gtdepth)
    reader-gtread()

87
XMLReader Consuming Yahoo Shoppingxmlreader/rest
_yahoo_shopping.php
  • function processCatalog(reader)
  • depth reader-gtdepth
  • print "Catalog ID".reader-gtgetAttribute('ID')
    ."\n"
  • if (reader-gtisEmptyElement
    (reader-gtread()
  • reader-gtnodeType
    XMLReaderEND_ELEMENT))
  • return
  • while(depth lt reader-gtdepth)
  • switch (reader-gtname)
  • case "ProductName"
  • case "PriceFrom"
  • case "PriceTo"
  • case "Description"
  • case "Url"
  • print reader-gtname."
    ".getTextValue(reader)."\n"
  • reader-gtnext()

88
XMLReader Consuming Yahoo ShoppingRESULTS
(Abbreviated)
  • Catalog ID1990338714
  • Url http//shopping.yahoo.com/pLinksys20Instant
    20Broadband20EtherFast20Cable2FDSL20Router19
    90338714
  • ProductName Linksys Instant Broadband EtherFast
    Cable/DSL Router
  • PriceFrom 39.99
  • PriceTo 72.71
  • Description ltPgtLinksys, a provider of networking
    hardware for the small/medium business (SMB),
    small office/home office (SOHO), and enterprise
    markets and broadband networking hardware for the
    home, has announced the new EtherFast Cable/DSL
    Router. The first in the new Instant Broadband
    series, this Linksys broadband router will enable
    home or office users to connect their computers
    to a cable or DSL modem and securely share
    Internet access and perform networking tasks such
    as file and printer sharing. The built-in
    hardware firewall gives users the security of
    sharing files without fear of intruders hacking
    into the network. lt/Pgt

89
XMLReader DTD Validationxmlreader/validation/rea
der.xml
  • lt!DOCTYPE chapter
  • lt!ELEMENT chapter (title, para, section)gt
  • lt!ELEMENT title (PCDATA)gt
  • lt!ELEMENT para ANYgt
  • lt!ATTLIST para name CDATA "default"gt
  • lt!ELEMENT section (PCDATA)gt
  • lt!ATTLIST section id ID REQUIREDgt
  • gt
  • ltchaptergt
  • lttitlegtXMLReaderlt/titlegt
  • ltparagt
  • First Paragraph
  • lt/paragt
  • ltsection id"about"gt
  • lttitlegtAbout this Documentlt/titlegt
  • ltparagtcontentlt/paragt
  • lt/sectiongt
  • lt/chaptergt

90
XMLReader DTD Validationxmlreader/validation/rea
der.php
  • objReader XMLReaderopen('reader.xml')
  • objReader-gtsetParserProperty(XMLReaderVALIDATE,
    TRUE)
  • libxml_use_internal_errors(TRUE)
  • while (objReader-gtread())
  • if (! objReader-gtisValid())
  • print "NOT VALID\n"
  • break
  • arErrors libxml_get_errors()
  • foreach (arErrors AS xmlError)
  • print xmlError-gtmessage

91
XMLReader DTD ValidationRESULTS
  • NOT VALID
  • Element section was declared PCDATA but contains
    non text nodes

92
XMLReader Relax NG Validationxmlreader/validatio
n/reader.rng
  • lt?xml version"1.0" encoding"utf-8" ?gt
  • ltelement name"chapter"
  • xmlns"http//relaxng.org/ns/struc
    ture/1.0"gt
  • ltelement name"title"gt
  • lttext/gt
  • lt/elementgt
  • ltelement name"para"gt
  • lttext/gt
  • lt/elementgt
  • ltelement name"section"gt
  • ltattribute name"id" /gt
  • lttext/gt
  • lt/elementgt
  • lt/elementgt

93
XMLReader Relax NG Validationxmlreader/validatio
n/reader-rng.php
  • objReader XMLReaderopen('reader.xml')
  • objReader-gtsetRelaxNGSchema('reader.rng')
  • libxml_use_internal_errors(TRUE)
  • while (objReader-gtread())
  • if (! objReader-gtisValid())
  • print "NOT VALID\n"
  • break
  • arErrors libxml_get_errors()
  • foreach (arErrors AS xmlError)
  • print xmlError-gtmessage

94
XMLReader Relax NG Validation RESULTS
  • NOT VALID
  • Did not expect element title there

95
XSL
  • Used to transform XML data
  • XSLT based on XPath
  • Works with DOM and SimpleXML, although the DOM
    extension is required.
  • Provides the capability of calling PHP functions
    during a transformation
  • DOM nodes may be returned from PHP functions
  • The LIBXML_NOCDATA and LIBXML_NOENT constants are
    your friends.
  • libxslt 1.1.5 is recommended to avoid problems
    when using xslkey

96
XSL XML Input Data xsl/sites.xml
  • lt?xml version"1.0"?gt
  • ltsitesgt
  • ltsite xmlid"php-gen"gt
  • ltnamegtPHP Generallt/namegt
  • lturlgthttp//news.php.net/group.php?groupphp
    .generalampformatrsslt/urlgt
  • lt/sitegt
  • ltsite xmlid"php-pear"gt
  • ltnamegtPHP Pear Devlt/namegt
  • lturlgthttp//news.php.net/group.php?groupphp
    .pear.devampformatrsslt/urlgt
  • lt/sitegt
  • ltsite xmlid"php-planet"gt
  • ltnamegtPlanet PHPlt/namegt
  • lturlgthttp//www.planet-php.org/rss/lt/urlgt
  • lt/sitegt
  • lt/sitesgt

97
XSL Simple Transformationxsl/simple_stylesheet.x
sl
lt?xml version"1.0"?gt ltxslstylesheet
xmlnsxsl"http//www.w3.org/1999/XSL/Transform"
version"1.0"gt ltxsloutput method"html"/gt
ltxsltemplate match"/"gt lthtmlgt ltbodygt
ltxslapply-templates select"/sites/site"/gt
lt/bodygt lt/htmlgt lt/xsltemplategt
ltxsltemplate match"/sites/site"gt
ltpgtltxslvalue-of select"./name"/gt
ltxslvalue-of select"./url"

disable-output-escaping"yes"/gtlt/pgt
lt/xsltemplategt lt/xslstylesheetgt
98
XSL Simple Transformationxsl/simple_stylesheet.p
hp
/ Load Stylesheet / stylesheet new
DOMDocument() stylesheet-gtload('simple_styleshee
t.xsl') / Create XSL Processor / proc new
xsltprocessor() proc-gtimportStylesheet(styleshe
et) / Load XML Data / dom new
DOMDocument() dom-gtload('sites.xml') print
proc-gttransformToXML(dom)
99
XSL Simple TransformationRESULTS
lthtmlgt ltbodygt ltpgtPHP General
http//news.php.net/group.php?groupphp.generalfo
rmatrsslt/pgt ltpgtPHP Pear Dev
http//news.php.net/group.php?groupphp.pear.devf
ormatrsslt/pgt ltpgtPlanet PHP
http//www.planet-php.org/rss/lt/pgt
lt/bodygt lt/htmlgt
Slide 1
Write a Comment
User Comments (0)
About PowerShow.com