Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed default namespace not working correctly #87

Merged
merged 4 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 14 additions & 29 deletions src/SchemaReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -1032,11 +1032,17 @@ private static function splitParts(\DOMElement $node, string $typeName): array
[$prefix, $name] = explode(':', $typeName);
}

/**
* @psalm-suppress PossiblyNullArgument
*/
// Get namespace URI for prefix. If prefix is null, it will return the default namespace
$namespace = $node->lookupNamespaceUri($prefix);

// 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.
// In this case, we should not throw an exception since this is valid xml.
if (!$namespace && $prefix !== null) {
throw new TypeException(sprintf("Can't find namespace for prefix '%s', at line %d in %s ", $prefix, $node->getLineNo(), $node->ownerDocument->documentURI));
}

return [
$name,
$namespace,
Expand All @@ -1048,11 +1054,6 @@ private function findAttributeItem(Schema $schema, \DOMElement $node, string $ty
{
[$name, $namespace] = self::splitParts($node, $typeName);

/**
* @var string|null $namespace
*/
$namespace = $namespace ?: $schema->getTargetNamespace();

try {
/**
* @var AttributeItem $out
Expand All @@ -1069,11 +1070,6 @@ private function findAttributeGroup(Schema $schema, \DOMElement $node, string $t
{
[$name, $namespace] = self::splitParts($node, $typeName);

/**
* @var string|null $namespace
*/
$namespace = $namespace ?: $schema->getTargetNamespace();

try {
/**
* @var AttributeGroup $out
Expand All @@ -1090,11 +1086,6 @@ private function findElement(Schema $schema, \DOMElement $node, string $typeName
{
[$name, $namespace] = self::splitParts($node, $typeName);

/**
* @var string|null $namespace
*/
$namespace = $namespace ?: $schema->getTargetNamespace();

try {
return $schema->findElement((string) $name, $namespace);
} catch (TypeNotFoundException $e) {
Expand All @@ -1106,11 +1097,6 @@ private function findGroup(Schema $schema, \DOMElement $node, string $typeName):
{
[$name, $namespace] = self::splitParts($node, $typeName);

/**
* @var string|null $namespace
*/
$namespace = $namespace ?: $schema->getTargetNamespace();

try {
/**
* @var Group $out
Expand All @@ -1127,11 +1113,6 @@ private function findType(Schema $schema, \DOMElement $node, string $typeName):
{
[$name, $namespace] = self::splitParts($node, $typeName);

/**
* @var string|null $namespace
*/
$namespace = $namespace ?: $schema->getTargetNamespace();

$tryFindType = static function (Schema $schema, string $name, ?string $namespace): ?SchemaItem {
try {
return $schema->findType($name, $namespace);
Expand Down Expand Up @@ -1199,13 +1180,17 @@ private function fillItemNonLocalType(Item $element, \DOMElement $node): void
*/
$type = $this->findSomeType($element, $node, 'type');
} else {
$prefix = $node->lookupPrefix(self::XSD_NS);
if ($prefix) {
$prefix .= ':';
}
/**
* @var Type
*/
$type = $this->findSomeTypeFromAttribute(
$element,
$node,
$node->lookupPrefix(self::XSD_NS) . ':anyType'
$prefix . 'anyType'
);
}

Expand Down
10 changes: 5 additions & 5 deletions tests/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testBase(): void
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://www.example.com" xmlns:ex="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attribute name="myAttribute" type="xs:string"></xs:attribute>
<xs:attribute name="myAttributeOptions" type="xs:string" use="required" nil="true"></xs:attribute>

Expand Down Expand Up @@ -101,22 +101,22 @@ public function testAttributeUseOverriding(): void
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://www.example.com" xmlns:ex="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:attribute name="lang" use="optional" type="xs:language"/>
<xs:element name="Name">
<xs:complexType mixed="true">
<xs:attribute ref="lang" use="required"/>
<xs:attribute ref="ex:lang" use="required"/>
</xs:complexType>
</xs:element>
<xs:complexType name="MyNameType">
<xs:sequence>
<xs:element ref="Name"/>
<xs:element ref="ex:Name"/>
</xs:sequence>
</xs:complexType>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="myName" type="MyNameType"/>
<xs:element name="myName" type="ex:MyNameType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Expand Down
60 changes: 30 additions & 30 deletions tests/ChoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ public function testChoiceSimple(): void
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://www.example.com" xmlns:ex="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Languages">
<xs:complexType>
<xs:choice>
<xs:element ref="german"/>
<xs:element ref="english"/>
<xs:element ref="spanish"/>
<xs:element ref="ex:german"/>
<xs:element ref="ex:english"/>
<xs:element ref="ex:spanish"/>
</xs:choice>
</xs:complexType>
</xs:element>
Expand All @@ -33,7 +33,7 @@ public function testChoiceSimple(): void
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="Languages"/>
<xs:element ref="ex:Languages"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Expand Down Expand Up @@ -61,14 +61,14 @@ public function testChoiceOptional(): void
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://www.example.com" xmlns:ex="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Languages">
<xs:complexType>
<xs:choice minOccurs="0">
<xs:element ref="german"/>
<xs:element ref="english"/>
<xs:element ref="spanish"/>
<xs:element ref="ex:german"/>
<xs:element ref="ex:english"/>
<xs:element ref="ex:spanish"/>
</xs:choice>
</xs:complexType>
</xs:element>
Expand All @@ -79,7 +79,7 @@ public function testChoiceOptional(): void
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="Languages"/>
<xs:element ref="ex:Languages"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Expand Down Expand Up @@ -107,14 +107,14 @@ public function testChoiceOptionalElement(): void
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://www.example.com" xmlns:ex="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Languages">
<xs:complexType>
<xs:choice>
<xs:element ref="german"/>
<xs:element ref="english"/>
<xs:element ref="spanish" minOccurs="0"/>
<xs:element ref="ex:german"/>
<xs:element ref="ex:english"/>
<xs:element ref="ex:spanish" minOccurs="0"/>
</xs:choice>
</xs:complexType>
</xs:element>
Expand All @@ -125,7 +125,7 @@ public function testChoiceOptionalElement(): void
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="Languages"/>
<xs:element ref="ex:Languages"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Expand Down Expand Up @@ -154,14 +154,14 @@ public function testChoiceUnbounded(): void
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://www.example.com" xmlns:ex="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Languages">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element ref="german"/>
<xs:element ref="english"/>
<xs:element ref="spanish"/>
<xs:element ref="ex:german"/>
<xs:element ref="ex:english"/>
<xs:element ref="ex:spanish"/>
</xs:choice>
</xs:complexType>
</xs:element>
Expand All @@ -172,7 +172,7 @@ public function testChoiceUnbounded(): void
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="Languages"/>
<xs:element ref="ex:Languages"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Expand Down Expand Up @@ -200,28 +200,28 @@ public function testChoiceNested(): void
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://www.example.com" xmlns:ex="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="LunchMenu">
<xs:complexType>
<xs:sequence>
<xs:choice minOccurs="0">
<xs:element ref="Soup1"/>
<xs:element ref="ex:Soup1"/>
<xs:choice>
<xs:element ref="Soup2"/>
<xs:element ref="Soup3"/>
<xs:element ref="ex:Soup2"/>
<xs:element ref="ex:Soup3"/>
</xs:choice>
</xs:choice>
<xs:choice>
<xs:element ref="MainMenu1"/>
<xs:element ref="ex:MainMenu1"/>
<xs:choice>
<xs:element ref="MainMenu2"/>
<xs:element ref="MainMenu3"/>
<xs:element ref="ex:MainMenu2"/>
<xs:element ref="ex:MainMenu3"/>
</xs:choice>
</xs:choice>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="Dessert1"/>
<xs:element ref="Dessert2"/>
<xs:element ref="ex:Dessert1"/>
<xs:element ref="ex:Dessert2"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
Expand All @@ -238,7 +238,7 @@ public function testChoiceNested(): void
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="LunchMenu"/>
<xs:element ref="ex:LunchMenu"/>
</xs:sequence>
</xs:complexType>
</xs:element>
Expand Down
22 changes: 11 additions & 11 deletions tests/ElementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testBase(): void
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://www.example.com" xmlns:ex="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="myElement" type="xs:string"></xs:element>

<xs:element name="myElementAnonType">
Expand Down Expand Up @@ -67,19 +67,19 @@ public function testGroupOccurrences($item, $min, $max): void
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://www.example.com" xmlns:ex="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="myType">
<xs:sequence>
<xs:group ref="myGroup" minOccurs="1" />
<xs:group ref="myGroup" minOccurs="2" />
<xs:group ref="ex:myGroup" minOccurs="1" />
<xs:group ref="ex:myGroup" minOccurs="2" />

<xs:group ref="myGroup" maxOccurs="1" />
<xs:group ref="myGroup" maxOccurs="unbounded" />
<xs:group ref="ex:myGroup" maxOccurs="1" />
<xs:group ref="ex:myGroup" maxOccurs="unbounded" />

<xs:group ref="myGroup" minOccurs="1" maxOccurs="1"/>
<xs:group ref="myGroup" minOccurs="2" maxOccurs="2"/>
<xs:group ref="ex:myGroup" minOccurs="1" maxOccurs="1"/>
<xs:group ref="ex:myGroup" minOccurs="2" maxOccurs="2"/>

<xs:group ref="myGroup" minOccurs="1" maxOccurs="unbounded"/>
<xs:group ref="ex:myGroup" minOccurs="1" maxOccurs="unbounded"/>

</xs:sequence>
</xs:complexType>
Expand Down Expand Up @@ -201,10 +201,10 @@ public function testGroupRefOccurrences(): void
{
$schema = $this->reader->readString(
'
<xs:schema targetNamespace="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:schema targetNamespace="http://www.example.com" xmlns:ex="http://www.example.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="myType">
<xs:sequence>
<xs:group ref="myGroup" />
<xs:group ref="ex:myGroup" />
</xs:sequence>
</xs:complexType>

Expand Down
Loading
Loading