From 007c9f9c10f74cd79c6ec8c4a6641a91b3158be3 Mon Sep 17 00:00:00 2001 From: David Pommer <pommer@code8.de> Date: Sun, 23 Feb 2025 14:04:24 +0100 Subject: [PATCH 1/2] fixing finding types in default namespaces --- src/Schema/Schema.php | 2 +- src/SchemaReader.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Schema/Schema.php b/src/Schema/Schema.php index 1c6d22a..2a86dcf 100644 --- a/src/Schema/Schema.php +++ b/src/Schema/Schema.php @@ -32,7 +32,7 @@ protected function findSomethingNoThrow( return $this->typeCache[$cid]; } - if ($this->getTargetNamespace() === $namespace) { + if ($this->getTargetNamespace() === $namespace || $namespace === null) { /** * @var SchemaItem|null $item */ diff --git a/src/SchemaReader.php b/src/SchemaReader.php index 76dbf2d..d106275 100644 --- a/src/SchemaReader.php +++ b/src/SchemaReader.php @@ -1042,7 +1042,7 @@ private static function splitParts(\DOMElement $node, string $typeName): array // If no namespace is found, throw an exception only if a prefix was provided. // If no prefix was provided and the above lookup failed, this means that there - // was no defalut namespace defined, making the element part of no namespace. + // was no default namespace defined, making the element part of no namespace. // In this case, we should not throw an exception since this is valid xml. if (!$namespace && null !== $prefix) { throw new TypeException(sprintf("Can't find namespace for prefix '%s', at line %d in %s ", $prefix, $node->getLineNo(), $node->ownerDocument->documentURI)); From f0f62d5ce542ed040718bff48a5fef21d8cb3757 Mon Sep 17 00:00:00 2001 From: David Pommer <pommer@code8.de> Date: Sat, 22 Mar 2025 11:20:24 +0100 Subject: [PATCH 2/2] Adding unit test --- tests/ImportTest.php | 7 +++++++ tests/schema/schema.xsd | 8 ++++++++ tests/schema/schema2.xsd | 10 ++++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/schema/schema.xsd create mode 100644 tests/schema/schema2.xsd diff --git a/tests/ImportTest.php b/tests/ImportTest.php index 483d278..d9aac70 100644 --- a/tests/ImportTest.php +++ b/tests/ImportTest.php @@ -121,4 +121,11 @@ public function testDependentImport(): void self::assertInstanceOf(ElementDef::class, $schema->findElement('outerEl', 'http://tempuri.org/1')); } + + public function testDefaultNamespaceImport(): void + { + $schema = $this->reader->readfile(__DIR__ . '/schema/schema.xsd'); + + self::assertInstanceOf(Schema::class, $schema); + } } diff --git a/tests/schema/schema.xsd b/tests/schema/schema.xsd new file mode 100644 index 0000000..79abbbb --- /dev/null +++ b/tests/schema/schema.xsd @@ -0,0 +1,8 @@ +<xs:schema elementFormDefault="qualified" targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns2="http://www.example2.com"> + <xs:import namespace="http://www.example2.com" schemaLocation="schema2.xsd"/> + <xs:complexType name="complexType1"> + <xs:sequence> + <xs:element name="ElementList" type="ns2:ElementList"/> + </xs:sequence> + </xs:complexType> +</xs:schema> \ No newline at end of file diff --git a/tests/schema/schema2.xsd b/tests/schema/schema2.xsd new file mode 100644 index 0000000..29596b2 --- /dev/null +++ b/tests/schema/schema2.xsd @@ -0,0 +1,10 @@ +<xs:schema elementFormDefault="qualified" targetNamespace="http://www.example2.com" xmlns:xs="http://www.w3.org/2001/XMLSchema"> + <xs:complexType name="ElementList"> + <xs:sequence> + <xs:element maxOccurs="5" name="Element" type="Element"/> + </xs:sequence> + </xs:complexType> + <xs:complexType name="Element"> + <xs:attribute name="attribute" type="xs:string" /> + </xs:complexType> +</xs:schema>