7373}
7474XSD = "http://www.w3.org/2001/XMLSchema"
7575NAMESPACES = {"xsd" : "http://www.w3.org/2001/XMLSchema" }
76+ SCHEMA = etree .QName (XSD , "schema" )
77+ COMPLEX_TYPE = etree .QName (XSD , "complexType" )
78+ SIMPLE_TYPE = etree .QName (XSD , "simpleType" )
79+ DOCUMENTATION = etree .QName (XSD , "documentation" )
80+ ANNOTATION = etree .QName (XSD , "annotation" )
81+ LIST = etree .QName (XSD , "list" )
82+ COMPLEX_CONTENT = etree .QName (XSD , "complexContent" )
83+ SIMPLE_CONTENT = etree .QName (XSD , "simpleContent" )
84+ ELEMENT = etree .QName (XSD , "element" )
85+ SEQUENCE = etree .QName (XSD , "sequence" )
86+ CHOICE = etree .QName (XSD , "choice" )
87+ RESTRICTION = etree .QName (XSD , "restriction" )
88+ EXTENSION = etree .QName (XSD , "extension" )
89+ UNION = etree .QName (XSD , "union" )
90+ ALL = etree .QName (XSD , "all" )
91+ MIN_LENGTH = etree .QName (XSD , "minLength" )
92+ MAX_LENGTH = etree .QName (XSD , "maxLength" )
93+ PATTERN = etree .QName (XSD , "pattern" )
94+ ATTRIBUTE = etree .QName (XSD , "attribute" )
95+ MIN_INCLUSIVE = etree .QName (XSD , "minInclusive" )
96+ MAX_INCLUSIVE = etree .QName (XSD , "maxInclusive" )
97+ GROUP = etree .QName (XSD , "group" )
7698
7799T = TypeVar ("T" )
78100def assert_type (x : Any , t : Type [T ]) -> T :
@@ -148,7 +170,7 @@ def find_class_name(el: etree._Element, root_name: str = "Root") -> str:
148170 for parent in el .iterancestors ():
149171 if "name" in parent .attrib :
150172 return assert_type (parent .attrib ["name" ], str )
151- elif parent .tag == f"{{ { XSD } }}schema" :
173+ elif parent .tag == SCHEMA :
152174 return root_name
153175 raise ValueError ("Could not find class name for element" )
154176
@@ -199,7 +221,7 @@ def visit_element(self, el: etree._Element) -> SlotDefinition:
199221
200222 # First pass, to determine if this is simple or complex
201223 for child in el :
202- if child .tag == f"{{ { XSD } }}complexType" :
224+ if child .tag == COMPLEX_TYPE :
203225 # If we find a complex type, we need to create a class
204226 cls_name : str = el .attrib .get ("name" , PLACEHOLDER_NAME )
205227 cls = ClassDefinition (
@@ -209,7 +231,7 @@ def visit_element(self, el: etree._Element) -> SlotDefinition:
209231 self .sb .add_class (cls )
210232 slot .range = cls_name
211233 self .visit_complex_type (child , cls )
212- elif child .tag == f"{{ { XSD } }}simpleType" :
234+ elif child .tag == SIMPLE_TYPE :
213235 # If we find a simple type, the range is a restriction of a primitive type
214236 self .visit_simple_type (child , slot )
215237
@@ -227,7 +249,7 @@ def visit_element(self, el: etree._Element) -> SlotDefinition:
227249
228250 # Second pass, to add annotations
229251 for child in el :
230- if child .tag == f"{{ { XSD } }}annotation" :
252+ if child .tag == ANNOTATION :
231253 # If we find an annotation, we can use it as documentation
232254 slot .description = self .visit_annotation (child , slot .description )
233255
@@ -254,7 +276,7 @@ def visit_annotation(self, el: etree._Element, existing: str | None = None) -> s
254276 if existing is not None :
255277 text .append (existing )
256278 for child in el :
257- if child .tag == f"{{ { XSD } }}documentation" :
279+ if child .tag == DOCUMENTATION :
258280 text .append (self .visit_documentation (child ))
259281 return "\n " .join (text )
260282
@@ -285,15 +307,15 @@ def visit_simple_type_restriction(self, el: etree._Element, slot: SlotDefinition
285307 slot .range = element_to_linkml_type (el , "base" )
286308 for child in el :
287309 value = assert_type (child .attrib ["value" ], str )
288- if child .tag == f"{{ { XSD } }}minInclusive" :
310+ if child .tag == MIN_INCLUSIVE :
289311 slot .minimum_value = value
290- elif child .tag == f"{{ { XSD } }}maxInclusive" :
312+ elif child .tag == MAX_INCLUSIVE :
291313 slot .maximum_value = value
292- elif child .tag == f"{{ { XSD } }}pattern" :
314+ elif child .tag == PATTERN :
293315 slot .pattern = value
294- elif child .tag == f"{{ { XSD } }}minLength" :
316+ elif child .tag == MIN_LENGTH :
295317 slot .minimum_cardinality = int (value )
296- elif child .tag == f"{{ { XSD } }}maxLength" :
318+ elif child .tag == MAX_LENGTH :
297319 slot .maximum_cardinality = int (value )
298320
299321 def visit_simple_content_restriction (self , el : etree ._Element , cls : ClassDefinition ) -> None :
@@ -312,7 +334,7 @@ def visit_simple_content_restriction(self, el: etree._Element, cls: ClassDefinit
312334 value = get_value_element (cls )
313335 self .visit_simple_type_restriction (el , value )
314336 for child in el :
315- if child .tag == f"{{ { XSD } }}attribute" :
337+ if child .tag == ATTRIBUTE :
316338 if cls .attributes is None :
317339 cls .attributes = {}
318340 attributes = assert_type (cls .attributes , Attributes )
@@ -334,7 +356,7 @@ def visit_simple_content_extension(self, el: etree._Element, cls: ClassDefinitio
334356 """
335357 cls .is_a = element_to_linkml_type (el , "base" )
336358 for child in el :
337- if child .tag == f"{{ { XSD } }}attribute" :
359+ if child .tag == ATTRIBUTE :
338360 name = assert_type (child .attrib ["name" ], str )
339361 attributes = assert_type (cls .attributes , dict [str , SlotDefinition ])
340362 attributes [name ] = self .visit_attribute (child )
@@ -380,11 +402,11 @@ def visit_simple_content(self, el: etree._Element, cls: ClassDefinition) -> None
380402 attributes = assert_type (cls .attributes , Attributes )
381403 attributes ["value" ] = value_slot
382404 for child in el :
383- if child .tag == f"{{ { XSD } }}restriction" :
405+ if child .tag == RESTRICTION :
384406 self .visit_simple_content_restriction (child , cls )
385- elif child .tag == f"{{ { XSD } }}extension" :
407+ elif child .tag == EXTENSION :
386408 self .visit_simple_content_extension (child , cls )
387- elif child .tag == f"{{ { XSD } }}annotation" :
409+ elif child .tag == ANNOTATION :
388410 # Annotations inside simple content are applied to the value slot, not the class
389411 value_slot .description = self .visit_annotation (child , value_slot .description )
390412
@@ -409,23 +431,23 @@ def visit_simple_type(self, el: etree._Element, slot: SlotDefinition | Anonymous
409431 slot: the slot to annotate
410432 """
411433 for child in el :
412- if child .tag == f"{{ { XSD } }}annotation" :
434+ if child .tag == ANNOTATION :
413435 slot .description = self .visit_annotation (child , slot .description )
414- elif child .tag == f"{{ { XSD } }}restriction" :
436+ elif child .tag == RESTRICTION :
415437 self .visit_simple_type_restriction (child , slot )
416- elif child .tag == f"{{ { XSD } }}list" :
438+ elif child .tag == LIST :
417439 slot .multivalued = True
418440 if "itemType" in child .attrib :
419441 slot .range = assert_type (child .attrib ["itemType" ], str )
420442 else :
421443 slot .range = AnonymousSlotExpression ()
422444 for list_child in child :
423- if list_child .tag == f"{{ { XSD } }}simpleType" :
445+ if list_child .tag == SIMPLE_TYPE :
424446 self .visit_simple_type (list_child , slot .range )
425447 else :
426448 raise ValueError ("Only xsd:simpleType is allowed inside xsd:list" )
427449 raise ValueError ("xsd:list must have an itemType attribute or an xsd:simpleType child element" )
428- elif child .tag == f"{{ { XSD } }}union" :
450+ elif child .tag == UNION :
429451 slot .any_of = []
430452 for union_child in child :
431453 union_slot = AnonymousSlotExpression ()
@@ -439,7 +461,7 @@ def visit_attribute(self, el: etree._Element) -> SlotDefinition:
439461 description : str | None = None
440462
441463 for child in el :
442- if child .tag == f"{{ { XSD } }}annotation" :
464+ if child .tag == ANNOTATION :
443465 description = self .visit_annotation (child )
444466
445467 return SlotDefinition (
@@ -482,22 +504,22 @@ def visit_complex_content_child(self, el: etree._Element, cls: ClassDefinition)
482504 cls .is_a = element_to_linkml_type (el , "base" )
483505
484506 attributes = assert_type (cls .attributes , Attributes )
485- if el .tag == f"{{ { XSD } }}sequence" :
507+ if el .tag == SEQUENCE :
486508 attributes |= {
487509 slot .name : slot for slot in self .visit_sequence (el )
488510 }
489- elif el .tag == f"{{ { XSD } }}group" :
511+ elif el .tag == GROUP :
490512 raise NotImplementedError ("xsd:group is not yet supported" )
491- elif el .tag == f"{{ { XSD } }}all" :
513+ elif el .tag == ALL :
492514 raise NotImplementedError ("xsd:all is not yet supported" )
493- elif el .tag == f"{{ { XSD } }}choice" :
515+ elif el .tag == CHOICE :
494516 attributes |= {
495517 slot .name : slot for slot in self .visit_choice (el )
496518 }
497- elif el .tag == f"{{ { XSD } }}attribute" :
519+ elif el .tag == ATTRIBUTE :
498520 slot = self .visit_attribute (el )
499521 attributes [slot .name ] = slot
500- elif el .tag == f"{{ { XSD } }}annotation" :
522+ elif el .tag == ANNOTATION :
501523 cls .description = self .visit_annotation (el , cls .description )
502524
503525 def visit_complex_content (self , el : etree ._Element , cls : ClassDefinition ) -> None :
@@ -519,11 +541,11 @@ def visit_complex_content(self, el: etree._Element, cls: ClassDefinition) -> Non
519541 """
520542 #: If we have found an extension or restriction
521543 for child in el :
522- if child .tag == f"{{ { XSD } }}extension" :
544+ if child .tag == EXTENSION :
523545 self .visit_complex_content_child (child , cls )
524- elif child .tag == f"{{ { XSD } }}restriction" :
546+ elif child .tag == RESTRICTION :
525547 self .visit_complex_content_child (child , cls )
526- elif child .tag == f"{{ { XSD } }}annotation" :
548+ elif child .tag == ANNOTATION :
527549 cls .description = self .visit_annotation (child , cls .description )
528550
529551 def visit_complex_type (self , el : etree ._Element , cls : ClassDefinition ) -> None :
@@ -539,10 +561,10 @@ def visit_complex_type(self, el: etree._Element, cls: ClassDefinition) -> None:
539561
540562 found_content = False
541563 for child in el :
542- if child .tag == f"{{ { XSD } }}complexContent" :
564+ if child .tag == COMPLEX_CONTENT :
543565 self .visit_complex_content (child , cls )
544566 found_content = True
545- elif child .tag == f"{{ { XSD } }}simpleContent" :
567+ elif child .tag == SIMPLE_CONTENT :
546568 self .visit_simple_content (child , cls )
547569 found_content = True
548570
@@ -566,11 +588,11 @@ def visit_sequence(self, el: etree._Element) -> Iterable[SlotDefinition]:
566588 Converts an xsd:sequence into a list of SlotDefinitions
567589 """
568590 for child in el :
569- if child .tag == f"{{ { XSD } }}element" :
591+ if child .tag == ELEMENT :
570592 yield self .visit_element (child )
571- elif child .tag == f"{{ { XSD } }}choice" :
593+ elif child .tag == CHOICE :
572594 yield from self .visit_choice (child )
573- elif child .tag == f"{{ { XSD } }}sequence" :
595+ elif child .tag == SEQUENCE :
574596 yield from self .visit_sequence (child )
575597 elif isinstance (child , etree ._Comment ):
576598 pass
@@ -588,10 +610,10 @@ def visit_schema(self, schema: etree._Element) -> None:
588610
589611 attributes : dict [str , SlotDefinition ] = {}
590612 for child in schema :
591- if child .tag == f"{{ { XSD } }}element" :
613+ if child .tag == ELEMENT :
592614 definition = self .visit_element (child )
593615 attributes [definition .name ] = definition
594- elif child .tag == f"{{ { XSD } }}complexType" :
616+ elif child .tag == COMPLEX_TYPE :
595617 # complexType can be at the top level
596618 cls = ClassDefinition (name = PLACEHOLDER_NAME )
597619 self .visit_complex_type (child , cls )
0 commit comments