@@ -94,6 +94,23 @@ def _read_epilogue(self):
9494 return epilogue_elements [0 ].text
9595 return ""
9696
97+ def _get_in_library_attribute (self , element ):
98+ """Parse inLibrary attribute from an extras element if present.
99+
100+ Parameters:
101+ element: XML element to parse
102+
103+ Returns:
104+ str or None: Library name if inLibrary attribute found, None otherwise
105+ """
106+ for attr_element in element .findall (xml_constants .ATTRIBUTE_ELEMENT ):
107+ name_elem = attr_element .find (xml_constants .NAME_ELEMENT )
108+ if name_elem is not None and name_elem .text == HedKey .InLibrary :
109+ value_elem = attr_element .find (xml_constants .VALUE_ELEMENT )
110+ if value_elem is not None :
111+ return value_elem .text
112+ return None
113+
97114 def _read_extras (self ):
98115 self ._schema .extras = {}
99116 self ._read_sources ()
@@ -107,22 +124,27 @@ def _read_sources(self):
107124 source_name = self ._get_element_value (source_element , xml_constants .NAME_ELEMENT )
108125 source_link = self ._get_element_value (source_element , xml_constants .LINK_ELEMENT )
109126 description = self ._get_element_value (source_element , xml_constants .DESCRIPTION_ELEMENT )
127+
128+ # Parse inLibrary attribute from element if present (for merged XML)
129+ in_library_value = self ._get_in_library_attribute (source_element )
130+ # If not found in XML but this is an unmerged library schema, use self.library
131+ if in_library_value is None and self .library and not self ._loading_merged :
132+ in_library_value = self .library
133+
110134 data .append (
111- {df_constants .source : source_name , df_constants .link : source_link , df_constants .description : description }
135+ {
136+ df_constants .source : source_name ,
137+ df_constants .link : source_link ,
138+ df_constants .description : description ,
139+ df_constants .in_library : in_library_value ,
140+ }
112141 )
113- library_df = pd .DataFrame (data , columns = df_constants .source_columns )
114-
115- # Add in_library column if this is a library schema
116- if self .library :
117- library_df [df_constants .in_library ] = self .library
142+ library_df = pd .DataFrame (data )
118143
119- # Merge with standard schema extras if applicable
120- if self .appending_to_schema :
121- standard_df = self ._schema .extras .get (df_constants .SOURCES_KEY , None )
122- if standard_df is not None and not standard_df .empty :
123- self ._schema .extras [df_constants .SOURCES_KEY ] = pd .concat ([standard_df , library_df ], ignore_index = True )
124- else :
125- self ._schema .extras [df_constants .SOURCES_KEY ] = library_df
144+ # Merge with existing schema extras if present (from withStandard base schema)
145+ standard_df = self ._schema .extras .get (df_constants .SOURCES_KEY , None )
146+ if standard_df is not None and not standard_df .empty :
147+ self ._schema .extras [df_constants .SOURCES_KEY ] = pd .concat ([standard_df , library_df ], ignore_index = True )
126148 else :
127149 self ._schema .extras [df_constants .SOURCES_KEY ] = library_df
128150
@@ -133,26 +155,27 @@ def _read_prefixes(self):
133155 prefix_name = self ._get_element_value (prefix_element , xml_constants .NAME_ELEMENT )
134156 prefix_namespace = self ._get_element_value (prefix_element , xml_constants .NAMESPACE_ELEMENT )
135157 prefix_description = self ._get_element_value (prefix_element , xml_constants .DESCRIPTION_ELEMENT )
158+
159+ # Parse inLibrary attribute from element if present (for merged XML)
160+ in_library_value = self ._get_in_library_attribute (prefix_element )
161+ # If not found in XML but this is an unmerged library schema, use self.library
162+ if in_library_value is None and self .library and not self ._loading_merged :
163+ in_library_value = self .library
164+
136165 data .append (
137166 {
138167 df_constants .prefix : prefix_name ,
139168 df_constants .namespace : prefix_namespace ,
140169 df_constants .description : prefix_description ,
170+ df_constants .in_library : in_library_value ,
141171 }
142172 )
143- library_df = pd .DataFrame (data , columns = df_constants . prefix_columns )
173+ library_df = pd .DataFrame (data )
144174
145- # Add in_library column if this is a library schema
146- if self .library :
147- library_df [df_constants .in_library ] = self .library
148-
149- # Merge with standard schema extras if applicable
150- if self .appending_to_schema :
151- standard_df = self ._schema .extras .get (df_constants .PREFIXES_KEY , None )
152- if standard_df is not None and not standard_df .empty :
153- self ._schema .extras [df_constants .PREFIXES_KEY ] = pd .concat ([standard_df , library_df ], ignore_index = True )
154- else :
155- self ._schema .extras [df_constants .PREFIXES_KEY ] = library_df
175+ # Merge with existing schema extras if present (from withStandard base schema)
176+ standard_df = self ._schema .extras .get (df_constants .PREFIXES_KEY , None )
177+ if standard_df is not None and not standard_df .empty :
178+ self ._schema .extras [df_constants .PREFIXES_KEY ] = pd .concat ([standard_df , library_df ], ignore_index = True )
156179 else :
157180 self ._schema .extras [df_constants .PREFIXES_KEY ] = library_df
158181
@@ -164,29 +187,28 @@ def _read_external_annotations(self):
164187 external_id = self ._get_element_value (external_element , xml_constants .ID_ELEMENT )
165188 external_iri = self ._get_element_value (external_element , xml_constants .IRI_ELEMENT )
166189 external_description = self ._get_element_value (external_element , xml_constants .DESCRIPTION_ELEMENT )
190+
191+ # Parse inLibrary attribute from element if present (for merged XML)
192+ in_library_value = self ._get_in_library_attribute (external_element )
193+ # If not found in XML but this is an unmerged library schema, use self.library
194+ if in_library_value is None and self .library and not self ._loading_merged :
195+ in_library_value = self .library
196+
167197 data .append (
168198 {
169199 df_constants .prefix : external_name ,
170200 df_constants .id : external_id ,
171201 df_constants .iri : external_iri ,
172202 df_constants .description : external_description ,
203+ df_constants .in_library : in_library_value ,
173204 }
174205 )
175- library_df = pd .DataFrame (data , columns = df_constants .external_annotation_columns )
176-
177- # Add in_library column if this is a library schema
178- if self .library :
179- library_df [df_constants .in_library ] = self .library
180-
181- # Merge with standard schema extras if applicable
182- if self .appending_to_schema :
183- standard_df = self ._schema .extras .get (df_constants .EXTERNAL_ANNOTATION_KEY , None )
184- if standard_df is not None and not standard_df .empty :
185- self ._schema .extras [df_constants .EXTERNAL_ANNOTATION_KEY ] = pd .concat (
186- [standard_df , library_df ], ignore_index = True
187- )
188- else :
189- self ._schema .extras [df_constants .EXTERNAL_ANNOTATION_KEY ] = library_df
206+ library_df = pd .DataFrame (data )
207+
208+ # Merge with existing schema extras if present (from withStandard base schema)
209+ standard_df = self ._schema .extras .get (df_constants .EXTERNAL_ANNOTATION_KEY , None )
210+ if standard_df is not None and not standard_df .empty :
211+ self ._schema .extras [df_constants .EXTERNAL_ANNOTATION_KEY ] = pd .concat ([standard_df , library_df ], ignore_index = True )
190212 else :
191213 self ._schema .extras [df_constants .EXTERNAL_ANNOTATION_KEY ] = library_df
192214
0 commit comments