Title: Advanced XML and Web Services in PHP
1Advanced XML and Web Services in PHP
- March 29, 2006
- Robert Richards
2Agenda
- Introduction to Terms and Concepts
- Libxml
- DOM
- SimpleXML
- SAX (ext/xml)
- XMLReader
- XSL
- XMLWriter
- SOAP (ext/soap)
3XML 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.
4XML 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
5XML 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
6Illegal 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
7Illegal 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
8Reserved 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.
9Schemas 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
10Document 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
11DTD 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
12XML 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
13RelaxNGvalidation/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
14XPath
- 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
15XPath 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
16XPath 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
17XPath Function Examplexpath/function.php
- string(/courses/course/pre-requisite_at_cref"c2"/.
.) - French II
- Intermediate French
-
-
- content
18XPath 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
19PHP 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
20XML 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
21Libxml
- 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.
22Libxml 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
23Libxml 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 )
24Libxml 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
25LibXMLError 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
26LibXMLError 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)
27DOM
- 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
28DOMNode Classes
- DOMDocument
- DOMElement
- DOMAttr
- DOMComment
- DOMDocumentType
- DOMNotation
- DOMEntity
- DOMEntityReference
- DOMProcessingInstruction
- DOMNameSpaceNode
- DOMDocumentFragment
- DOMCharacterData
- DOMText
- DOMCdataSection
29Additional DOM Classes
- DOMException
- DOMImplementation
- DOMNodeList
- DOMNamedNodeMap
- DOMXPath
30DOM 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)
31DOM Document Navigation Results
- 0 Introduction to Languages
- 1 Introduction to French
- 2 Intermediate French
32DOMDocument 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
33DOM 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)
34DOM 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)
35DOM 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
36DOM 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)
37DOM 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
38DOM 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)
39DOM 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
40DOM 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
41DOM 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
42DOM 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)
43DOM 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
44Dom 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")
45DOM 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
46DOM 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"
47DOM 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
48DOM 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
49DOM 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)")
50DOM 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
51Performing 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"
52Performing 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
53Extending 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.
54Extending 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
-
-
55DOM 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"
56DOMCommon 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
57SimpleXML
- 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().
58SimpleXML 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
59SimpleXML 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" -
60SimpleXML 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
61SimpleXML 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"
62SimpleXML Namespaces Results
- To Kill a Mockingbird
- Grapes of Wrath
- Of Mice and Men
- To Kill a Mockingbird
63SimpleXML 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"
64SimpleXML 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
65SimpleXML 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')
66SimpleXML 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
67SimpleXML 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')
68SimpleXML 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
69SimpleXML 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
70Simple 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
71SAX 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
72SAX 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"
-
73SAX 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
74SAX 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
75SAX 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
76SAX 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)
77SAX 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 ""
78SAX 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
-
79SAX 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
80XMLReader
- 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
81XMLReader 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
82XMLReader 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
83XMLReader 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()
84XMLReader 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
85XMLReader 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
86XMLReader 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()
87XMLReader 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()
-
88XMLReader 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
89XMLReader 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
90XMLReader 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
91XMLReader DTD ValidationRESULTS
- NOT VALID
- Element section was declared PCDATA but contains
non text nodes
92XMLReader 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
93XMLReader 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
94XMLReader Relax NG Validation RESULTS
- NOT VALID
- Did not expect element title there
95XSL
- 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
96XSL 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
97XSL 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
98XSL 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)
99XSL 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