7
* parses an XML Schema, allows access to it's data, other utility methods.
8
* imperfect, no validation... yet, but quite functional.
10
* @author Dietrich Ayala <dietrich@ganx4.com>
11
* @author Scott Nichol <snichol@users.sourceforge.net>
12
* @version $Id: class.xmlschema.php,v 1.49 2007/11/06 14:17:53 snichol Exp $
15
class nusoap_xmlschema extends nusoap_base {
21
var $enclosingNamespaces;
23
var $schemaInfo = array();
24
var $schemaTargetNamespace = '';
25
// types, elements, attributes defined by the schema
26
var $attributes = array();
27
var $complexTypes = array();
28
var $complexTypeStack = array();
29
var $currentComplexType = null;
30
var $elements = array();
31
var $elementStack = array();
32
var $currentElement = null;
33
var $simpleTypes = array();
34
var $simpleTypeStack = array();
35
var $currentSimpleType = null;
37
var $imports = array();
42
var $depth_array = array();
43
var $message = array();
44
var $defaultNamespace = array();
49
* @param string $schema schema document URI
50
* @param string $xml xml document URI
51
* @param string $namespaces namespaces defined in enclosing XML
54
function nusoap_xmlschema($schema='',$xml='',$namespaces=array()){
55
parent::nusoap_base();
56
$this->debug('nusoap_xmlschema class instantiated, inside constructor');
58
$this->schema = $schema;
62
$this->enclosingNamespaces = $namespaces;
63
$this->namespaces = array_merge($this->namespaces, $namespaces);
67
$this->debug('initial schema file: '.$schema);
68
$this->parseFile($schema, 'schema');
73
$this->debug('initial xml file: '.$xml);
74
$this->parseFile($xml, 'xml');
82
* @param string $xml path/URL to XML file
83
* @param string $type (schema | xml)
87
function parseFile($xml,$type){
90
$xmlStr = @join("",@file($xml));
92
$msg = 'Error reading XML from '.$xml;
93
$this->setError($msg);
97
$this->debug("parsing $xml");
98
$this->parseString($xmlStr,$type);
99
$this->debug("done parsing $xml");
107
* parse an XML string
109
* @param string $xml path or URL
110
* @param string $type (schema|xml)
113
function parseString($xml,$type){
117
// Create an XML parser.
118
$this->parser = xml_parser_create();
119
// Set the options for parsing the XML data.
120
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
122
// Set the object for the parser.
123
xml_set_object($this->parser, $this);
125
// Set the element handlers for the parser.
126
if($type == "schema"){
127
xml_set_element_handler($this->parser, 'schemaStartElement','schemaEndElement');
128
xml_set_character_data_handler($this->parser,'schemaCharacterData');
129
} elseif($type == "xml"){
130
xml_set_element_handler($this->parser, 'xmlStartElement','xmlEndElement');
131
xml_set_character_data_handler($this->parser,'xmlCharacterData');
134
// Parse the XML file.
135
if(!xml_parse($this->parser,$xml,true)){
136
// Display an error message.
137
$errstr = sprintf('XML error parsing XML schema on line %d: %s',
138
xml_get_current_line_number($this->parser),
139
xml_error_string(xml_get_error_code($this->parser))
141
$this->debug($errstr);
142
$this->debug("XML payload:\n" . $xml);
143
$this->setError($errstr);
146
xml_parser_free($this->parser);
148
$this->debug('no xml passed to parseString()!!');
149
$this->setError('no xml passed to parseString()!!');
154
* gets a type name for an unnamed type
156
* @param string Element name
157
* @return string A type name for an unnamed type
160
function CreateTypeName($ename) {
162
for ($i = 0; $i < count($this->complexTypeStack); $i++) {
163
$scope .= $this->complexTypeStack[$i] . '_';
165
return $scope . $ename . '_ContainedType';
169
* start-element handler
171
* @param string $parser XML parser object
172
* @param string $name element name
173
* @param string $attrs associative array of attributes
176
function schemaStartElement($parser, $name, $attrs) {
178
// position in the total number of elements, starting from 0
179
$pos = $this->position++;
180
$depth = $this->depth++;
181
// set self as current value for this depth
182
$this->depth_array[$depth] = $pos;
183
$this->message[$pos] = array('cdata' => '');
185
$this->defaultNamespace[$pos] = $this->defaultNamespace[$this->depth_array[$depth - 1]];
187
$this->defaultNamespace[$pos] = false;
190
// get element prefix
191
if($prefix = $this->getPrefix($name)){
192
// get unqualified name
193
$name = $this->getLocalPart($name);
198
// loop thru attributes, expanding, and registering namespace declarations
199
if(count($attrs) > 0){
200
foreach($attrs as $k => $v){
201
// if ns declarations, add to class level array of valid namespaces
202
if(ereg("^xmlns",$k)){
203
//$this->xdebug("$k: $v");
204
//$this->xdebug('ns_prefix: '.$this->getPrefix($k));
205
if($ns_prefix = substr(strrchr($k,':'),1)){
206
//$this->xdebug("Add namespace[$ns_prefix] = $v");
207
$this->namespaces[$ns_prefix] = $v;
209
$this->defaultNamespace[$pos] = $v;
210
if (! $this->getPrefixFromNamespace($v)) {
211
$this->namespaces['ns'.(count($this->namespaces)+1)] = $v;
214
if($v == 'http://www.w3.org/2001/XMLSchema' || $v == 'http://www.w3.org/1999/XMLSchema' || $v == 'http://www.w3.org/2000/10/XMLSchema'){
215
$this->XMLSchemaVersion = $v;
216
$this->namespaces['xsi'] = $v.'-instance';
220
foreach($attrs as $k => $v){
221
// expand each attribute
222
$k = strpos($k,':') ? $this->expandQname($k) : $k;
223
$v = strpos($v,':') ? $this->expandQname($v) : $v;
230
// find status, register data
232
case 'all': // (optional) compositor content for a complexType
236
//$this->xdebug("compositor $name for currentComplexType: $this->currentComplexType and currentElement: $this->currentElement");
237
$this->complexTypes[$this->currentComplexType]['compositor'] = $name;
238
//if($name == 'all' || $name == 'sequence'){
239
// $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
242
case 'attribute': // complexType attribute
243
//$this->xdebug("parsing attribute $attrs[name] $attrs[ref] of value: ".$attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']);
244
$this->xdebug("parsing attribute:");
245
$this->appendDebug($this->varDump($attrs));
246
if (!isset($attrs['form'])) {
247
$attrs['form'] = $this->schemaInfo['attributeFormDefault'];
249
if (isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) {
250
$v = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
251
if (!strpos($v, ':')) {
252
// no namespace in arrayType attribute value...
253
if ($this->defaultNamespace[$pos]) {
254
// ...so use the default
255
$attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'] = $this->defaultNamespace[$pos] . ':' . $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
259
if(isset($attrs['name'])){
260
$this->attributes[$attrs['name']] = $attrs;
261
$aname = $attrs['name'];
262
} elseif(isset($attrs['ref']) && $attrs['ref'] == 'http://schemas.xmlsoap.org/soap/encoding/:arrayType'){
263
if (isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) {
264
$aname = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
268
} elseif(isset($attrs['ref'])){
269
$aname = $attrs['ref'];
270
$this->attributes[$attrs['ref']] = $attrs;
273
if($this->currentComplexType){ // This should *always* be
274
$this->complexTypes[$this->currentComplexType]['attrs'][$aname] = $attrs;
276
// arrayType attribute
277
if(isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']) || $this->getLocalPart($aname) == 'arrayType'){
278
$this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
279
$prefix = $this->getPrefix($aname);
280
if(isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])){
281
$v = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
285
if(strpos($v,'[,]')){
286
$this->complexTypes[$this->currentComplexType]['multidimensional'] = true;
288
$v = substr($v,0,strpos($v,'[')); // clip the []
289
if(!strpos($v,':') && isset($this->typemap[$this->XMLSchemaVersion][$v])){
290
$v = $this->XMLSchemaVersion.':'.$v;
292
$this->complexTypes[$this->currentComplexType]['arrayType'] = $v;
295
case 'complexContent': // (optional) content for a complexType
298
array_push($this->complexTypeStack, $this->currentComplexType);
299
if(isset($attrs['name'])){
300
// TODO: what is the scope of named complexTypes that appear
301
// nested within other c complexTypes?
302
$this->xdebug('processing named complexType '.$attrs['name']);
303
//$this->currentElement = false;
304
$this->currentComplexType = $attrs['name'];
305
$this->complexTypes[$this->currentComplexType] = $attrs;
306
$this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType';
307
// This is for constructs like
308
// <complexType name="ListOfString" base="soap:Array">
310
// <element name="string" type="xsd:string"
311
// minOccurs="0" maxOccurs="unbounded" />
314
if(isset($attrs['base']) && ereg(':Array$',$attrs['base'])){
315
$this->xdebug('complexType is unusual array');
316
$this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
318
$this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
321
$name = $this->CreateTypeName($this->currentElement);
322
$this->xdebug('processing unnamed complexType for element ' . $this->currentElement . ' named ' . $name);
323
$this->currentComplexType = $name;
324
//$this->currentElement = false;
325
$this->complexTypes[$this->currentComplexType] = $attrs;
326
$this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType';
327
// This is for constructs like
328
// <complexType name="ListOfString" base="soap:Array">
330
// <element name="string" type="xsd:string"
331
// minOccurs="0" maxOccurs="unbounded" />
334
if(isset($attrs['base']) && ereg(':Array$',$attrs['base'])){
335
$this->xdebug('complexType is unusual array');
336
$this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
338
$this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
343
array_push($this->elementStack, $this->currentElement);
344
if (!isset($attrs['form'])) {
345
$attrs['form'] = $this->schemaInfo['elementFormDefault'];
347
if(isset($attrs['type'])){
348
$this->xdebug("processing typed element ".$attrs['name']." of type ".$attrs['type']);
349
if (! $this->getPrefix($attrs['type'])) {
350
if ($this->defaultNamespace[$pos]) {
351
$attrs['type'] = $this->defaultNamespace[$pos] . ':' . $attrs['type'];
352
$this->xdebug('used default namespace to make type ' . $attrs['type']);
355
// This is for constructs like
356
// <complexType name="ListOfString" base="soap:Array">
358
// <element name="string" type="xsd:string"
359
// minOccurs="0" maxOccurs="unbounded" />
362
if ($this->currentComplexType && $this->complexTypes[$this->currentComplexType]['phpType'] == 'array') {
363
$this->xdebug('arrayType for unusual array is ' . $attrs['type']);
364
$this->complexTypes[$this->currentComplexType]['arrayType'] = $attrs['type'];
366
$this->currentElement = $attrs['name'];
367
$ename = $attrs['name'];
368
} elseif(isset($attrs['ref'])){
369
$this->xdebug("processing element as ref to ".$attrs['ref']);
370
$this->currentElement = "ref to ".$attrs['ref'];
371
$ename = $this->getLocalPart($attrs['ref']);
373
$type = $this->CreateTypeName($this->currentComplexType . '_' . $attrs['name']);
374
$this->xdebug("processing untyped element " . $attrs['name'] . ' type ' . $type);
375
$this->currentElement = $attrs['name'];
376
$attrs['type'] = $this->schemaTargetNamespace . ':' . $type;
377
$ename = $attrs['name'];
379
if (isset($ename) && $this->currentComplexType) {
380
$this->xdebug("add element $ename to complexType $this->currentComplexType");
381
$this->complexTypes[$this->currentComplexType]['elements'][$ename] = $attrs;
382
} elseif (!isset($attrs['ref'])) {
383
$this->xdebug("add element $ename to elements array");
384
$this->elements[ $attrs['name'] ] = $attrs;
385
$this->elements[ $attrs['name'] ]['typeClass'] = 'element';
388
case 'enumeration': // restriction value list member
389
$this->xdebug('enumeration ' . $attrs['value']);
390
if ($this->currentSimpleType) {
391
$this->simpleTypes[$this->currentSimpleType]['enumeration'][] = $attrs['value'];
392
} elseif ($this->currentComplexType) {
393
$this->complexTypes[$this->currentComplexType]['enumeration'][] = $attrs['value'];
396
case 'extension': // simpleContent or complexContent type extension
397
$this->xdebug('extension ' . $attrs['base']);
398
if ($this->currentComplexType) {
399
$this->complexTypes[$this->currentComplexType]['extensionBase'] = $attrs['base'];
403
if (isset($attrs['schemaLocation'])) {
404
//$this->xdebug('import namespace ' . $attrs['namespace'] . ' from ' . $attrs['schemaLocation']);
405
$this->imports[$attrs['namespace']][] = array('location' => $attrs['schemaLocation'], 'loaded' => false);
407
//$this->xdebug('import namespace ' . $attrs['namespace']);
408
$this->imports[$attrs['namespace']][] = array('location' => '', 'loaded' => true);
409
if (! $this->getPrefixFromNamespace($attrs['namespace'])) {
410
$this->namespaces['ns'.(count($this->namespaces)+1)] = $attrs['namespace'];
414
case 'list': // simpleType value list
416
case 'restriction': // simpleType, simpleContent or complexContent value restriction
417
$this->xdebug('restriction ' . $attrs['base']);
418
if($this->currentSimpleType){
419
$this->simpleTypes[$this->currentSimpleType]['type'] = $attrs['base'];
420
} elseif($this->currentComplexType){
421
$this->complexTypes[$this->currentComplexType]['restrictionBase'] = $attrs['base'];
422
if(strstr($attrs['base'],':') == ':Array'){
423
$this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
428
$this->schemaInfo = $attrs;
429
$this->schemaInfo['schemaVersion'] = $this->getNamespaceFromPrefix($prefix);
430
if (isset($attrs['targetNamespace'])) {
431
$this->schemaTargetNamespace = $attrs['targetNamespace'];
433
if (!isset($attrs['elementFormDefault'])) {
434
$this->schemaInfo['elementFormDefault'] = 'unqualified';
436
if (!isset($attrs['attributeFormDefault'])) {
437
$this->schemaInfo['attributeFormDefault'] = 'unqualified';
440
case 'simpleContent': // (optional) content for a complexType
443
array_push($this->simpleTypeStack, $this->currentSimpleType);
444
if(isset($attrs['name'])){
445
$this->xdebug("processing simpleType for name " . $attrs['name']);
446
$this->currentSimpleType = $attrs['name'];
447
$this->simpleTypes[ $attrs['name'] ] = $attrs;
448
$this->simpleTypes[ $attrs['name'] ]['typeClass'] = 'simpleType';
449
$this->simpleTypes[ $attrs['name'] ]['phpType'] = 'scalar';
451
$name = $this->CreateTypeName($this->currentComplexType . '_' . $this->currentElement);
452
$this->xdebug('processing unnamed simpleType for element ' . $this->currentElement . ' named ' . $name);
453
$this->currentSimpleType = $name;
454
//$this->currentElement = false;
455
$this->simpleTypes[$this->currentSimpleType] = $attrs;
456
$this->simpleTypes[$this->currentSimpleType]['phpType'] = 'scalar';
459
case 'union': // simpleType type list
462
//$this->xdebug("do not have anything to do for element $name");
467
* end-element handler
469
* @param string $parser XML parser object
470
* @param string $name element name
473
function schemaEndElement($parser, $name) {
474
// bring depth down a notch
476
// position of current element is equal to the last value left in depth_array for my depth
477
if(isset($this->depth_array[$this->depth])){
478
$pos = $this->depth_array[$this->depth];
480
// get element prefix
481
if ($prefix = $this->getPrefix($name)){
482
// get unqualified name
483
$name = $this->getLocalPart($name);
488
if($name == 'complexType'){
489
$this->xdebug('done processing complexType ' . ($this->currentComplexType ? $this->currentComplexType : '(unknown)'));
490
$this->currentComplexType = array_pop($this->complexTypeStack);
491
//$this->currentElement = false;
493
if($name == 'element'){
494
$this->xdebug('done processing element ' . ($this->currentElement ? $this->currentElement : '(unknown)'));
495
$this->currentElement = array_pop($this->elementStack);
497
if($name == 'simpleType'){
498
$this->xdebug('done processing simpleType ' . ($this->currentSimpleType ? $this->currentSimpleType : '(unknown)'));
499
$this->currentSimpleType = array_pop($this->simpleTypeStack);
504
* element content handler
506
* @param string $parser XML parser object
507
* @param string $data element content
510
function schemaCharacterData($parser, $data){
511
$pos = $this->depth_array[$this->depth - 1];
512
$this->message[$pos]['cdata'] .= $data;
516
* serialize the schema
520
function serializeSchema(){
522
$schemaPrefix = $this->getPrefixFromNamespace($this->XMLSchemaVersion);
525
if (sizeof($this->imports) > 0) {
526
foreach($this->imports as $ns => $list) {
527
foreach ($list as $ii) {
528
if ($ii['location'] != '') {
529
$xml .= " <$schemaPrefix:import location=\"" . $ii['location'] . '" namespace="' . $ns . "\" />\n";
531
$xml .= " <$schemaPrefix:import namespace=\"" . $ns . "\" />\n";
537
foreach($this->complexTypes as $typeName => $attrs){
539
// serialize child elements
540
if(isset($attrs['elements']) && (count($attrs['elements']) > 0)){
541
foreach($attrs['elements'] as $element => $eParts){
542
if(isset($eParts['ref'])){
543
$contentStr .= " <$schemaPrefix:element ref=\"$element\"/>\n";
545
$contentStr .= " <$schemaPrefix:element name=\"$element\" type=\"" . $this->contractQName($eParts['type']) . "\"";
546
foreach ($eParts as $aName => $aValue) {
547
// handle, e.g., abstract, default, form, minOccurs, maxOccurs, nillable
548
if ($aName != 'name' && $aName != 'type') {
549
$contentStr .= " $aName=\"$aValue\"";
552
$contentStr .= "/>\n";
555
// compositor wraps elements
556
if (isset($attrs['compositor']) && ($attrs['compositor'] != '')) {
557
$contentStr = " <$schemaPrefix:$attrs[compositor]>\n".$contentStr." </$schemaPrefix:$attrs[compositor]>\n";
561
if(isset($attrs['attrs']) && (count($attrs['attrs']) >= 1)){
562
foreach($attrs['attrs'] as $attr => $aParts){
563
$contentStr .= " <$schemaPrefix:attribute";
564
foreach ($aParts as $a => $v) {
565
if ($a == 'ref' || $a == 'type') {
566
$contentStr .= " $a=\"".$this->contractQName($v).'"';
567
} elseif ($a == 'http://schemas.xmlsoap.org/wsdl/:arrayType') {
568
$this->usedNamespaces['wsdl'] = $this->namespaces['wsdl'];
569
$contentStr .= ' wsdl:arrayType="'.$this->contractQName($v).'"';
571
$contentStr .= " $a=\"$v\"";
574
$contentStr .= "/>\n";
578
if (isset($attrs['restrictionBase']) && $attrs['restrictionBase'] != ''){
579
$contentStr = " <$schemaPrefix:restriction base=\"".$this->contractQName($attrs['restrictionBase'])."\">\n".$contentStr." </$schemaPrefix:restriction>\n";
580
// complex or simple content
581
if ((isset($attrs['elements']) && count($attrs['elements']) > 0) || (isset($attrs['attrs']) && count($attrs['attrs']) > 0)){
582
$contentStr = " <$schemaPrefix:complexContent>\n".$contentStr." </$schemaPrefix:complexContent>\n";
585
// finalize complex type
586
if($contentStr != ''){
587
$contentStr = " <$schemaPrefix:complexType name=\"$typeName\">\n".$contentStr." </$schemaPrefix:complexType>\n";
589
$contentStr = " <$schemaPrefix:complexType name=\"$typeName\"/>\n";
594
if(isset($this->simpleTypes) && count($this->simpleTypes) > 0){
595
foreach($this->simpleTypes as $typeName => $eParts){
596
$xml .= " <$schemaPrefix:simpleType name=\"$typeName\">\n <$schemaPrefix:restriction base=\"".$this->contractQName($eParts['type'])."\">\n";
597
if (isset($eParts['enumeration'])) {
598
foreach ($eParts['enumeration'] as $e) {
599
$xml .= " <$schemaPrefix:enumeration value=\"$e\"/>\n";
602
$xml .= " </$schemaPrefix:restriction>\n </$schemaPrefix:simpleType>";
606
if(isset($this->elements) && count($this->elements) > 0){
607
foreach($this->elements as $element => $eParts){
608
$xml .= " <$schemaPrefix:element name=\"$element\" type=\"".$this->contractQName($eParts['type'])."\"/>\n";
612
if(isset($this->attributes) && count($this->attributes) > 0){
613
foreach($this->attributes as $attr => $aParts){
614
$xml .= " <$schemaPrefix:attribute name=\"$attr\" type=\"".$this->contractQName($aParts['type'])."\"\n/>";
619
foreach ($this->schemaInfo as $k => $v) {
620
if ($k == 'elementFormDefault' || $k == 'attributeFormDefault') {
621
$attr .= " $k=\"$v\"";
624
$el = "<$schemaPrefix:schema$attr targetNamespace=\"$this->schemaTargetNamespace\"\n";
625
foreach (array_diff($this->usedNamespaces, $this->enclosingNamespaces) as $nsp => $ns) {
626
$el .= " xmlns:$nsp=\"$ns\"";
628
$xml = $el . ">\n".$xml."</$schemaPrefix:schema>\n";
633
* adds debug data to the clas level debug string
635
* @param string $string debug data
638
function xdebug($string){
639
$this->debug('<' . $this->schemaTargetNamespace . '> '.$string);
643
* get the PHP type of a user defined type in the schema
644
* PHP type is kind of a misnomer since it actually returns 'struct' for assoc. arrays
645
* returns false if no type exists, or not w/ the given namespace
646
* else returns a string that is either a native php type, or 'struct'
648
* @param string $type name of defined type
649
* @param string $ns namespace of type
654
function getPHPType($type,$ns){
655
if(isset($this->typemap[$ns][$type])){
656
//print "found type '$type' and ns $ns in typemap<br>";
657
return $this->typemap[$ns][$type];
658
} elseif(isset($this->complexTypes[$type])){
659
//print "getting type '$type' and ns $ns from complexTypes array<br>";
660
return $this->complexTypes[$type]['phpType'];
666
* returns an associative array of information about a given type
667
* returns false if no type exists by the given name
669
* For a complexType typeDef = array(
670
* 'restrictionBase' => '',
672
* 'compositor' => '(sequence|all)',
673
* 'elements' => array(), // refs to elements array
674
* 'attrs' => array() // refs to attributes array
675
* ... and so on (see addComplexType)
678
* For simpleType or element, the array has different keys.
680
* @param string $type
683
* @see addComplexType
687
function getTypeDef($type){
688
//$this->debug("in getTypeDef for type $type");
689
if (substr($type, -1) == '^') {
691
$type = substr($type, 0, -1);
696
if((! $is_element) && isset($this->complexTypes[$type])){
697
$this->xdebug("in getTypeDef, found complexType $type");
698
return $this->complexTypes[$type];
699
} elseif((! $is_element) && isset($this->simpleTypes[$type])){
700
$this->xdebug("in getTypeDef, found simpleType $type");
701
if (!isset($this->simpleTypes[$type]['phpType'])) {
702
// get info for type to tack onto the simple type
703
// TODO: can this ever really apply (i.e. what is a simpleType really?)
704
$uqType = substr($this->simpleTypes[$type]['type'], strrpos($this->simpleTypes[$type]['type'], ':') + 1);
705
$ns = substr($this->simpleTypes[$type]['type'], 0, strrpos($this->simpleTypes[$type]['type'], ':'));
706
$etype = $this->getTypeDef($uqType);
708
$this->xdebug("in getTypeDef, found type for simpleType $type:");
709
$this->xdebug($this->varDump($etype));
710
if (isset($etype['phpType'])) {
711
$this->simpleTypes[$type]['phpType'] = $etype['phpType'];
713
if (isset($etype['elements'])) {
714
$this->simpleTypes[$type]['elements'] = $etype['elements'];
718
return $this->simpleTypes[$type];
719
} elseif(isset($this->elements[$type])){
720
$this->xdebug("in getTypeDef, found element $type");
721
if (!isset($this->elements[$type]['phpType'])) {
722
// get info for type to tack onto the element
723
$uqType = substr($this->elements[$type]['type'], strrpos($this->elements[$type]['type'], ':') + 1);
724
$ns = substr($this->elements[$type]['type'], 0, strrpos($this->elements[$type]['type'], ':'));
725
$etype = $this->getTypeDef($uqType);
727
$this->xdebug("in getTypeDef, found type for element $type:");
728
$this->xdebug($this->varDump($etype));
729
if (isset($etype['phpType'])) {
730
$this->elements[$type]['phpType'] = $etype['phpType'];
732
if (isset($etype['elements'])) {
733
$this->elements[$type]['elements'] = $etype['elements'];
735
} elseif ($ns == 'http://www.w3.org/2001/XMLSchema') {
736
$this->xdebug("in getTypeDef, element $type is an XSD type");
737
$this->elements[$type]['phpType'] = 'scalar';
740
return $this->elements[$type];
741
} elseif(isset($this->attributes[$type])){
742
$this->xdebug("in getTypeDef, found attribute $type");
743
return $this->attributes[$type];
744
} elseif (ereg('_ContainedType$', $type)) {
745
$this->xdebug("in getTypeDef, have an untyped element $type");
746
$typeDef['typeClass'] = 'simpleType';
747
$typeDef['phpType'] = 'scalar';
748
$typeDef['type'] = 'http://www.w3.org/2001/XMLSchema:string';
751
$this->xdebug("in getTypeDef, did not find $type");
756
* returns a sample serialization of a given type, or false if no type by the given name
758
* @param string $type name of type
763
function serializeTypeDef($type){
764
//print "in sTD() for type $type<br>";
765
if($typeDef = $this->getTypeDef($type)){
767
if(is_array($typeDef['attrs'])){
768
foreach($typeDef['attrs'] as $attName => $data){
769
$str .= " $attName=\"{type = ".$data['type']."}\"";
772
$str .= " xmlns=\"".$this->schema['targetNamespace']."\"";
773
if(count($typeDef['elements']) > 0){
775
foreach($typeDef['elements'] as $element => $eData){
776
$str .= $this->serializeTypeDef($element);
779
} elseif($typeDef['typeClass'] == 'element') {
790
* returns HTML form elements that allow a user
791
* to enter values for creating an instance of the given type.
793
* @param string $name name for type instance
794
* @param string $type name of type
799
function typeToForm($name,$type){
801
if($typeDef = $this->getTypeDef($type)){
803
if($typeDef['phpType'] == 'struct'){
804
$buffer .= '<table>';
805
foreach($typeDef['elements'] as $child => $childDef){
807
<tr><td align='right'>$childDef[name] (type: ".$this->getLocalPart($childDef['type'])."):</td>
808
<td><input type='text' name='parameters[".$name."][$childDef[name]]'></td></tr>";
810
$buffer .= '</table>';
812
} elseif($typeDef['phpType'] == 'array'){
813
$buffer .= '<table>';
814
for($i=0;$i < 3; $i++){
816
<tr><td align='right'>array item (type: $typeDef[arrayType]):</td>
817
<td><input type='text' name='parameters[".$name."][]'></td></tr>";
819
$buffer .= '</table>';
822
$buffer .= "<input type='text' name='parameters[$name]'>";
825
$buffer .= "<input type='text' name='parameters[$name]'>";
831
* adds a complex type to the schema
841
* array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'string[]'),
845
* example: PHP associative array ( SOAP Struct )
852
* array('myVar'=> array('name'=>'myVar','type'=>'string')
856
* @param typeClass (complexType|simpleType|attribute)
857
* @param phpType: currently supported are array and struct (php assoc array)
858
* @param compositor (all|sequence|choice)
859
* @param restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
860
* @param elements = array ( name = array(name=>'',type=>'') )
861
* @param attrs = array(
863
* 'ref' => "http://schemas.xmlsoap.org/soap/encoding/:arrayType",
864
* "http://schemas.xmlsoap.org/wsdl/:arrayType" => "string[]"
867
* @param arrayType: namespace:name (http://www.w3.org/2001/XMLSchema:string)
871
function addComplexType($name,$typeClass='complexType',$phpType='array',$compositor='',$restrictionBase='',$elements=array(),$attrs=array(),$arrayType=''){
872
$this->complexTypes[$name] = array(
874
'typeClass' => $typeClass,
875
'phpType' => $phpType,
876
'compositor'=> $compositor,
877
'restrictionBase' => $restrictionBase,
878
'elements' => $elements,
880
'arrayType' => $arrayType
883
$this->xdebug("addComplexType $name:");
884
$this->appendDebug($this->varDump($this->complexTypes[$name]));
888
* adds a simple type to the schema
890
* @param string $name
891
* @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
892
* @param string $typeClass (should always be simpleType)
893
* @param string $phpType (should always be scalar)
894
* @param array $enumeration array of values
896
* @see nusoap_xmlschema
899
function addSimpleType($name, $restrictionBase='', $typeClass='simpleType', $phpType='scalar', $enumeration=array()) {
900
$this->simpleTypes[$name] = array(
902
'typeClass' => $typeClass,
903
'phpType' => $phpType,
904
'type' => $restrictionBase,
905
'enumeration' => $enumeration
908
$this->xdebug("addSimpleType $name:");
909
$this->appendDebug($this->varDump($this->simpleTypes[$name]));
913
* adds an element to the schema
915
* @param array $attrs attributes that must include name and type
916
* @see nusoap_xmlschema
919
function addElement($attrs) {
920
if (! $this->getPrefix($attrs['type'])) {
921
$attrs['type'] = $this->schemaTargetNamespace . ':' . $attrs['type'];
923
$this->elements[ $attrs['name'] ] = $attrs;
924
$this->elements[ $attrs['name'] ]['typeClass'] = 'element';
926
$this->xdebug("addElement " . $attrs['name']);
927
$this->appendDebug($this->varDump($this->elements[ $attrs['name'] ]));
932
* Backward compatibility
934
class XMLSchema extends nusoap_xmlschema {
7
* parses an XML Schema, allows access to it's data, other utility methods.
8
* imperfect, no validation... yet, but quite functional.
10
* @author Dietrich Ayala <dietrich@ganx4.com>
11
* @author Scott Nichol <snichol@users.sourceforge.net>
12
* @version $Id: class.xmlschema.php,v 1.49 2007/11/06 14:17:53 snichol Exp $
15
class nusoap_xmlschema extends nusoap_base {
21
var $enclosingNamespaces;
23
var $schemaInfo = array();
24
var $schemaTargetNamespace = '';
25
// types, elements, attributes defined by the schema
26
var $attributes = array();
27
var $complexTypes = array();
28
var $complexTypeStack = array();
29
var $currentComplexType = null;
30
var $elements = array();
31
var $elementStack = array();
32
var $currentElement = null;
33
var $simpleTypes = array();
34
var $simpleTypeStack = array();
35
var $currentSimpleType = null;
37
var $imports = array();
42
var $depth_array = array();
43
var $message = array();
44
var $defaultNamespace = array();
49
* @param string $schema schema document URI
50
* @param string $xml xml document URI
51
* @param string $namespaces namespaces defined in enclosing XML
54
function nusoap_xmlschema($schema='',$xml='',$namespaces=array()){
55
parent::nusoap_base();
56
$this->debug('nusoap_xmlschema class instantiated, inside constructor');
58
$this->schema = $schema;
62
$this->enclosingNamespaces = $namespaces;
63
$this->namespaces = array_merge($this->namespaces, $namespaces);
67
$this->debug('initial schema file: '.$schema);
68
$this->parseFile($schema, 'schema');
73
$this->debug('initial xml file: '.$xml);
74
$this->parseFile($xml, 'xml');
82
* @param string $xml path/URL to XML file
83
* @param string $type (schema | xml)
87
function parseFile($xml,$type){
90
$xmlStr = @join("",@file($xml));
92
$msg = 'Error reading XML from '.$xml;
93
$this->setError($msg);
97
$this->debug("parsing $xml");
98
$this->parseString($xmlStr,$type);
99
$this->debug("done parsing $xml");
107
* parse an XML string
109
* @param string $xml path or URL
110
* @param string $type (schema|xml)
113
function parseString($xml,$type){
117
// Create an XML parser.
118
$this->parser = xml_parser_create();
119
// Set the options for parsing the XML data.
120
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
122
// Set the object for the parser.
123
xml_set_object($this->parser, $this);
125
// Set the element handlers for the parser.
126
if($type == "schema"){
127
xml_set_element_handler($this->parser, 'schemaStartElement','schemaEndElement');
128
xml_set_character_data_handler($this->parser,'schemaCharacterData');
129
} elseif($type == "xml"){
130
xml_set_element_handler($this->parser, 'xmlStartElement','xmlEndElement');
131
xml_set_character_data_handler($this->parser,'xmlCharacterData');
134
// Parse the XML file.
135
if(!xml_parse($this->parser,$xml,true)){
136
// Display an error message.
137
$errstr = sprintf('XML error parsing XML schema on line %d: %s',
138
xml_get_current_line_number($this->parser),
139
xml_error_string(xml_get_error_code($this->parser))
141
$this->debug($errstr);
142
$this->debug("XML payload:\n" . $xml);
143
$this->setError($errstr);
146
xml_parser_free($this->parser);
148
$this->debug('no xml passed to parseString()!!');
149
$this->setError('no xml passed to parseString()!!');
154
* gets a type name for an unnamed type
156
* @param string Element name
157
* @return string A type name for an unnamed type
160
function CreateTypeName($ename) {
162
for ($i = 0; $i < count($this->complexTypeStack); $i++) {
163
$scope .= $this->complexTypeStack[$i] . '_';
165
return $scope . $ename . '_ContainedType';
169
* start-element handler
171
* @param string $parser XML parser object
172
* @param string $name element name
173
* @param string $attrs associative array of attributes
176
function schemaStartElement($parser, $name, $attrs) {
178
// position in the total number of elements, starting from 0
179
$pos = $this->position++;
180
$depth = $this->depth++;
181
// set self as current value for this depth
182
$this->depth_array[$depth] = $pos;
183
$this->message[$pos] = array('cdata' => '');
185
$this->defaultNamespace[$pos] = $this->defaultNamespace[$this->depth_array[$depth - 1]];
187
$this->defaultNamespace[$pos] = false;
190
// get element prefix
191
if($prefix = $this->getPrefix($name)){
192
// get unqualified name
193
$name = $this->getLocalPart($name);
198
// loop thru attributes, expanding, and registering namespace declarations
199
if(count($attrs) > 0){
200
foreach($attrs as $k => $v){
201
// if ns declarations, add to class level array of valid namespaces
202
if(ereg("^xmlns",$k)){
203
//$this->xdebug("$k: $v");
204
//$this->xdebug('ns_prefix: '.$this->getPrefix($k));
205
if($ns_prefix = substr(strrchr($k,':'),1)){
206
//$this->xdebug("Add namespace[$ns_prefix] = $v");
207
$this->namespaces[$ns_prefix] = $v;
209
$this->defaultNamespace[$pos] = $v;
210
if (! $this->getPrefixFromNamespace($v)) {
211
$this->namespaces['ns'.(count($this->namespaces)+1)] = $v;
214
if($v == 'http://www.w3.org/2001/XMLSchema' || $v == 'http://www.w3.org/1999/XMLSchema' || $v == 'http://www.w3.org/2000/10/XMLSchema'){
215
$this->XMLSchemaVersion = $v;
216
$this->namespaces['xsi'] = $v.'-instance';
220
foreach($attrs as $k => $v){
221
// expand each attribute
222
$k = strpos($k,':') ? $this->expandQname($k) : $k;
223
$v = strpos($v,':') ? $this->expandQname($v) : $v;
230
// find status, register data
232
case 'all': // (optional) compositor content for a complexType
236
//$this->xdebug("compositor $name for currentComplexType: $this->currentComplexType and currentElement: $this->currentElement");
237
$this->complexTypes[$this->currentComplexType]['compositor'] = $name;
238
//if($name == 'all' || $name == 'sequence'){
239
// $this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
242
case 'attribute': // complexType attribute
243
//$this->xdebug("parsing attribute $attrs[name] $attrs[ref] of value: ".$attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']);
244
$this->xdebug("parsing attribute:");
245
$this->appendDebug($this->varDump($attrs));
246
if (!isset($attrs['form'])) {
247
$attrs['form'] = $this->schemaInfo['attributeFormDefault'];
249
if (isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) {
250
$v = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
251
if (!strpos($v, ':')) {
252
// no namespace in arrayType attribute value...
253
if ($this->defaultNamespace[$pos]) {
254
// ...so use the default
255
$attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'] = $this->defaultNamespace[$pos] . ':' . $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
259
if(isset($attrs['name'])){
260
$this->attributes[$attrs['name']] = $attrs;
261
$aname = $attrs['name'];
262
} elseif(isset($attrs['ref']) && $attrs['ref'] == 'http://schemas.xmlsoap.org/soap/encoding/:arrayType'){
263
if (isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])) {
264
$aname = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
268
} elseif(isset($attrs['ref'])){
269
$aname = $attrs['ref'];
270
$this->attributes[$attrs['ref']] = $attrs;
273
if($this->currentComplexType){ // This should *always* be
274
$this->complexTypes[$this->currentComplexType]['attrs'][$aname] = $attrs;
276
// arrayType attribute
277
if(isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType']) || $this->getLocalPart($aname) == 'arrayType'){
278
$this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
279
$prefix = $this->getPrefix($aname);
280
if(isset($attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'])){
281
$v = $attrs['http://schemas.xmlsoap.org/wsdl/:arrayType'];
285
if(strpos($v,'[,]')){
286
$this->complexTypes[$this->currentComplexType]['multidimensional'] = true;
288
$v = substr($v,0,strpos($v,'[')); // clip the []
289
if(!strpos($v,':') && isset($this->typemap[$this->XMLSchemaVersion][$v])){
290
$v = $this->XMLSchemaVersion.':'.$v;
292
$this->complexTypes[$this->currentComplexType]['arrayType'] = $v;
295
case 'complexContent': // (optional) content for a complexType
298
array_push($this->complexTypeStack, $this->currentComplexType);
299
if(isset($attrs['name'])){
300
// TODO: what is the scope of named complexTypes that appear
301
// nested within other c complexTypes?
302
$this->xdebug('processing named complexType '.$attrs['name']);
303
//$this->currentElement = false;
304
$this->currentComplexType = $attrs['name'];
305
$this->complexTypes[$this->currentComplexType] = $attrs;
306
$this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType';
307
// This is for constructs like
308
// <complexType name="ListOfString" base="soap:Array">
310
// <element name="string" type="xsd:string"
311
// minOccurs="0" maxOccurs="unbounded" />
314
if(isset($attrs['base']) && ereg(':Array$',$attrs['base'])){
315
$this->xdebug('complexType is unusual array');
316
$this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
318
$this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
321
$name = $this->CreateTypeName($this->currentElement);
322
$this->xdebug('processing unnamed complexType for element ' . $this->currentElement . ' named ' . $name);
323
$this->currentComplexType = $name;
324
//$this->currentElement = false;
325
$this->complexTypes[$this->currentComplexType] = $attrs;
326
$this->complexTypes[$this->currentComplexType]['typeClass'] = 'complexType';
327
// This is for constructs like
328
// <complexType name="ListOfString" base="soap:Array">
330
// <element name="string" type="xsd:string"
331
// minOccurs="0" maxOccurs="unbounded" />
334
if(isset($attrs['base']) && ereg(':Array$',$attrs['base'])){
335
$this->xdebug('complexType is unusual array');
336
$this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
338
$this->complexTypes[$this->currentComplexType]['phpType'] = 'struct';
343
array_push($this->elementStack, $this->currentElement);
344
if (!isset($attrs['form'])) {
345
$attrs['form'] = $this->schemaInfo['elementFormDefault'];
347
if(isset($attrs['type'])){
348
$this->xdebug("processing typed element ".$attrs['name']." of type ".$attrs['type']);
349
if (! $this->getPrefix($attrs['type'])) {
350
if ($this->defaultNamespace[$pos]) {
351
$attrs['type'] = $this->defaultNamespace[$pos] . ':' . $attrs['type'];
352
$this->xdebug('used default namespace to make type ' . $attrs['type']);
355
// This is for constructs like
356
// <complexType name="ListOfString" base="soap:Array">
358
// <element name="string" type="xsd:string"
359
// minOccurs="0" maxOccurs="unbounded" />
362
if ($this->currentComplexType && $this->complexTypes[$this->currentComplexType]['phpType'] == 'array') {
363
$this->xdebug('arrayType for unusual array is ' . $attrs['type']);
364
$this->complexTypes[$this->currentComplexType]['arrayType'] = $attrs['type'];
366
$this->currentElement = $attrs['name'];
367
$ename = $attrs['name'];
368
} elseif(isset($attrs['ref'])){
369
$this->xdebug("processing element as ref to ".$attrs['ref']);
370
$this->currentElement = "ref to ".$attrs['ref'];
371
$ename = $this->getLocalPart($attrs['ref']);
373
$type = $this->CreateTypeName($this->currentComplexType . '_' . $attrs['name']);
374
$this->xdebug("processing untyped element " . $attrs['name'] . ' type ' . $type);
375
$this->currentElement = $attrs['name'];
376
$attrs['type'] = $this->schemaTargetNamespace . ':' . $type;
377
$ename = $attrs['name'];
379
if (isset($ename) && $this->currentComplexType) {
380
$this->xdebug("add element $ename to complexType $this->currentComplexType");
381
$this->complexTypes[$this->currentComplexType]['elements'][$ename] = $attrs;
382
} elseif (!isset($attrs['ref'])) {
383
$this->xdebug("add element $ename to elements array");
384
$this->elements[ $attrs['name'] ] = $attrs;
385
$this->elements[ $attrs['name'] ]['typeClass'] = 'element';
388
case 'enumeration': // restriction value list member
389
$this->xdebug('enumeration ' . $attrs['value']);
390
if ($this->currentSimpleType) {
391
$this->simpleTypes[$this->currentSimpleType]['enumeration'][] = $attrs['value'];
392
} elseif ($this->currentComplexType) {
393
$this->complexTypes[$this->currentComplexType]['enumeration'][] = $attrs['value'];
396
case 'extension': // simpleContent or complexContent type extension
397
$this->xdebug('extension ' . $attrs['base']);
398
if ($this->currentComplexType) {
399
$this->complexTypes[$this->currentComplexType]['extensionBase'] = $attrs['base'];
403
if (isset($attrs['schemaLocation'])) {
404
//$this->xdebug('import namespace ' . $attrs['namespace'] . ' from ' . $attrs['schemaLocation']);
405
$this->imports[$attrs['namespace']][] = array('location' => $attrs['schemaLocation'], 'loaded' => false);
407
//$this->xdebug('import namespace ' . $attrs['namespace']);
408
$this->imports[$attrs['namespace']][] = array('location' => '', 'loaded' => true);
409
if (! $this->getPrefixFromNamespace($attrs['namespace'])) {
410
$this->namespaces['ns'.(count($this->namespaces)+1)] = $attrs['namespace'];
414
case 'list': // simpleType value list
416
case 'restriction': // simpleType, simpleContent or complexContent value restriction
417
$this->xdebug('restriction ' . $attrs['base']);
418
if($this->currentSimpleType){
419
$this->simpleTypes[$this->currentSimpleType]['type'] = $attrs['base'];
420
} elseif($this->currentComplexType){
421
$this->complexTypes[$this->currentComplexType]['restrictionBase'] = $attrs['base'];
422
if(strstr($attrs['base'],':') == ':Array'){
423
$this->complexTypes[$this->currentComplexType]['phpType'] = 'array';
428
$this->schemaInfo = $attrs;
429
$this->schemaInfo['schemaVersion'] = $this->getNamespaceFromPrefix($prefix);
430
if (isset($attrs['targetNamespace'])) {
431
$this->schemaTargetNamespace = $attrs['targetNamespace'];
433
if (!isset($attrs['elementFormDefault'])) {
434
$this->schemaInfo['elementFormDefault'] = 'unqualified';
436
if (!isset($attrs['attributeFormDefault'])) {
437
$this->schemaInfo['attributeFormDefault'] = 'unqualified';
440
case 'simpleContent': // (optional) content for a complexType
443
array_push($this->simpleTypeStack, $this->currentSimpleType);
444
if(isset($attrs['name'])){
445
$this->xdebug("processing simpleType for name " . $attrs['name']);
446
$this->currentSimpleType = $attrs['name'];
447
$this->simpleTypes[ $attrs['name'] ] = $attrs;
448
$this->simpleTypes[ $attrs['name'] ]['typeClass'] = 'simpleType';
449
$this->simpleTypes[ $attrs['name'] ]['phpType'] = 'scalar';
451
$name = $this->CreateTypeName($this->currentComplexType . '_' . $this->currentElement);
452
$this->xdebug('processing unnamed simpleType for element ' . $this->currentElement . ' named ' . $name);
453
$this->currentSimpleType = $name;
454
//$this->currentElement = false;
455
$this->simpleTypes[$this->currentSimpleType] = $attrs;
456
$this->simpleTypes[$this->currentSimpleType]['phpType'] = 'scalar';
459
case 'union': // simpleType type list
462
//$this->xdebug("do not have anything to do for element $name");
467
* end-element handler
469
* @param string $parser XML parser object
470
* @param string $name element name
473
function schemaEndElement($parser, $name) {
474
// bring depth down a notch
476
// position of current element is equal to the last value left in depth_array for my depth
477
if(isset($this->depth_array[$this->depth])){
478
$pos = $this->depth_array[$this->depth];
480
// get element prefix
481
if ($prefix = $this->getPrefix($name)){
482
// get unqualified name
483
$name = $this->getLocalPart($name);
488
if($name == 'complexType'){
489
$this->xdebug('done processing complexType ' . ($this->currentComplexType ? $this->currentComplexType : '(unknown)'));
490
$this->currentComplexType = array_pop($this->complexTypeStack);
491
//$this->currentElement = false;
493
if($name == 'element'){
494
$this->xdebug('done processing element ' . ($this->currentElement ? $this->currentElement : '(unknown)'));
495
$this->currentElement = array_pop($this->elementStack);
497
if($name == 'simpleType'){
498
$this->xdebug('done processing simpleType ' . ($this->currentSimpleType ? $this->currentSimpleType : '(unknown)'));
499
$this->currentSimpleType = array_pop($this->simpleTypeStack);
504
* element content handler
506
* @param string $parser XML parser object
507
* @param string $data element content
510
function schemaCharacterData($parser, $data){
511
$pos = $this->depth_array[$this->depth - 1];
512
$this->message[$pos]['cdata'] .= $data;
516
* serialize the schema
520
function serializeSchema(){
522
$schemaPrefix = $this->getPrefixFromNamespace($this->XMLSchemaVersion);
525
if (sizeof($this->imports) > 0) {
526
foreach($this->imports as $ns => $list) {
527
foreach ($list as $ii) {
528
if ($ii['location'] != '') {
529
$xml .= " <$schemaPrefix:import location=\"" . $ii['location'] . '" namespace="' . $ns . "\" />\n";
531
$xml .= " <$schemaPrefix:import namespace=\"" . $ns . "\" />\n";
537
foreach($this->complexTypes as $typeName => $attrs){
539
// serialize child elements
540
if(isset($attrs['elements']) && (count($attrs['elements']) > 0)){
541
foreach($attrs['elements'] as $element => $eParts){
542
if(isset($eParts['ref'])){
543
$contentStr .= " <$schemaPrefix:element ref=\"$element\"/>\n";
545
$contentStr .= " <$schemaPrefix:element name=\"$element\" type=\"" . $this->contractQName($eParts['type']) . "\"";
546
foreach ($eParts as $aName => $aValue) {
547
// handle, e.g., abstract, default, form, minOccurs, maxOccurs, nillable
548
if ($aName != 'name' && $aName != 'type') {
549
$contentStr .= " $aName=\"$aValue\"";
552
$contentStr .= "/>\n";
555
// compositor wraps elements
556
if (isset($attrs['compositor']) && ($attrs['compositor'] != '')) {
557
$contentStr = " <$schemaPrefix:$attrs[compositor]>\n".$contentStr." </$schemaPrefix:$attrs[compositor]>\n";
561
if(isset($attrs['attrs']) && (count($attrs['attrs']) >= 1)){
562
foreach($attrs['attrs'] as $attr => $aParts){
563
$contentStr .= " <$schemaPrefix:attribute";
564
foreach ($aParts as $a => $v) {
565
if ($a == 'ref' || $a == 'type') {
566
$contentStr .= " $a=\"".$this->contractQName($v).'"';
567
} elseif ($a == 'http://schemas.xmlsoap.org/wsdl/:arrayType') {
568
$this->usedNamespaces['wsdl'] = $this->namespaces['wsdl'];
569
$contentStr .= ' wsdl:arrayType="'.$this->contractQName($v).'"';
571
$contentStr .= " $a=\"$v\"";
574
$contentStr .= "/>\n";
578
if (isset($attrs['restrictionBase']) && $attrs['restrictionBase'] != ''){
579
$contentStr = " <$schemaPrefix:restriction base=\"".$this->contractQName($attrs['restrictionBase'])."\">\n".$contentStr." </$schemaPrefix:restriction>\n";
580
// complex or simple content
581
if ((isset($attrs['elements']) && count($attrs['elements']) > 0) || (isset($attrs['attrs']) && count($attrs['attrs']) > 0)){
582
$contentStr = " <$schemaPrefix:complexContent>\n".$contentStr." </$schemaPrefix:complexContent>\n";
585
// finalize complex type
586
if($contentStr != ''){
587
$contentStr = " <$schemaPrefix:complexType name=\"$typeName\">\n".$contentStr." </$schemaPrefix:complexType>\n";
589
$contentStr = " <$schemaPrefix:complexType name=\"$typeName\"/>\n";
594
if(isset($this->simpleTypes) && count($this->simpleTypes) > 0){
595
foreach($this->simpleTypes as $typeName => $eParts){
596
$xml .= " <$schemaPrefix:simpleType name=\"$typeName\">\n <$schemaPrefix:restriction base=\"".$this->contractQName($eParts['type'])."\">\n";
597
if (isset($eParts['enumeration'])) {
598
foreach ($eParts['enumeration'] as $e) {
599
$xml .= " <$schemaPrefix:enumeration value=\"$e\"/>\n";
602
$xml .= " </$schemaPrefix:restriction>\n </$schemaPrefix:simpleType>";
606
if(isset($this->elements) && count($this->elements) > 0){
607
foreach($this->elements as $element => $eParts){
608
$xml .= " <$schemaPrefix:element name=\"$element\" type=\"".$this->contractQName($eParts['type'])."\"/>\n";
612
if(isset($this->attributes) && count($this->attributes) > 0){
613
foreach($this->attributes as $attr => $aParts){
614
$xml .= " <$schemaPrefix:attribute name=\"$attr\" type=\"".$this->contractQName($aParts['type'])."\"\n/>";
619
foreach ($this->schemaInfo as $k => $v) {
620
if ($k == 'elementFormDefault' || $k == 'attributeFormDefault') {
621
$attr .= " $k=\"$v\"";
624
$el = "<$schemaPrefix:schema$attr targetNamespace=\"$this->schemaTargetNamespace\"\n";
625
foreach (array_diff($this->usedNamespaces, $this->enclosingNamespaces) as $nsp => $ns) {
626
$el .= " xmlns:$nsp=\"$ns\"";
628
$xml = $el . ">\n".$xml."</$schemaPrefix:schema>\n";
633
* adds debug data to the clas level debug string
635
* @param string $string debug data
638
function xdebug($string){
639
$this->debug('<' . $this->schemaTargetNamespace . '> '.$string);
643
* get the PHP type of a user defined type in the schema
644
* PHP type is kind of a misnomer since it actually returns 'struct' for assoc. arrays
645
* returns false if no type exists, or not w/ the given namespace
646
* else returns a string that is either a native php type, or 'struct'
648
* @param string $type name of defined type
649
* @param string $ns namespace of type
654
function getPHPType($type,$ns){
655
if(isset($this->typemap[$ns][$type])){
656
//print "found type '$type' and ns $ns in typemap<br>";
657
return $this->typemap[$ns][$type];
658
} elseif(isset($this->complexTypes[$type])){
659
//print "getting type '$type' and ns $ns from complexTypes array<br>";
660
return $this->complexTypes[$type]['phpType'];
666
* returns an associative array of information about a given type
667
* returns false if no type exists by the given name
669
* For a complexType typeDef = array(
670
* 'restrictionBase' => '',
672
* 'compositor' => '(sequence|all)',
673
* 'elements' => array(), // refs to elements array
674
* 'attrs' => array() // refs to attributes array
675
* ... and so on (see addComplexType)
678
* For simpleType or element, the array has different keys.
680
* @param string $type
683
* @see addComplexType
687
function getTypeDef($type){
688
//$this->debug("in getTypeDef for type $type");
689
if (substr($type, -1) == '^') {
691
$type = substr($type, 0, -1);
696
if((! $is_element) && isset($this->complexTypes[$type])){
697
$this->xdebug("in getTypeDef, found complexType $type");
698
return $this->complexTypes[$type];
699
} elseif((! $is_element) && isset($this->simpleTypes[$type])){
700
$this->xdebug("in getTypeDef, found simpleType $type");
701
if (!isset($this->simpleTypes[$type]['phpType'])) {
702
// get info for type to tack onto the simple type
703
// TODO: can this ever really apply (i.e. what is a simpleType really?)
704
$uqType = substr($this->simpleTypes[$type]['type'], strrpos($this->simpleTypes[$type]['type'], ':') + 1);
705
$ns = substr($this->simpleTypes[$type]['type'], 0, strrpos($this->simpleTypes[$type]['type'], ':'));
706
$etype = $this->getTypeDef($uqType);
708
$this->xdebug("in getTypeDef, found type for simpleType $type:");
709
$this->xdebug($this->varDump($etype));
710
if (isset($etype['phpType'])) {
711
$this->simpleTypes[$type]['phpType'] = $etype['phpType'];
713
if (isset($etype['elements'])) {
714
$this->simpleTypes[$type]['elements'] = $etype['elements'];
718
return $this->simpleTypes[$type];
719
} elseif(isset($this->elements[$type])){
720
$this->xdebug("in getTypeDef, found element $type");
721
if (!isset($this->elements[$type]['phpType'])) {
722
// get info for type to tack onto the element
723
$uqType = substr($this->elements[$type]['type'], strrpos($this->elements[$type]['type'], ':') + 1);
724
$ns = substr($this->elements[$type]['type'], 0, strrpos($this->elements[$type]['type'], ':'));
725
$etype = $this->getTypeDef($uqType);
727
$this->xdebug("in getTypeDef, found type for element $type:");
728
$this->xdebug($this->varDump($etype));
729
if (isset($etype['phpType'])) {
730
$this->elements[$type]['phpType'] = $etype['phpType'];
732
if (isset($etype['elements'])) {
733
$this->elements[$type]['elements'] = $etype['elements'];
735
} elseif ($ns == 'http://www.w3.org/2001/XMLSchema') {
736
$this->xdebug("in getTypeDef, element $type is an XSD type");
737
$this->elements[$type]['phpType'] = 'scalar';
740
return $this->elements[$type];
741
} elseif(isset($this->attributes[$type])){
742
$this->xdebug("in getTypeDef, found attribute $type");
743
return $this->attributes[$type];
744
} elseif (ereg('_ContainedType$', $type)) {
745
$this->xdebug("in getTypeDef, have an untyped element $type");
746
$typeDef['typeClass'] = 'simpleType';
747
$typeDef['phpType'] = 'scalar';
748
$typeDef['type'] = 'http://www.w3.org/2001/XMLSchema:string';
751
$this->xdebug("in getTypeDef, did not find $type");
756
* returns a sample serialization of a given type, or false if no type by the given name
758
* @param string $type name of type
763
function serializeTypeDef($type){
764
//print "in sTD() for type $type<br>";
765
if($typeDef = $this->getTypeDef($type)){
767
if(is_array($typeDef['attrs'])){
768
foreach($typeDef['attrs'] as $attName => $data){
769
$str .= " $attName=\"{type = ".$data['type']."}\"";
772
$str .= " xmlns=\"".$this->schema['targetNamespace']."\"";
773
if(count($typeDef['elements']) > 0){
775
foreach($typeDef['elements'] as $element => $eData){
776
$str .= $this->serializeTypeDef($element);
779
} elseif($typeDef['typeClass'] == 'element') {
790
* returns HTML form elements that allow a user
791
* to enter values for creating an instance of the given type.
793
* @param string $name name for type instance
794
* @param string $type name of type
799
function typeToForm($name,$type){
801
if($typeDef = $this->getTypeDef($type)){
803
if($typeDef['phpType'] == 'struct'){
804
$buffer .= '<table>';
805
foreach($typeDef['elements'] as $child => $childDef){
807
<tr><td align='right'>$childDef[name] (type: ".$this->getLocalPart($childDef['type'])."):</td>
808
<td><input type='text' name='parameters[".$name."][$childDef[name]]'></td></tr>";
810
$buffer .= '</table>';
812
} elseif($typeDef['phpType'] == 'array'){
813
$buffer .= '<table>';
814
for($i=0;$i < 3; $i++){
816
<tr><td align='right'>array item (type: $typeDef[arrayType]):</td>
817
<td><input type='text' name='parameters[".$name."][]'></td></tr>";
819
$buffer .= '</table>';
822
$buffer .= "<input type='text' name='parameters[$name]'>";
825
$buffer .= "<input type='text' name='parameters[$name]'>";
831
* adds a complex type to the schema
841
* array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'string[]'),
845
* example: PHP associative array ( SOAP Struct )
852
* array('myVar'=> array('name'=>'myVar','type'=>'string')
856
* @param typeClass (complexType|simpleType|attribute)
857
* @param phpType: currently supported are array and struct (php assoc array)
858
* @param compositor (all|sequence|choice)
859
* @param restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
860
* @param elements = array ( name = array(name=>'',type=>'') )
861
* @param attrs = array(
863
* 'ref' => "http://schemas.xmlsoap.org/soap/encoding/:arrayType",
864
* "http://schemas.xmlsoap.org/wsdl/:arrayType" => "string[]"
867
* @param arrayType: namespace:name (http://www.w3.org/2001/XMLSchema:string)
871
function addComplexType($name,$typeClass='complexType',$phpType='array',$compositor='',$restrictionBase='',$elements=array(),$attrs=array(),$arrayType=''){
872
$this->complexTypes[$name] = array(
874
'typeClass' => $typeClass,
875
'phpType' => $phpType,
876
'compositor'=> $compositor,
877
'restrictionBase' => $restrictionBase,
878
'elements' => $elements,
880
'arrayType' => $arrayType
883
$this->xdebug("addComplexType $name:");
884
$this->appendDebug($this->varDump($this->complexTypes[$name]));
888
* adds a simple type to the schema
890
* @param string $name
891
* @param string $restrictionBase namespace:name (http://schemas.xmlsoap.org/soap/encoding/:Array)
892
* @param string $typeClass (should always be simpleType)
893
* @param string $phpType (should always be scalar)
894
* @param array $enumeration array of values
896
* @see nusoap_xmlschema
899
function addSimpleType($name, $restrictionBase='', $typeClass='simpleType', $phpType='scalar', $enumeration=array()) {
900
$this->simpleTypes[$name] = array(
902
'typeClass' => $typeClass,
903
'phpType' => $phpType,
904
'type' => $restrictionBase,
905
'enumeration' => $enumeration
908
$this->xdebug("addSimpleType $name:");
909
$this->appendDebug($this->varDump($this->simpleTypes[$name]));
913
* adds an element to the schema
915
* @param array $attrs attributes that must include name and type
916
* @see nusoap_xmlschema
919
function addElement($attrs) {
920
if (! $this->getPrefix($attrs['type'])) {
921
$attrs['type'] = $this->schemaTargetNamespace . ':' . $attrs['type'];
923
$this->elements[ $attrs['name'] ] = $attrs;
924
$this->elements[ $attrs['name'] ]['typeClass'] = 'element';
926
$this->xdebug("addElement " . $attrs['name']);
927
$this->appendDebug($this->varDump($this->elements[ $attrs['name'] ]));
932
* Backward compatibility
934
class XMLSchema extends nusoap_xmlschema {
b'\\ No newline at end of file'