Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse XML using a XSD in PHP

Tags:

php

xml

xsd

I have a XML file and a a bunch of XSD files with schemas. How do I parse the XML file using the correct XSD file and schema in PHP?

like image 353
Jonas Lejon Avatar asked Jul 14 '10 08:07

Jonas Lejon


People also ask

How connect XML to XSD?

Reference the XSD schema in the XML document using XML schema instance attributes such as either xsi:schemaLocation or xsi:noNamespaceSchemaLocation. Add the XSD schema file to a schema cache and then connect that cache to the DOM document or SAX reader, prior to loading or parsing the XML document.

How does XSD work with XML?

Basically an XSD file defines how the XML file is going to look like. It's a Schema file which defines the structure of the XML file. So it specifies what the possible fields are and what size they are going to be. An XML file is an instance of XSD as it uses the rules defined in the XSD.

Can we generate XML from XSD?

To create an XML file from an XSD file: Right-click the XML Documents folder in your data development project, and select New > XML. The New XML File wizard opens.

What is XSD parser?

XsdParser is a library that parses a XML Definition file (. xsd) into a list of java objects. Each different XSD tag has a corresponding Java class and the attributes of a given XSD type are represented as fields of that class.


2 Answers

You can use the DOM or XmlReader extensions

  • DOMDocument::schemaValidate — Validates a document based on a schema
  • XMLReader::setSchema — Validate document against XSD

to validate documents against a schema.

like image 65
Gordon Avatar answered Sep 28 '22 02:09

Gordon


Check this tool - http://github.com/moyarada/XSD-to-PHP. It sounds exactly what you're asking. You can marshal/unmarshal XML usind generated from XSD PHP classes.

like image 26
Mike Bevz Avatar answered Sep 28 '22 01:09

Mike Bevz