在XSD中创建xs和xmls:xsi属性

时间:2021-01-07 17:14:46

I have the following schema

我有以下架构

<?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>

How I can define the following required attributes, so when adding a new translator node, those attributes are also added?

我如何定义以下必需属性,因此在添加新的翻译器节点时,还会添加这些属性?

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Translator.xsd"

If I put them in XSD, like this

如果我把它们放在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>

The following problem is reported by Xerces

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.

1 个解决方案

#1


1  

A warning first: the XML Schema specification forbids declaring attributes in the XML Schema instance namespace, and explicitly discourages attempts to alter its behavior.

首先是警告:XML Schema规范禁止在XML Schema实例命名空间中声明属性,并明确阻止尝试更改其行为。

Having said that, the reason for the errors you are getting is that the name attribute only supports definition of new elements in the target namespace (or in this case in no namespace) by providing their local names.

话虽如此,你得到的错误的原因是name属性只支持通过提供它们的本地名称来定义目标命名空间中的新元素(或者在这种情况下没有命名空间)。

You could technically do something like this, by referencing the xsi:noNamespaceSchemaLocation attribute, which is already defined in the builtin XML Schema instance namespace:

从技术上讲,您可以通过引用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>

However, you cannot change its definition, and since this attribute is builtin and processed in a special way, I am not sure you can much influence its behavior.

但是,您无法更改其定义,并且由于此属性是以特殊方式构建和处理的,因此我不确定您是否可以对其行为产生太大影响。

#1


1  

A warning first: the XML Schema specification forbids declaring attributes in the XML Schema instance namespace, and explicitly discourages attempts to alter its behavior.

首先是警告:XML Schema规范禁止在XML Schema实例命名空间中声明属性,并明确阻止尝试更改其行为。

Having said that, the reason for the errors you are getting is that the name attribute only supports definition of new elements in the target namespace (or in this case in no namespace) by providing their local names.

话虽如此,你得到的错误的原因是name属性只支持通过提供它们的本地名称来定义目标命名空间中的新元素(或者在这种情况下没有命名空间)。

You could technically do something like this, by referencing the xsi:noNamespaceSchemaLocation attribute, which is already defined in the builtin XML Schema instance namespace:

从技术上讲,您可以通过引用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>

However, you cannot change its definition, and since this attribute is builtin and processed in a special way, I am not sure you can much influence its behavior.

但是,您无法更改其定义,并且由于此属性是以特殊方式构建和处理的,因此我不确定您是否可以对其行为产生太大影响。