You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When attempting to parse an instance document that utilizes a third party CV, an exception is raised. Here is some test output:
>>> s = STIXPackage.from_xml("TESTVOCAB.xml")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/c/dev/python-stix/stix/core/stix_package.py", line 429, in from_xml
return parser.parse_xml(xml_file)
File "/c/dev/python-stix/stix/utils/parser.py", line 109, in parse_xml
stix_package = STIXPackage().from_obj(stix_package_obj)
File "/c/dev/python-stix/stix/core/stix_package.py", line 379, in from_obj
return_obj.stix_header = STIXHeader.from_obj(obj.get_STIX_Header())
File "/c/dev/python-stix/stix/core/stix_header.py", line 112, in from_obj
return_obj.package_intents = [VocabString.from_obj(x) for x in obj.get_Package_Intent()]
File "/c/dev/python-stix/stix/common/vocabs.py", line 110, in from_obj
klass = VocabString.lookup_class(vocab_obj.xsi_type)
File "/c/dev/python-stix/stix/common/vocabs.py", line 70, in lookup_class
raise ValueError("Unregistered xsi:type %s" % xsi_type)
ValueError: Unregistered xsi:type testVocabs:TestVocab-1.0
This issue was raised during the STIX Community call on 2014/06/12.
The python code to parse this requires developers to create their own VocabString derivation and register it as a known Controlled Vocabulary, using stix.common.vocabs.add_vocab():
fromstix.coreimportSTIXPackagefromstix.common.vocabsimportVocabString, add_vocabclassTestVocab(VocabString):
_namespace='http://testvocabs.com/vocabs-1'_XSI_TYPE='testVocabs:TestVocab-1.0'add_vocab(TestVocab)
s=STIXPackage.from_xml("TESTVOCAB.xml")
prints.stix_header.package_intents[0].xsi_type# prints testVocabs:TestVocab-1.0
This parses the document just fine, and prints the xsi_type class property correctly. However, executing s.to_xml() raises a KeyError because the http://testvocabs.com/vocabs-1 namespace isn't registered as a known namespace to export.
I am going to address the serialization bug and change the behavior of VocabString.lookup_class to return a VocabString instance if the xsi:type lookup fails.
The following commits have addressed a couple of issues:
5a79974 : Parsing no longer throws an exception when an unknown CV is encountered. A generic VocabString class is returned from lookup_class(). With this setup, xsi:type information is stripped, though maybe we don't want this behavior?
564e8f3 : A lot of work done to nsparser. This commit allows users to define a VocabString derivation under a non-STIX namespace and export it without the namespace parser throwing up the KeyError exception mentioned above.
To be done:
Persist schemalocations on input so non-STIX-namespaced CVs can validate properly on round-trip
Consider requiring CVs to define an _XSI_TYPE if the _namespace attribute is not the default STIX vocab namespace.
3da966c : to_xml() now accepts a schemaloc_dict parameter which allows for third-party schemalocations to be passed in. It's possible to overwrite existing STIX default schemalocations with this parameter, which may or may not be a desired behavior.
Activity
bworrell commentedon Jun 13, 2014
My testing, proprietary Controlled Vocabulary schema
My test instance document ("TESTVOCAB.xml"). I override the default Package_Intent CV to use my
testVocabs:TestVocab-1.0CV:The python code to parse this requires developers to create their own
VocabStringderivation and register it as a known Controlled Vocabulary, usingstix.common.vocabs.add_vocab():This parses the document just fine, and prints the
xsi_typeclass property correctly. However, executings.to_xml()raises aKeyErrorbecause thehttp://testvocabs.com/vocabs-1namespace isn't registered as a known namespace to export.I am going to address the serialization bug and change the behavior of
VocabString.lookup_classto return aVocabStringinstance if thexsi:typelookup fails.bworrell commentedon Jun 14, 2014
The following commits have addressed a couple of issues:
lookup_class(). With this setup,xsi:typeinformation is stripped, though maybe we don't want this behavior?nsparser. This commit allows users to define a VocabString derivation under a non-STIX namespace and export it without the namespace parser throwing up the KeyError exception mentioned above.To be done:
bworrell commentedon Aug 11, 2014
Still need to figure out:
_XSI_TYPEor_namespaceor as a parameter toto_xml()bworrell commentedon Aug 11, 2014
to_xml()now accepts aschemaloc_dictparameter which allows for third-party schemalocations to be passed in. It's possible to overwrite existing STIX default schemalocations with this parameter, which may or may not be a desired behavior.Closing this issue now.