-1

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

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

[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.
nucandrei
  • 926
  • 3
  • 13
  • 31

1 Answers1

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.

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.

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

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

Ghislain Fourny
  • 6,971
  • 1
  • 30
  • 37
  • I would only like to transfer them from XSD to XML when creating a new XML file. – nucandrei Mar 30 '16 at 09:19
  • The following warning is shown in Xerces log after making recomended changes: 678:89: src-resolve.4.2: Error resolving component 'xsi:noNamespaceSchemaLocation'. It was detected that 'xsi:noNamespaceSchemaLocation' is in namespace 'http://www.w3.org/2001/XMLSchema-instance', but components from this namespace are not referenceable from schema document 'null'. If this is the incorrect namespace, perhaps the prefix of 'xsi:noNamespaceSchemaLocation' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'null'. – nucandrei Mar 30 '16 at 09:23
  • Hi nucandrei, you can try to use an import as specified on http://stackoverflow.com/questions/17094247/where-is-the-xsd-file-for-http-www-w3-org-2001-xmlschema-instance but keep in mind this is all unchartered, unrecommended territory and processors may not behave as you would expect. – Ghislain Fourny Mar 30 '16 at 16:09