S Yuki
S Yuki

Reputation: 95

How to add custom Attributes to ComplexType Elements in xsd?

I am generating a c# class file from an XSD provided to me. I am able to do this without problems if using just the original native XSD. However, I would like to make modifications to the XSD by adding my own namespace and I would like to feed the newly modified XSD through the generator to produce a class file that contains class definitions I was provided, as well as definitions I have added.

So my understanding is that I need to add a namespace as well as the new attribute under that namespace. I do have access and the ability to modify the original XSD or add my own xsd.

I looked at this post which was similar Extend XSD with custom attributes?

An example of my attempt is below. In my attempt I am trying to add an attribute "Humidity" to the DeviceInfo element under the namespace of "mns". This currently shows an error of the "'mns:attribute' element is not supported in this context."

test.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       targetNamespace="http://MyNamespace.com"
       elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="DeviceInfo">
        <xs:complexType>
            <xs:attribute name="Humidity" type="xs:float" />
        </xs:complexType>
    </xs:element>
</xs:schema>

main.xsd

<xs:schema targetNamespace="http://www.CIP4.org/JDFSchema_2_0"
       xmlns="http://www.CIP4.org/JDFSchema_2_0"
       xmlns:xs="http://www.w3.org/2001/XMLSchema"
       xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mns="http://MyNamespace.com"
       xsi:schemaLocation="http://MyNamespace.com ./test.xsd"
       elementFormDefault="qualified"
       attributeFormDefault="unqualified">
<xs:element name="DeviceInfo">
    <xs:complexType>
        <xs:sequence>
            <xs:element maxOccurs="unbounded" minOccurs="0" ref="Activity"/>
            <xs:element maxOccurs="2" minOccurs="0" ref="FileSpec"/>
            <xs:element maxOccurs="unbounded" minOccurs="0" ref="JobPhase"/>
        </xs:sequence>
        <xs:attribute name="CounterUnit" type="NMTOKEN" use="optional"/>
        <mns:Humidity value="0"/>
</xs:schema>

Currently this returns the 'mns:humidity' element is not supported in this context.

Can anybody explain how I can insert this to the complex type?

I also tried placing the mns:Humidity in the same tag as deviceInfo

<xs:element name="DeviceInfo mns:Humidity="0">

Sending that through the class generator produced two separate instances of "DeviceInfo" where the second had appended a 1.

How can I insert it in the complexType with the namespace?

Upvotes: 1

Views: 1395

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

In main.xsd, in the declaration of DeviceInfo, add

<xs:attribute ref="mns:Humidity"/>

plus a namespace declaration for "mns" and an xs:import declaration for that namespace with schemaLocation="test.xsd".

In test.xsd include the single declaration

<xs:attribute name="Humidity" type="xs:float"/>

Your attempt to solve this suggests that you are using trial and error rather than actually reading up what constructs in XSD actually mean. That isn't a good way forward. You seem fairly confused about the concepts, for example a schema should never contain an xsi:schemaLocation attribute.

Upvotes: 2

Related Questions