我有以下架构
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="translator">
...
</xs:element>
</xs:schema>
如何定义以下必需属性,因此在添加新的翻译器节点时,还会添加这些属性?
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Translator.xsd"
如果我把它们放在XSD中,就像这样
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="translator">
<xs:attribute name="xmlns:xsi" type="xs:string" default="http://www.w3.org/2001/XMLSchema-instance"/>
<xs:attribute name="xsi:noNamespaceSchemaLocation" type="xs:string" default="Translator.xsd"/>
</xs:element>
</xs:schema>
Xerces报告了以下问题
[Error] :678:114: s4s-att-invalid-value: Invalid attribute value for 'name' in element 'attribute'. Recorded reason: cvc-datatype-valid.1.2.1: 'xmlns:xsi' is not a valid value for 'NCName'.
[Error] :678:114: src-attribute.3.1: One of 'ref' or 'name' must be present in a local attribute declaration.
[Error] :679:117: s4s-att-invalid-value: Invalid attribute value for 'name' in element 'attribute'. Recorded reason: cvc-datatype-valid.1.2.1: 'xsi:noNamespaceSchemaLocation' is not a valid value for 'NCName'.
[Error] :679:117: src-attribute.3.1: One of 'ref' or 'name' must be present in a local attribute declaration.
答案 0 :(得分:1)
首先警告:XML Schema规范禁止在XML Schema实例名称空间中声明属性,并explicitly discourages attempts to alter its behavior。
话虽如此,您获得的错误的原因是name属性仅支持通过提供其本地名称来定义目标命名空间中的新元素(或者在这种情况下在无命名空间中)。
您可以通过引用xsi:noNamespaceSchemaLocation属性在技术上做这样的事情,该属性已经在内置的XML Schema实例命名空间中定义:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xs:element name="translator">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute ref="xsi:noNamespaceSchemaLocation" default="Translator.xsd"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
但是,您无法更改其定义,并且由于此属性是以特殊方式构建和处理的,因此我不确定您是否可以对其行为产生太大影响。