Declaring an attribute for another namespace in an XML schema

I use the XML format, which is a combination of various existing formats and some user elements and attributes, and I thought I should write a schema for these user bits.

One thing I do is use custom attributes for elements in existing formats, for example:

<ns1:something attA="b" attB="a" ns2:extraAtt="c"/> 

I understand that this is permitted, but I cannot imagine how to declare my "extraAtt" in an XML schema or, even worse, in a DTD.

I tried to read the spec , but it could be as well written in Chinese as I know. Most manuals only talk about “name,” “type,” and “use,” for example. this and this .

+6
source share
2 answers

Each schema document defines the components (parts of the schema) for one namespace. So, to define your ns2:extraAtt , you need a schema document similar to this:

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/my-ns2"> <xs:attribute name="extraAtt" type="xs:anySimpleType"/> </xs:schema> 

Declaring an element ns1: something must somehow resolve this attribute either with a reference to the attribute ( <xs:attribute ref="ns2:extraAtt"/> ) or using the attribute template ( <xs:anyAttribute namespace="http://example.com/my-ns2"/> or similar).


Sorry for the clarity of the specification; this is a long story, but in essence some WG members did not think that people like you exist ("no one except the performers reads the specification, and until they complain about it readable enough" - at least it was that they said that some developers complained loudly and bitterly, and then simply changed the subject).

+6
source

To declare only an attribute, you can use this XSD:

 <xs:schema targetNamespace="theNamespaceUri" elementFormDefault="qualified" attributeFormDefault="qualified" xmlns="theNamespaceUri" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:attribute name="extraAtt" type="xs:string"> </xs:attribute> </xs:schema> 

(assuming extraAtt is a simple string - you can use any type or restrict an existing type, etc.)

+1
source

Source: https://habr.com/ru/post/953077/


All Articles