Question

Given the following representative snippet:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:j="http://foo" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://foo" elementFormDefault="qualified" attributeFormDefault="unqualified" version="3.2">
<xs:element name="Event">
    <xs:annotation>
        <xs:documentation>The Incident beginning and end date and time</xs:documentation>
    </xs:annotation>
    <xs:complexType>
        <xs:complexContent>
            <xs:extension base="j:EventType"/>
        </xs:complexContent>
    </xs:complexType>
</xs:element>
<xs:complexType name="EventType">
    <xs:sequence>
        <xs:element ref="j:EventDate" minOccurs="0"/>
        <xs:element ref="j:EventTime" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute ref="j:EventType" use="required"/>
</xs:complexType>
<xs:attribute name="EventType">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="Beginning"/>
            <xs:enumeration value="Ending"/>
        </xs:restriction>
    </xs:simpleType>
</xs:attribute>

The following XML doesn't validate. Specifically, it won't validate the EventType attribute.

<?xml version="1.0" encoding="utf-8"?>
<SomeDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://hostedbykarpel.com/Schemas/ReferralDocument_2">
  <Event EventType="TypeOne">
    <EventDate>2013-12-18</EventDate>
    <EventTime>00:15:28</EventTime>
  </Event>
</SomDoc>

However, if I explicitly add the namespace inside the element it works:

<Event a:EventType="TypeOne" xmlns:a="http://foo">
  <EventDate>2013-12-18</EventDate>
  <EventTime>00:15:28</EventTime>
</Event>

The namespace is already declared at the root of the document. Why would I need to specify it again just to get the attribute to show up? The Event element itself validates just fine, it's just the EventType attribute that won't.

Was it helpful?

Solution

I think this behavior is defined by spec for named attributes referenced with ref. I don't believe there's a way to change it. However, you could get around by not using ref.

<xs:complexType name="EventElement">
  ...
  <xs:attribute name="EventType" type="j:EventTypeValue" use="required"/>
  ...
</xs:complexType>
...
<xs:simpleType name="EventTypeValue">
  ...
</xs:simpleType>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top