Generating XML from schema (XSD) using PHP


I am doing a project for generating, storing, editing and searching XML documents in PHP. Could any one tell me how to generate XML file from the XSD schema file using PHP .

I want a process similar to what JAXB does in Java.


This would be awkward or involve creating a simple parser, however why can’t you just invoke JAXB?

Thanks for the response but my preference is to do it with PHP so I am searching for solutions with PHP. :slight_smile:


It not at all clear what you’re really asking for. You say you want to generate XML from the the XML schema, but it’s already an XML file.

I looked up JAXB, and what it seems to do is to generate Java classes from the XML schema that do the same type checks that the XML schema does. So are you really asking how to generate PHP code from the XML schema? I’m sure that’s possible, but probably no small job.

Or are you really just asking how to do parse an XML file with validation using a schema?

Cross referencing his other post, it is apparent that he wishes to generate an XML document from a defined XML Schema (XSD). Firstly you would need to validate the XSD ideally, which is quite simply done with PHP5, however this would be a little more awkward with PHP4. It would simply just add complexity to the initial problem, so I wont go into detail on that.

(Hint: PHP4 DTD for XSD)

You mentioned generating the XML document from XSL, now while this is possible I would avoid using XSL for the this task. Using Sax, Sablotron or LibXML, you can use events to trigger processing on different elements, attributes etc, which would therefore reduce the overall time to produce a simple example.


[]XML Schema Part 0: Primer
]XML Schema Part 1: Structures
[*]XML Schema Part 2: Datatypes
[/list] It really depends on, how in depth you need to go, for a basic working sample, it should just be logically applying default elements, attributes etc.

What’s wrong with using XSLT to generate the XML document? That is what it was designed for – converting one XML document to another…


i agree with Seratonin, XSL is ideal for transforming XML docs, that is in fact its main purpose

How would you handle regular expressions in XSLT for example? This functionality has been incorperated into XPath 2.0, which is not supported by any of the libraries present in PHP. There are work arounds such as embedding PHP and calling a stream, however this is not advisable. EXSLT does provide functionality for regexp, however i’m not sure whether the current implementation of libXML incorperates this nor can I currently check to see, does it?

[XSLT 2.0]

If you are going from an XMLSchema document to another XML document, why do you need regular expression support? If you have to use EXSLT for anything why not just use PHP functions from the XSLT document:

If the problem is to transform an XML document from one form to another, the standard solution is to use XSLT. Even source code generation would be a cinch with XSLT (I have done this before by taking a UML diagram, exporting it to XMI, and then generating Java source code using XSLT).


Calling PHP functions from XSLT (evil…):wink:

This is the problem with other parsers, with custom inbuilt functions, therefore you’re stuck with a solution for a particular problem. EXSLT has helped, although it is still limited, for example, base64. The reason I immediately suggested parsing this using DOM or SAX, was due to certain datatypes in XML Schema’s. However, probably the best method would be to define a custom function and then call PHP…

Define your own XSLT functions with EXSLT

   <func:function name="custom:base64decode">
       <xsl:param name="binary"/>
       <func:result select="php:functionbase64decode($binary)" />
       <xsl:fallback><p>This version of the parser does not support EXSLT functions or calling of PHP functions.</p></xsl:fallback>

   <func:function name="custom:base64encode">
       <xsl:param name="binary"/>
       <func:result select="php:functionbase64encode($binary)" />
       <xsl:fallback><p>This version of the parser does not support EXSLT functions or calling of PHP functions.</p></xsl:fallback>

PS. I didn’t suggest this;)

Hello everybody,

Thanks for all of you interests and sorry for replying late.

I have come to a conclusion of generating XML manually as there are no such APIs to generate PHP classes from XSD.

When I tried to transform XML to XSLT I had to recompile PHP 4 to use the XSLT functions but my ISP does not want to recompile PHP. Similarly to use DOM functions I have to recompile PHP 4. So could you guys suggest me PHP API (which my depend on PEAR) for dealing with XML,SAX,DOM,XSLT without recompiling PHP 4 . Some thing like myXML ( but this has only limited functionalities and no SAX.

Again the SAX funtions of PHP 4 does not meet the standard with W3C so the functions are changed in PHP 5 so if I use the PHP directly without a API the I have to recode the SAX functions if I have to move from PHP 4 to PHP 5 :frowning:


Hi, I have the same question… except, I can use php 5.

So if I understand this right, I have to do the following:

  • create a xsd-file
  • create a xslt-file
  • make a php script, that creates the xml document, the php script uses the xsd and xslt-files.

If this is correct, can someone give me some hints on creating that php file? I use php 5.