Is there a way to extend XSD elements with custom attributes?
For example, I'd like to do the following in an XSD:
<xs:element name="myElement" type="xs:string" myCustomAttribute="true" />
Is there a way to extend XSD elements with custom attributes?
For example, I'd like to do the following in an XSD:
<xs:element name="myElement" type="xs:string" myCustomAttribute="true" />
Extending XSD with custom attributes can be accomplished by first defining the custom attributes in your own namespace, as follows:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.mycompany.com"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:attribute name="myAttribute" type="xs:boolean" default="true"/>
</xs:schema>
In this namespace, http://www.mycompany.com
, a single attribute named myAttribute
is defined, with a type of xs:boolean
.
Next, use this namespace in your target schema, as follows:
<?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"
xmlns:mc="http://www.mycompany.com"
xsi:schemaLocation="http://www.mycompany.com ./doc.xsd"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="element1" mc:myAttribute="false"/>
</xs:schema>
In this example, the <schema>
element includes attributes that define the custom namespace (xmlns:mc="http://www.mycompany.com"
), and the location for the custom schema file (xsi:schemaLocation="http://www.mycompany.com ./doc.xsd"
).
The target schema contains a single element, "element1"
, and has the custom attribute myAttribute
defined above, with a value of "false"
. Note that the name of the custom attribute is prefixed with the custom namespace prefix. Also note that if a value of an invalid type is used (example: mc:myAttribute="invalid"
), a validation error will be generated.
Credit to @GhislainFourny and @kjhughes for help with this answer.
No, you cannot add your own components to an XSD without confusing XSD processors.
For example, Xerces-J, upon encountering your custom attribute example,
<xs:element name="myElement" type="xs:string" myCustomAttribute="true" />
will respond with the following error:
[Error] try.xsd:3:59: s4s-att-not-allowed: Attribute 'myCustomAttribute' cannot appear in element 'element'.
If you want to augment an XSD, use xsd:annotation/xsd:appinfo
or attributes from your own namespace [Credit: @SpatialBridge]:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:myns="http://www.mycompany.com">
<xs:element name="myElement" myns:myCustomAttribute="true"/>
</xs:schema>
XBRL is an example of technology that extends XML Schema with its own custom attributes.
The following example is taken directly from the XBRL 2.1 specification. xbrli:balance and xbrli:periodType are added by XBRL on top of XML Schema.
<element
id="a2"
name="fixedAssets"
xbrli:balance="debit"
xbrli:periodType="instant"
type="xbrli:monetaryItemType"
substitutionGroup="xbrli:item"/>
As stated in kjhughes's answer though, you need to use your own namespace (in this case the xbrli prefix is bound with http://www.xbrl.org/2003/instance).