Search code examples
xmlxsdxsd-validationxml-validationxlink

How to use XLink data types in XSD


When I try to run XSD file with xjc I get this error:

Error resolving component 'xlin:href'. It was detected that 'xlin:href' is in namespace 'http://www.w3.org', but components from this namespace are not referenceable from schema document "file address" perhaps the prefix of 'xlin:href' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to "file address"

Below is the code:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="properties">
<xs:complexType>
  <xs:sequence>
    <xs:element name="property" maxOccurs="unbounded" minOccurs="0">
      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:string">
            <xs:attribute type="xs:int" name="giataId" use="optional"/>
            <xs:attribute type="xs:dateTime" name="lastUpdate" use="optional"/>
            <xs:attribute ref="xlin:href" xmlns:xlin="http://www.w3.org/1999/xlink"/>
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
  <xs:attribute type="xs:date" name="lastUpdate"/>
     </xs:complexType>
       </xs:element>
            </xs:schema>

Solution

  • To access any XSD components in another namespace, you have import that namespace.

    In your case, add

    <xs:import namespace="http://www.w3.org/1999/xlink" 
               schemaLocation="http://www.w3.org/1999/xlink.xsd"/>
    

    to your XSD to have access to the XLink components.


    Here is general guidance for referencing XLink components such as href from an XSD:

    1. Declare xlink namespace prefix, commonly on the xs:schema element:

      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                 xmlns:xlink="http://www.w3.org/1999/xlink"
      
    2. Import the XLink XSD:

      <xs:import namespace="http://www.w3.org/1999/xlink" 
                 schemaLocation="http://www.w3.org/1999/xlink.xsd"/>
      
    3. Reference the needed XLink component:

      <xs:attribute ref="xlink:href" xmlns:xlin="http://www.w3.org/1999/xlink"/>