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.
linepogl
source share