Skip to content

Commit fa6226a

Browse files
author
Puraskar Sapkota
committed
Changed XSD parser to parse custom elements
1 parent 9949dd8 commit fa6226a

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

Generator/Parser/XsdParser.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ protected function addChildrenElements($parentNode, &$parentElement)
8282
$this->setComplexTypeChildrenRecursively($node, $element);
8383

8484
$parentElement->addChild($element);
85+
86+
} elseif ($this->isCustomTypeNode($node)) {
87+
$element = new Parser\Element\ComplexTypeElement();
88+
$element->setName($node->getAttribute('name'));
89+
$this->setCustomTypeChildrenRecursively($node, $element);
90+
91+
$parentElement->addChild($element);
92+
8593
} else {
8694
$element = new Parser\Element\Element();
8795
$element->setName($node->getAttribute('name'));
@@ -155,4 +163,98 @@ protected function setComplexTypeChildrenRecursively($node, &$element)
155163
}
156164
}
157165
}
166+
167+
168+
/**
169+
* Checks if the node is custom Type which is user Defined.
170+
* It is a Complex type but name given by user.
171+
*
172+
* If given type of the element is not ComplexType but there is root level element with that name,
173+
* it is supposed to be custom Node..
174+
*
175+
* Example of XML
176+
* <?xml version="1.0" encoding="UTF-8"?>
177+
* <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
178+
<xs:element name="oUm">
179+
<xs:annotation>
180+
<xs:documentation>This is an example of complex Element</xs:documentation>
181+
</xs:annotation>
182+
<xs:complexType>
183+
<xs:sequence>
184+
<xs:element name="id">
185+
<xs:annotation>
186+
<xs:documentation>Unique id</xs:documentation>
187+
</xs:annotation>
188+
<xs:simpleType>
189+
<xs:restriction base="xs:string">
190+
<xs:maxLength value="100"/>
191+
<xs:minLength value="1"/>
192+
</xs:restriction>
193+
</xs:simpleType>
194+
</xs:element>
195+
<xs:element name="billToAddress" type="addressType" minOccurs="0">
196+
<xs:annotation>
197+
<xs:documentation>Bill to address</xs:documentation>
198+
</xs:annotation>
199+
</xs:element>
200+
</xs:sequence>
201+
</xs:complexType>
202+
</xs:element>
203+
<xs:element name="address" type="addressType"/>
204+
<xs:complexType name="addressType">
205+
<xs:all>
206+
<xs:element name="title" type="xs:string" minOccurs="0">
207+
<xs:annotation>
208+
<xs:documentation>Customer title</xs:documentation>
209+
</xs:annotation>
210+
</xs:element>
211+
</xs:all>
212+
</xs:complexType>
213+
</xs:schema>
214+
*
215+
* @param DomNode $node
216+
*
217+
* @return boolean
218+
*/
219+
protected function isCustomTypeNode($node)
220+
{
221+
$nodes = $this->start->childNodes;
222+
223+
$nodeType = $node->nodeType;
224+
$typeAttribute = $node->getAttribute('type');
225+
226+
if ($nodeType === XML_ELEMENT_NODE) {
227+
if (!empty($typeAttribute)) {
228+
foreach ($nodes as $node) {
229+
//If any Node exists with the same name as Attribute Type defined for any element
230+
if (method_exists($node, 'getAttribute')) {
231+
if ($typeAttribute === $node->getAttribute('name')) {
232+
return true;
233+
}
234+
}
235+
}
236+
}
237+
}
238+
return false;
239+
}
240+
241+
/**
242+
* Sets Children of Custom Type Recursively
243+
* @param DomNode $node
244+
* @param Element $element
245+
*/
246+
protected function setCustomTypeChildrenRecursively($node, &$element)
247+
{
248+
$nodes = $this->start->childNodes;
249+
$typeAttribute = $node->getAttribute('type');
250+
251+
foreach ($nodes as $node) {
252+
//If any Node exists with the same name as Attribute Type defined for any element
253+
if (method_exists($node, 'getAttribute')) {
254+
if ($typeAttribute === $node->getAttribute('name')) {
255+
$this->addChildrenElements($node, $element);
256+
}
257+
}
258+
}
259+
}
158260
}

0 commit comments

Comments
 (0)