How can I create a schema based XML document using php?

I want to create an xml file from data given by users.

I can use simplexml or DOMDocument to create xml files, and there is even an option in DOMDocument to validate an XML document with a schema.

But I need instead of creating nodes and adding values ​​using the xml classes, can I create an xml file from data stored somewhere else in connection with the schema?

I think .net has the ability to write to xml from a read from a dataset. But I could not find such a thing in PHP.

Is this possible and are there classes for this?

If there are no predefined classes, at least any help on any means of this?

Edit:

I am editing this question because it seems that some of you do not quite understand my requirement.

For example, if the circuit

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified"> <xs:element name="formpatterns"> <xs:element name="pan" type="pantype"/> <xs:element name="name" type="nametype"/> <xs:element name="fatherName" type="nametype"/> <xs:group ref="address"/> <xs:element name="dob" type="xs:date"/> </xs:schema> 

and if the user gives data for panning, name, father's name, address, dob, then I need to create an xml document automatically, matching the scheme and the data.

The scheme may change from time to time, so I do not want to edit all the code to create / modify nodes and attributes. I just need to specify a new scheme so that the code creates on the basis of xml.

+6
xml php xsd
source share
3 answers

Take a look at https://github.com/moyarada/XSD-to-PHP , it compiles PHP bindings from XSD, and then you can serialize PHP classes to XML.

+1
source share

There is no easy answer in your question. This can be done with metaprograms, but it will be long. In fact, the more complex your XSD, the longer your code will be. However, if you use a simple XSD with primitive xs: * types, it is easy to define a converter from a PHP type to an XML string.

Parsing your XSD, you can dynamically create an array that will look so tough:

 $meta = array( 'name' => 'xs:string', 'dob' => 'xs:date' ); 

If the user enters something like:

 $input = array( 'name' => 'Name', 'dob' => new DateTime('30 years ago') ); 

Then you can dynamically produce the following:

 $output = array( 'name' => convert( $input['name'] , $meta['name'] ), 'dob' => convert( $input['dob'] , $meta['dob'] ) ); 

The key is a conversion function. Using the instanceof and is_ * operators, you can determine the data type of the first argument. So, all you have to do is return an XML escaped string for each of the possible combinations:

 php string --> xml 'xs:string' php DateTime --> xml 'xs:date' 

...

Then you can create your final XML.

0
source share

I think this should help you if you want to create XML files using PHP DOM functions

-one
source share

All Articles