0

I have given

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
  targetNamespace="AdditionalAttribute.OtherXSD"
  xmlns:o="AdditionalAttribute.OtherXSD" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:element name="Tag" type="o:TagType" />
  <xs:complexType name="TagType" />
</xs:schema>

and I want to write

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
  targetNamespace="AdditionalAttribute.MyXSD" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <!-- ? -->
</xs:schema>

so this becomes valid XML:

<?xml version="1.0" encoding="UTF-8"?>
<o:Tag
  xmlns:o="AdditionalAttribute.OtherXSD"
  xmlns:m="AdditionalAttribute.MyXSD"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
    AdditionalAttribute.OtherXSD AdditionalAttribute.OtherXSD.xsd 
    AdditionalAttribute.MyXSD AdditionalAttribute.MyXSD.xsd
  "
  m:AdditionalAttribute="Value"
/>

I am aware of a similar question, that asks how to place an additional attribute to all complex types in another XSD. The answer for that is no. However, I'm not an expert in this and from the answers there I cannot derieve if it would be possible for a specific complex type. I tried several things without success and it seems that this is only possible by xsd:extension'ing or xsd:restriction'ing a complex type, thus derieving a new one, which is not what I want. I want to "merge" attributes on the same tag:

  <o:Tag o:...=...           />
+ <o:Tag           m:...=... />
= <o:Tag o:...=... m:...=... />

Is this possible and how?

Community
  • 1
  • 1
Max
  • 671
  • 5
  • 13

2 Answers2

0
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:o="AdditionalAttribute.OtherXSD" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="AdditionalAttribute.MyXSD" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="AdditionalAttribute.OtherXSD" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="AdditionalAttribute.MyXSD" />
  <xs:element name="Tag">
    <xs:complexType>
      <xs:attribute ref="m:AdditionalAttribute" use="required" />
    </xs:complexType>
  </xs:element>
</xs:schema>
Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87
  • I think this is not doing what I want. I exchanged the namespaces "MyXSD" and "OtherXSD" here because it is MyXSD that I can edit and that can import OtherXSD. I want to add an additional (new) attribute to an elsewhere defined tag. With this here XMLLint gives me: `AdditionalAttribute.MyXSD.xsd:11: element attribute: Schemas parser error : attribute use (unknown), attribute 'ref': The QName value '{AdditionalAttribute.OtherXSD}AdditionalAttribute' does not resolve to a(n) attribute declaration. WXS schema AdditionalAttribute.MyXSD.xsd failed to compile` – Max Mar 28 '13 at 21:20
0

I think your problem is that you are trying to operate outside of some deliberately-imposed constraints in XML Schema.

  • a tag is identified by a qualified name ( QName ) which is composed of a name and namespace.
  • the type of a tag defines its allowed content

What you are proposing would add a new attribute to 'Tag' and thus change its type for some consumers of the XSD model.

Having said that, you can do what you want, provided that you are willing to add an xsi:type attribute to the XML. Given that o:TagType is an empty type, you might as well do it this way:

<m:Tag xsi:type="m:TagWithAttribute" m:additionalAttribute="Value" />
  • make o:TagType an abstract type
  • use xs:extension to create a new type 'm:TagWithAttribute' based on o:TagType

By adding the xsi:type attribute, the schema validator can distinguish your usage of 'Tag' from that of somebody else who wants it to be validated differently. In other words, XML Schema is enforcing strong typing ( xs:redefines gets around the strong typing, which is why some people don't like it ).

kimbert
  • 2,376
  • 1
  • 10
  • 20