Advanced XML with PHP 5 - CDATA XML with PHP 5 March 14, 2007 ... XML Namespaces An XML Namespace is a collection of names ... XML Schema RelaxNG

  • View
    216

  • Download
    2

Embed Size (px)

Text of Advanced XML with PHP 5 - CDATA XML with PHP 5 March 14, 2007 ... XML Namespaces An XML Namespace...

  • Advanced XML Advanced XML with PHP 5with PHP 5

    March 14, 2007March 14, 2007Robert RichardsRobert Richards

    rrichards@ctindustries.netrrichards@ctindustries.nethttp://www.cdatazone.org/talks/quebec_2007/workshop.ziphttp://www.cdatazone.org/talks/quebec_2007/workshop.zip

  • AgendaAgenda Introduction to Terms and ConceptsIntroduction to Terms and Concepts LibxmlLibxml SimpleXMLSimpleXML SDOSDO DOMDOM XMLReaderXMLReader XMLWriterXMLWriter XSLXSL

  • XML NamespacesXML Namespaces

    An XML Namespace is a collection of names An XML Namespace is a collection of names identified by a URI.identified by a URI.

    They are applicable to elements and attributes.They are applicable to elements and attributes. Namespaces may or may not be associated with a Namespaces may or may not be associated with a

    prefix.prefix. xmlns:rob="urn:rob"xmlns:rob="urn:rob" xmlns=xmlns=http://www.example.com/robhttp://www.example.com/rob

    Attributes never reside within a default namespace.Attributes never reside within a default namespace. It is illegal to have two attributes with the same It is illegal to have two attributes with the same

    localname and same namespace on the same element.localname and same namespace on the same element.

  • XML Namespace ExampleXML Namespace Example

    John Smith 123 Here Jane Doe 456 Somewhere else

  • XML Namespace ExampleXML Namespace Example

    John Smith 123 Here Jane Doe 456 Somewhere else

  • Illegal Namespace UsageIllegal Namespace Usage

    John Smith

    Both "type" attributes are bound to same namespace which is not valid!

  • Illegal Namespace UsageIllegal Namespace Usage

    John Smith

  • Reserved Namespaces and Reserved Namespaces and PrefixesPrefixes

    The prefix The prefix xmlxml is bound to is bound to http://www.w3.org/XML/1998/namespacehttp://www.w3.org/XML/1998/namespace..

    The prefix The prefix xmlnsxmlns is bound to is bound to http://www.w3.org/2000/xmlns/http://www.w3.org/2000/xmlns/..

    Prefixes should also not begin with the Prefixes should also not begin with the characters characters xmlxml..

  • Schemas and ValidationSchemas and Validation

    Validation insures an XML document Validation insures an XML document conforms to a set of defined rules.conforms to a set of defined rules.

    Multiple mechanisms exist to write document Multiple mechanisms exist to write document rule sets:rule sets: Document Type Definition (DTD)Document Type Definition (DTD) XML SchemaXML Schema RelaxNGRelaxNG

  • Document Type Definition Document Type Definition (DTD)(DTD)validation/courses-dtd.xmlvalidation/courses-dtd.xml

    ]>

    Basic Languages Introduction to Languages 1.5 2004-09-01T11:13:01

  • DTD and IDsDTD and IDsvalidation/course-id.xmlvalidation/course-id.xml

    Basic Languages Introduction to Languages French I Introduction to French

  • XML SchemaXML Schemavalidation/course.xsdvalidation/course.xsd

  • RelaxNGRelaxNGvalidation/course.rngvalidation/course.rng

  • XPathXPath

    Language to locate and retrieve information Language to locate and retrieve information from an XML documentfrom an XML document

    A foundation for XSLTA foundation for XSLT An XML document is a tree containing nodesAn XML document is a tree containing nodes The XML document is the root nodeThe XML document is the root node Locations are addressable similar to the syntax Locations are addressable similar to the syntax

    for a filesystemfor a filesystem

  • XPath Reference DocumentXPath Reference Documentxpath/courses.xmlxpath/courses.xml

    French I Introduction to French

    French II Intermediate French content

  • XPath Location ExampleXPath Location Examplexpath/location.phpxpath/location.php

    /courses/course/description/courses/*/description//description//description[ancestor::course]

    Expression:

    Resulting Nodset:

    Introduction to FrenchIntermediate French

  • XPath Function ExampleXPath Function Examplexpath/function.phpxpath/function.php

    string(/courses/course/pre-requisite[@cref="c2"]/..)string(/courses/course/pre-requisite[@cref="c2"]/..)

    French IIFrench II Intermediate FrenchIntermediate French contentcontent

    Reference:Reference:

  • XPath and NamespacesXPath and Namespacesxpath/namespaces.phpxpath/namespaces.php

    //titleEmpty NodeSet

    //defnsEmpty NodeSet

    content

    //t:titleFrench IFrench II

    //*[local-name()="defns"]

    French I

    French II content

    Reference:

  • PHP and XMLPHP and XML

    PHP 5 introduced numerous interfaces for PHP 5 introduced numerous interfaces for working with XMLworking with XML

    The libxml2 library (http://www.xmlsoft.org/) The libxml2 library (http://www.xmlsoft.org/) was chosen to provide XML supportwas chosen to provide XML support

    The sister library libxslt provides XSLT supportThe sister library libxslt provides XSLT support I/O is handled via PHP streamsI/O is handled via PHP streams ext/domxml was removed in favor of ext/domext/domxml was removed in favor of ext/dom

  • XML Entensions for PHP 5XML Entensions for PHP 5

    ext/libxmlext/libxml ext/xml (SAX push parser)ext/xml (SAX push parser) ext/domext/dom ext/simplexmlext/simplexml ext/xmlreader (pull parser)ext/xmlreader (pull parser) pecl/sdo (SCA_SDO)pecl/sdo (SCA_SDO) ext/xmlwriterext/xmlwriter ext/xslext/xsl ext/wddxext/wddx ext/soapext/soap

  • LibxmlLibxml

    Contains common functionality shared across Contains common functionality shared across extensions.extensions.

    Defines constants to modify parse time Defines constants to modify parse time behavior.behavior.

    Provides access to streams context.Provides access to streams context. Allows modification of error handling Allows modification of error handling

    behavior for XML based extensions.behavior for XML based extensions.

  • Libxml: Parser OptionsLibxml: Parser Options

    Disable network access when loadingLIBXML_NONET

    Merge CDATA nodes in Text nodesLIBXML_NOCDATA

    Perform XIncludes during parsingLIBXML_XINCLUDE

    Remove insignificant whitespace on parsingLIBXML_NOBLANKS

    Suppress parser warnings from libxml2LIBXML_NOWARNING

    Suppress parsing errors from libxml2LIBXML_NOERROR

    Loads subsets and perform validationLIBXML_DTDVALID

    Create defaulted attributes defined in DTDLIBXML_DTDATTR

    Load subsets but do not perform validationLIBXML_DTDLOAD

    Substitute entities with replacement contentLIBXML_NOENT

  • Libxml: Error HandlingLibxml: Error Handling

    bool libxml_use_internal_errors ([bool use_errors])bool libxml_use_internal_errors ([bool use_errors])

    void libxml_clear_errors ( void )void libxml_clear_errors ( void )

    LibXMLError libxml_get_last_error ( void )LibXMLError libxml_get_last_error ( void )

    array libxml_get_errors ( void )array libxml_get_errors ( void )

  • Libxml: LibXMLErrorLibxml: LibXMLError

    Class: LibXMLErrorProperties (Read-Only):

    (int) level(int) code(int) column(string) message(string) file(int) line

    LibXMLError::code Values:LIBXML_ERR_NONELIBXML_ERR_WARNINGLIBXML_ERR_ERRORLIBXML_ERR_FATAL

  • LibXMLError ExampleLibXMLError Examplelibxml/error.phplibxml/error.php

    loadXML('root')) { $arrError = libxml_get_errors(); foreach ($arrError AS $xmlError) { var_dump($xmlError); }} else { print "Document Loaded";}?>

  • LibXMLError ResultLibXMLError Result

    PHP Warning: DOMDocument::loadXML(): Premature end of data in tag root line 1 PHP Warning: DOMDocument::loadXML(): Premature end of data in tag root line 1 in Entity, line: 1 in /home/rrichards/workshop/libxml/error.php on line 4in Entity, line: 1 in /home/rrichards/workshop/libxml/error.php on line 4

    Warning: DOMDocument::loadXML(): Premature end of data in tag root line 1 in Warning: DOMDocument::loadXML(): Premature end of data in tag root line 1 in Entity, line: 1 in /home/rrichards/workshop/libxml/error.php on line 4Entity, line: 1 in /home/rrichards/workshop/libxml/error.php on line 4

    New Error Handling:New Error Handling:object(LibXMLError)#2 (6) {object(LibXMLError)#2 (6) { ["level"]=> int(3)["level"]=> int(3) ["code"]=> int(4)["code"]=> int(4) ["column"]=> int(1)["column"]=> int(1) ["message"]=> string(34) "Start tag expected, '

  • Libxml: Stream ContextLibxml: Stream Context

    $opts = array($opts = array(

    'http' => array('http' => array('user_agent' => 'PHP libxml2 agent','user_agent' => 'PHP libxml2 agent',

    'proxy' => 'tcp://localhost:8082','proxy' => 'tcp://localhost:8082',

    'request_fulluri' => TRUE'request_fulluri' => TRUE

    ))););

    $context = stream_context_create($opts);$context = stream_context_create($opts);libxml_set_streams_context($context);libxml_set_streams_context($context);

    $doc = DOMDocument::load('http://www.example.org/file.xml');$doc = DOMDocument::load('http://www.example.org/file.xml');

  • Preparing for Unicode in PHP 6Preparing for Unicode in PHP 6

    XML extensions are unicode ready for PHP 6XML extensions are unicode ready for PHP 6 Both the UTF-8 binary strings (existing behavi