PHPackages                             goetas/xsd2php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. goetas/xsd2php

Abandoned → [goetas-webservices/xsd2php](/?search=goetas-webservices%2Fxsd2php)ArchivedLibrary[Parsing &amp; Serialization](/categories/parsing)

goetas/xsd2php
==============

Convert XSD (XML Schema) definitions into PHP classes

2.1.0(9y ago)153337.6k↑225%61[7 issues](https://github.com/goetas/xsd2php/issues)[5 PRs](https://github.com/goetas/xsd2php/pulls)10LGPLPHPPHP &gt;=5.5

Since Jan 8Pushed 6y ago18 watchersCompare

[ Source](https://github.com/goetas/xsd2php)[ Packagist](https://packagist.org/packages/goetas/xsd2php)[ RSS](/packages/goetas-xsd2php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (8)Versions (9)Used By (10)

xsd2php
=======

[](#xsd2php)

**This repository is DEPRECATED, please use [goetas-webservices/xsd2php](https://github.com/goetas-webservices/xsd2php)**

[![Build Status](https://camo.githubusercontent.com/b9780826400393d8490d68ef28244fa4a1f213ded0326e60ab1d6f378541717d/68747470733a2f2f7472617669732d63692e6f72672f676f657461732f787364327068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/goetas/xsd2php)[![Code Coverage](https://camo.githubusercontent.com/47e346870cb42f7790d11cf8aa5d02f24a75992eebbafef5324645bc16e998da/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f676f657461732f787364327068702f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/goetas/xsd2php/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/3c5e17fabcd4c785bc2e0f2febafd382f10e332da967e35c45f82592b23b493b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f676f657461732f787364327068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/goetas/xsd2php/?branch=master)

Convert XSD into PHP classes.

With `goetas/xsd2php` you can convert any XSD/WSDL definition into PHP classes.

**XSD2PHP can also generate [JMS Serializer](http://jmsyst.com/libs/serializer) compatible metadata that can be used to serialize/unserialize the object instances**.

Installation
------------

[](#installation)

There is one recommended way to install xsd2php via [Composer](https://getcomposer.org/):

- adding the dependency to your `composer.json` file:

```
  "require-dev": {
      ..
      "goetas/xsd2php":"^2.1",
      ..
  }
```

Usage
-----

[](#usage)

With this example we will convert [OTA XSD definitions](http://opentravel.org/Specifications/OnlineXmlSchema.aspx) into PHP classes.

Suppose that you have allo XSD files in `/home/my/ota`.

Generate PHP classes
--------------------

[](#generate-php-classes)

```
vendor/bin/xsd2php convert:php \
`/home/my/ota/OTA_HotelAvail*.xsd \

--ns-map='http://www.opentravel.org/OTA/2003/05;Mercurio/OTA/2007B/' \

--ns-dest='Mercurio/OTA/2007B/;src/Mercurio/OTA/V2007B' \

--alias-map='http://www.opentravel.org/OTA/2003/05;CustomOTADateTimeFormat;Vendor/Project/CustomDateClass'

```

What about namespaces?

- `http://www.opentravel.org/OTA/2003/05` will be converted into `Mercurio/OTA/2007B` PHP namespace

Where place the files?

- `Mercurio/OTA/2007B` classes will be placed into `src/Mercurio/OTA/V2007B` directory

What about custom types?

- `--alias-map='http://www.opentravel.org/OTA/2003/05;CustomOTADateTimeFormat;Vendor/Project/CustomDateClass'`will instruct XSD2PHP to not generate any class for `CustomOTADateTimeFormat` type inside the `http://www.opentravel.org/OTA/2003/05` namespace. All reference to this type are replaced with the `Vendor/Project/CustomDateClass` class.

### Use composer scripts to generate classes

[](#use-composer-scripts-to-generate-classes)

```
  "scripts": {
    "build": "xsd2php convert:php '/home/my/ota/OTA_HotelAvail*.xsd' --ns-map='http://www.opentravel.org/OTA/2003/05;Mercurio/OTA/2007B/' --ns-dest='Mercurio/OTA/2007B/;src/Mercurio/OTA/V2007B'"
  }
```

Now you can build your classes with `composer build`.

Serialize / Unserialize
-----------------------

[](#serialize--unserialize)

XSD2PHP can also generate for you [JMS Serializer](http://jmsyst.com/libs/serializer) metadata that you can use to serialize/unserialize the generated PHP class instances.

```
vendor/bin/xsd2php  convert:jms-yaml \
`/home/my/ota/OTA_HotelAvail*.xsd \

--ns-map='http://www.opentravel.org/OTA/2003/05;Mercurio/OTA/2007B/'  \
--ns-dest='Mercurio/OTA/2007B/;src/Metadata/JMS;' \

--alias-map='http://www.opentravel.org/OTA/2003/05;CustomOTADateTimeFormat;Vendor/Project/CustomDateClass'

```

What about namespaces?

- `http://www.opentravel.org/OTA/2003/05` will be converted into `Mercurio/OTA/2007B` PHP namespace

Where place the files?

- `http://www.opentravel.org/OTA/2003/05` will be placed into `src/Metadata/JMS` directory

What about custom types?

- `--alias-map='http://www.opentravel.org/OTA/2003/05;CustomOTADateTimeFormat;Vendor/Project/CustomDateClass'`will instruct XSD2PHP to not generate any metadata information for `CustomOTADateTimeFormat` type inside the `http://www.opentravel.org/OTA/2003/05` namespace. All reference to this type are replaced with the `Vendor/Project/CustomDateClass` class. You have to provide a [custom serializer](http://jmsyst.com/libs/serializer/master/handlers#subscribing-handlers) for this type
- Add xsd2php dependency to satisfy BaseTypesHandler and XmlSchemaDateHandler.

```
"require" : {
    "goetas-webservices/xsd2php-runtime":"^0.2.2",
}
```

```
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\Handler\HandlerRegistryInterface;

use GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\BaseTypesHandler;
use GoetasWebservices\Xsd\XsdToPhpRuntime\Jms\Handler\XmlSchemaDateHandler;

$serializerBuilder = SerializerBuilder::create();
$serializerBuilder->addMetadataDir('metadata dir', 'DemoNs');
$serializerBuilder->configureHandlers(function (HandlerRegistryInterface $handler) use ($serializerBuilder) {
    $serializerBuilder->addDefaultHandlers();
    $handler->registerSubscribingHandler(new BaseTypesHandler()); // XMLSchema List handling
    $handler->registerSubscribingHandler(new XmlSchemaDateHandler()); // XMLSchema date handling

    // $handler->registerSubscribingHandler(new YourhandlerHere());
});

$serializer = $serializerBuilder->build();

// deserialize the XML into Demo\MyObject object
$object = $serializer->deserialize('', 'DemoNs\MyObject', 'xml');

// some code ....

// serialize the Demo\MyObject back into XML
$newXml = $serializer->serialize($object, 'xml');
```

Dealing with `xsd:anyType` or `xsd:anySimpleType`
-------------------------------------------------

[](#dealing-with-xsdanytype-or-xsdanysimpletype)

If your XSD contains `xsd:anyType` or `xsd:anySimpleType` types you have to specify a handler for this.

When you generate the JMS metadata you have to specify a custom handler:

```
bin/xsd2php.php convert:jms-yaml \

 ... various params ... \

--alias-map='http://www.w3.org/2001/XMLSchema;anyType;MyCustomAnyTypeHandler' \
--alias-map='http://www.w3.org/2001/XMLSchema;anyType;MyCustomAnySimpleTypeHandler' \
```

Now you have to create a custom serialization handler:

```
use JMS\Serializer\XmlSerializationVisitor;
use JMS\Serializer\XmlDeserializationVisitor;

use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\VisitorInterface;
use JMS\Serializer\Context;

class MyHandler implements SubscribingHandlerInterface
{
    public static function getSubscribingMethods()
    {
        return array(
            array(
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
                'format' => 'xml',
                'type' => 'MyCustomAnyTypeHandler',
                'method' => 'deserializeAnyType'
            ),
            array(
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
                'format' => 'xml',
                'type' => 'MyCustomAnyTypeHandler',
                'method' => 'serializeAnyType'
            )
        );
    }

    public function serializeAnyType(XmlSerializationVisitor $visitor, $data, array $type, Context $context)
    {
        // serialize your object here
    }

    public function deserializeAnyType(XmlDeserializationVisitor $visitor, $data, array $type)
    {
        // deserialize your object here
    }
}
```

Naming Strategy
---------------

[](#naming-strategy)

There are two types of naming strategies: `short` and `long`. The default is `short`, this naming strategy can however generate naming conflicts.

The `long` naming strategy will suffix elements with `Element` and types with `Type`.

- `MyNamespace\User` will become `MyNamespace\UserElement`
- `MyNamespace\UserType` will become `MyNamespace\UserTypeType`

An XSD for instance with a type named `User`, a type named `UserType`, a root element named `User` and `UserElement`, will only work when using the `long` naming strategy.

- If you don't have naming conflicts and you want to have short and descriptive class names, use the `--naming-strategy=short` option.
- If you have naming conflicts use the `--naming-strategy=long` option.
- If you want to be safe, use the `--naming-strategy=long` option.

Note
----

[](#note)

The code in this project is provided under the [MIT](https://opensource.org/licenses/MIT) license. For professional support contact or visit

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity50

Moderate usage in the ecosystem

Community36

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 91.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~234 days

Total

5

Last Release

3574d ago

Major Versions

1.0.1 → 2.0.0-alpha2014-09-25

PHP version history (3 changes)1.0.0PHP &gt;=5.3.2

2.0.0-alphaPHP ~5.4

2.0.0PHP &gt;=5.5

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/776743?v=4)[Asmir Mustafic](/maintainers/goetas)[@goetas](https://github.com/goetas)

---

Top Contributors

[![goetas](https://avatars.githubusercontent.com/u/776743?v=4)](https://github.com/goetas "goetas (182 commits)")[![Metabor](https://avatars.githubusercontent.com/u/2135064?v=4)](https://github.com/Metabor "Metabor (3 commits)")[![holtkamp](https://avatars.githubusercontent.com/u/776405?v=4)](https://github.com/holtkamp "holtkamp (2 commits)")[![harold4](https://avatars.githubusercontent.com/u/4209932?v=4)](https://github.com/harold4 "harold4 (1 commits)")[![jormeijer](https://avatars.githubusercontent.com/u/8984962?v=4)](https://github.com/jormeijer "jormeijer (1 commits)")[![l3l0](https://avatars.githubusercontent.com/u/209405?v=4)](https://github.com/l3l0 "l3l0 (1 commits)")[![polem](https://avatars.githubusercontent.com/u/379985?v=4)](https://github.com/polem "polem (1 commits)")[![reenl](https://avatars.githubusercontent.com/u/951945?v=4)](https://github.com/reenl "reenl (1 commits)")[![robinkanters](https://avatars.githubusercontent.com/u/1043866?v=4)](https://github.com/robinkanters "robinkanters (1 commits)")[![RSully](https://avatars.githubusercontent.com/u/185004?v=4)](https://github.com/RSully "RSully (1 commits)")[![thomporter](https://avatars.githubusercontent.com/u/27414?v=4)](https://github.com/thomporter "thomporter (1 commits)")[![agnagna](https://avatars.githubusercontent.com/u/7677033?v=4)](https://github.com/agnagna "agnagna (1 commits)")[![tvillaluz](https://avatars.githubusercontent.com/u/1340778?v=4)](https://github.com/tvillaluz "tvillaluz (1 commits)")[![bitdeli-chef](https://avatars.githubusercontent.com/u/3092978?v=4)](https://github.com/bitdeli-chef "bitdeli-chef (1 commits)")[![discordier](https://avatars.githubusercontent.com/u/940331?v=4)](https://github.com/discordier "discordier (1 commits)")

---

Tags

phpxmlserializerconverterjmsxsd

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/goetas-xsd2php/health.svg)

```
[![Health](https://phpackages.com/badges/goetas-xsd2php/health.svg)](https://phpackages.com/packages/goetas-xsd2php)
```

###  Alternatives

[goetas-webservices/xsd2php

Convert XSD (XML Schema) definitions into PHP classes and JMS metadata

2411.6M37](/packages/goetas-webservices-xsd2php)[goetas-webservices/xsd2php-runtime

Convert XSD (XML Schema) definitions into PHP classes

4910.9M36](/packages/goetas-webservices-xsd2php-runtime)[goetas/xsd2php-runtime

Convert XSD (XML Schema) definitions into PHP classes

493.3k](/packages/goetas-xsd2php-runtime)[goetas-webservices/xsd-reader

Read any XML Schema (XSD) programmatically with PHP

624.7M15](/packages/goetas-webservices-xsd-reader)[php-soap/wsdl-reader

A WSDL reader in PHP

212.3M9](/packages/php-soap-wsdl-reader)[thunderer/serializard

Flexible serializer

2767.3k1](/packages/thunderer-serializard)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
