xmlschema 元素不是架构的元素
xmlschema element is not an element of the schema
使用 xmlschema 和 XPath 解码 XML 文档的一部分,选择具有属性名称和值 doc_id=2
的所有 item
元素失败。
这是我的 xml 文件 simple.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<na:main
xmlns:na="ames"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="ames ./simple.xsd">
<na:item doc_id="1" ref_id="k1">content_k1</na:item>
<na:item doc_id="2" ref_id="k2">content_k2</na:item>
</na:main>
这是我的 xml 架构文件 simple.xsd
:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:na="ames"
targetNamespace="ames"
elementFormDefault="qualified">
<xs:complexType name="itemtype">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="doc_id" type="xs:int" />
<xs:attribute name="ref_id" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="maintype">
<xs:sequence>
<xs:element name="item" maxOccurs="unbounded" type="na:itemtype" />
</xs:sequence>
</xs:complexType>
<xs:element name="main" type="na:maintype" />
</xs:schema>
这是我的 python 代码:
>>> import xmlschema
>>> xs = xmlschema.XMLSchema('simple.xsd')
>>> xs.is_valid('simple.xml')
True
>>> xs.to_dict('simple.xml', ".//na:item[@doc_id=1]")
{'@doc_id': 1, '@ref_id': 'k1', '$': 'content_k1'}
>>> xs.to_dict('simple.xml', ".//na:item[@doc_id=2]")
---------------------------------------------------------------------------
XMLSchemaValidationError Traceback (most recent call last)
<ipython-input-57-8ff81c2eaf9c> in <module>
----> 1 xmlschema.XMLSchema('simple.xsd').to_dict('simple.xml', ".//na:item[@doc_id=2]")
/rao/uhome/rmol3/bin/anaconda3_rmol3_lglxs408_2/envs/MW41_Quicklook/lib/python3.7/site-packages/xmlschema/validators/schema.py in decode(self, source, path, schema_path, validation, *args, **kwargs)
1553 """
1554 data, errors = [], []
-> 1555 for result in self.iter_decode(source, path, schema_path, validation, *args, **kwargs):
1556 if not isinstance(result, XMLSchemaValidationError):
1557 data.append(result)
/rao/uhome/rmol3/bin/anaconda3_rmol3_lglxs408_2/envs/MW41_Quicklook/lib/python3.7/site-packages/xmlschema/validators/schema.py in iter_decode(self, source, path, schema_path, validation, process_namespaces, namespaces, use_defaults, decimal_type, datetime_types, converter, filler, fill_missing, max_depth, depth_filler, lazy_decode, **kwargs)
1540 else:
1541 reason = "{!r} is not an element of the schema".format(elem)
-> 1542 yield schema.validation_error(validation, reason, elem, source, namespaces)
1543 return
1544
/rao/uhome/rmol3/bin/anaconda3_rmol3_lglxs408_2/envs/MW41_Quicklook/lib/python3.7/site-packages/xmlschema/validators/xsdbase.py in validation_error(self, validation, error, obj, source, namespaces, **_kwargs)
904
905 if validation == 'strict' and error.elem is not None:
--> 906 raise error
907 return error
908
XMLSchemaValidationError: failed validating <Element '{ames}item' at 0x7eff7913db90> with XMLSchema10(basename='simple.xsd', namespace='ames'):
Reason: <Element '{ames}item' at 0x7eff7913db90> is not an element of the schema
Instance:
<na:item xmlns:na="ames" doc_id="2" ref_id="k2">content_k2</na:item>
Path: /na:main/na:item[2]
XPath 语句有什么问题 ".//na:item[@doc_id=2]"
?
您可以尝试以下 XPath:
/na:main/na:item[@doc_id='2']
XPath 不相关 - 只有在 XML 文档可以解析时才能执行。但是您从 XML 解析器收到架构验证错误。它声称您文档中的根标记未在您的 XSD 中声明。 但是,我已经在https://www.freeformatter.com/xml-validator-xsd.html中测试了你的XSD和XML并且验证正常。
请检查您发布的 XML/XSD 组合是否是您测试过的组合 - 这可能解释了相当令人费解的情况。
架构处理器找不到用于解码数据的匹配架构元素,因为提供的路径不适合在架构元素上使用。您必须提供指向右侧 XSD 元素的显式 schema_path:
>>> xs.to_dict("simple.xml", "/na:main/na:item[@doc_id=2]", schema_path="/na:main/na:item")
{'@doc_id': 2, '@ref_id': 'k2', '$': 'content_k2'}
更多有效示例:
>>> import xmlschema
>>> xs = xmlschema.XMLSchema('simple.xsd')
>>> xs.to_dict('simple.xml', ".//na:item[@doc_id='2']", schema_path='.//na:item')
{'@doc_id': 2, '@ref_id': 'k2', '$': 'content_k2'}
>>> xs.to_dict('simple.xml', ".//na:item[@doc_id=2]", schema_path='.//na:item')
{'@doc_id': 2, '@ref_id': 'k2', '$': 'content_k2'}
使用 xmlschema 和 XPath 解码 XML 文档的一部分,选择具有属性名称和值 doc_id=2
的所有 item
元素失败。
这是我的 xml 文件 simple.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<na:main
xmlns:na="ames"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="ames ./simple.xsd">
<na:item doc_id="1" ref_id="k1">content_k1</na:item>
<na:item doc_id="2" ref_id="k2">content_k2</na:item>
</na:main>
这是我的 xml 架构文件 simple.xsd
:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:na="ames"
targetNamespace="ames"
elementFormDefault="qualified">
<xs:complexType name="itemtype">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="doc_id" type="xs:int" />
<xs:attribute name="ref_id" type="xs:string" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="maintype">
<xs:sequence>
<xs:element name="item" maxOccurs="unbounded" type="na:itemtype" />
</xs:sequence>
</xs:complexType>
<xs:element name="main" type="na:maintype" />
</xs:schema>
这是我的 python 代码:
>>> import xmlschema
>>> xs = xmlschema.XMLSchema('simple.xsd')
>>> xs.is_valid('simple.xml')
True
>>> xs.to_dict('simple.xml', ".//na:item[@doc_id=1]")
{'@doc_id': 1, '@ref_id': 'k1', '$': 'content_k1'}
>>> xs.to_dict('simple.xml', ".//na:item[@doc_id=2]")
---------------------------------------------------------------------------
XMLSchemaValidationError Traceback (most recent call last)
<ipython-input-57-8ff81c2eaf9c> in <module>
----> 1 xmlschema.XMLSchema('simple.xsd').to_dict('simple.xml', ".//na:item[@doc_id=2]")
/rao/uhome/rmol3/bin/anaconda3_rmol3_lglxs408_2/envs/MW41_Quicklook/lib/python3.7/site-packages/xmlschema/validators/schema.py in decode(self, source, path, schema_path, validation, *args, **kwargs)
1553 """
1554 data, errors = [], []
-> 1555 for result in self.iter_decode(source, path, schema_path, validation, *args, **kwargs):
1556 if not isinstance(result, XMLSchemaValidationError):
1557 data.append(result)
/rao/uhome/rmol3/bin/anaconda3_rmol3_lglxs408_2/envs/MW41_Quicklook/lib/python3.7/site-packages/xmlschema/validators/schema.py in iter_decode(self, source, path, schema_path, validation, process_namespaces, namespaces, use_defaults, decimal_type, datetime_types, converter, filler, fill_missing, max_depth, depth_filler, lazy_decode, **kwargs)
1540 else:
1541 reason = "{!r} is not an element of the schema".format(elem)
-> 1542 yield schema.validation_error(validation, reason, elem, source, namespaces)
1543 return
1544
/rao/uhome/rmol3/bin/anaconda3_rmol3_lglxs408_2/envs/MW41_Quicklook/lib/python3.7/site-packages/xmlschema/validators/xsdbase.py in validation_error(self, validation, error, obj, source, namespaces, **_kwargs)
904
905 if validation == 'strict' and error.elem is not None:
--> 906 raise error
907 return error
908
XMLSchemaValidationError: failed validating <Element '{ames}item' at 0x7eff7913db90> with XMLSchema10(basename='simple.xsd', namespace='ames'):
Reason: <Element '{ames}item' at 0x7eff7913db90> is not an element of the schema
Instance:
<na:item xmlns:na="ames" doc_id="2" ref_id="k2">content_k2</na:item>
Path: /na:main/na:item[2]
XPath 语句有什么问题 ".//na:item[@doc_id=2]"
?
您可以尝试以下 XPath:
/na:main/na:item[@doc_id='2']
XPath 不相关 - 只有在 XML 文档可以解析时才能执行。但是您从 XML 解析器收到架构验证错误。它声称您文档中的根标记未在您的 XSD 中声明。 但是,我已经在https://www.freeformatter.com/xml-validator-xsd.html中测试了你的XSD和XML并且验证正常。
请检查您发布的 XML/XSD 组合是否是您测试过的组合 - 这可能解释了相当令人费解的情况。
架构处理器找不到用于解码数据的匹配架构元素,因为提供的路径不适合在架构元素上使用。您必须提供指向右侧 XSD 元素的显式 schema_path:
>>> xs.to_dict("simple.xml", "/na:main/na:item[@doc_id=2]", schema_path="/na:main/na:item")
{'@doc_id': 2, '@ref_id': 'k2', '$': 'content_k2'}
更多有效示例:
>>> import xmlschema
>>> xs = xmlschema.XMLSchema('simple.xsd')
>>> xs.to_dict('simple.xml', ".//na:item[@doc_id='2']", schema_path='.//na:item')
{'@doc_id': 2, '@ref_id': 'k2', '$': 'content_k2'}
>>> xs.to_dict('simple.xml', ".//na:item[@doc_id=2]", schema_path='.//na:item')
{'@doc_id': 2, '@ref_id': 'k2', '$': 'content_k2'}