@@ -103,27 +103,42 @@ def __init__(self, message: str, validation_result: Optional[ValidationResult] =
103103 self .validation_result = validation_result
104104
105105
106- def _fix_known_schema_issues (schema_content : str ) -> str :
106+ def _fix_known_schema_issues (schema_content : bytes ) -> bytes :
107107 """Fix known issues in the official XTCE XSD schema.
108108
109109 The official OMG XTCE schema references xml:base but doesn't declare
110110 the xml namespace, causing lxml validation to fail.
111+
112+ Parameters
113+ ----------
114+ schema_content : bytes
115+ The schema content as bytes
116+
117+ Returns
118+ -------
119+ bytes
120+ The fixed schema content as bytes
111121 """
112- if 'ref="xml:base"' in schema_content :
122+ # Decode to string for regex processing
123+ content_str = schema_content .decode ("utf-8" )
124+
125+ if 'ref="xml:base"' in content_str :
113126 import re
114127
115128 # Remove the problematic reference entirely since it's optional for validation
116- schema_content = re .sub (
129+ content_str = re .sub (
117130 r'\s*<attribute\s+ref="xml:base"\s*/>\s*' ,
118131 "\n \t \t \t \t <!-- xml:base attribute removed for lxml compatibility -->\n \t \t \t \t " ,
119- schema_content ,
132+ content_str ,
120133 )
121- schema_content = re .sub (
134+ content_str = re .sub (
122135 r'\s*<attribute\s+ref="xml:base"></attribute>\s*' ,
123136 "\n \t \t \t \t <!-- xml:base attribute removed for lxml compatibility -->\n \t \t \t \t " ,
124- schema_content ,
137+ content_str ,
125138 )
126- return schema_content
139+
140+ # Return as bytes
141+ return content_str .encode ("utf-8" )
127142
128143
129144def _load_schema (schema_location : Union [str , Path ], timeout : int = 30 ) -> tuple [ElementTree .XMLSchema , str ]:
@@ -151,7 +166,6 @@ def _is_http_url(s):
151166 result = urlparse (s )
152167 return result if all ([result .scheme in ("http" , "https" ), result .netloc ]) else False
153168
154- schema_content = None
155169 parser = ElementTree .XMLParser (recover = True )
156170
157171 # If the location is a string that parses as a URL
@@ -164,8 +178,8 @@ def _is_http_url(s):
164178 raise XtceValidationError (f"Failed to download schema from { schema_location } : { e } " ) from e
165179 # Otherwise assume a local filepath
166180 else :
167- with Path (schema_location ).open ("r " ) as sfh :
168- schema_content = sfh .read (). encode ( "utf-8" )
181+ with Path (schema_location ).open ("rb " ) as sfh :
182+ schema_content = sfh .read ()
169183
170184 # Fix and parse the schema content
171185 try :
@@ -174,12 +188,11 @@ def _is_http_url(s):
174188 return ElementTree .XMLSchema (schema_root_element ), schema_root_element .get ("version" , "unknown" )
175189 except ElementTree .XMLSchemaError as e :
176190 # Try to fix known issues
177- content_str = schema_content .decode ("utf-8" ) if isinstance (schema_content , bytes ) else schema_content
178- fixed_content = _fix_known_schema_issues (content_str )
191+ fixed_content = _fix_known_schema_issues (schema_content )
179192
180- if fixed_content != content_str :
193+ if fixed_content != schema_content :
181194 try :
182- schema_root_element = ElementTree .XML (fixed_content . encode ( "utf-8" ) , parser )
195+ schema_root_element = ElementTree .XML (fixed_content , parser )
183196 return ElementTree .XMLSchema (schema_root_element ), schema_root_element .get ("version" , "unknown" )
184197 except ElementTree .XMLSchemaError :
185198 pass # Fall through to raise original error
0 commit comments