views:

752

answers:

2

I have XML that includes an element that has the attribute: xsi:type="AnswerBool". My xsd has that element and has set up an attribute with the name="type" and then restricts the enumeration values to "AnswerBool" (and others). However, when I try to validate the XML it fails. If I change the XML so that the element uses type rather than xsi:type all is well.

XML:

      <Answer xsi:type="AnswerBool">

-1

XSD:

        <xs:element name="Answer" minOccurs="0" maxOccurs="unbounded">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="Value">
                        <xs:complexType>
                            <xs:simpleContent>
                                <xs:extension base="xs:string">
                                    <xs:attribute name="type">
                                        <xs:simpleType>
                                            <xs:restriction base="xs:string">
                                                <xs:enumeration value="xsd:int"/>
                                            </xs:restriction>
                                        </xs:simpleType>
                                    </xs:attribute>
                                </xs:extension>
                            </xs:simpleContent>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
                <xs:attribute name="type" use="optional">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:enumeration value="AnswerBool"/>
                            <xs:enumeration value="AnswerMsc2DTO"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
            </xs:complexType>
        </xs:element>

I tried to name the "type" attribute to "xsi:type" but I am not allowed to have a ':' in the name. Unfortunately I cannot update the XML to NOT use "xsi:" before "type". I know what I am doing is basically some sort of work around to allow multiple different types for my "Answer" element. So I might be pushing things too far to get this all to work. However...

Does anyone have any ideas as to how I can get my XML to validate?

I mean is there a way to include the "xsi:" in my attribute's name or perhaps is there a way to have the validation process only look at the "type" part and ignore the "xsi:"?

A: 

xsi, here is the prefix indicating the xmlns, i.e. the namespace

The xml input is probably missing a reference to the corresponding namespace.

typically, xsi is used for the Schema-instance namespace.

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
mjv
I am not quite sure I understand what you're getting at. The instance for the XML root element is already set for http://www.w3.org/2001/XMLSchema-instance. Am I supposed to somehow add that xsi to the schema?
Eves
Good, and is the prefix used with this namespace also xsi ? If it is, look to the fact that the Answer element is not effectivley declared to have an "xsi" type (maybe it is just declared as "nothing" (i.e. with the default xmlns, actualy) type. If it is not xsi, then see that the type attribute for the Answer nodes use this particular prefix.
mjv
Nothing seems to be working for me. My schema instances are all the same and there is still a problem. I'll have to skip using this method. Thanks for the help though
Eves
+1  A: 

First, xsi is the prefix conventionally given for the schema instance, so don't change that.

Second, the purpose of xsl:type is to designate that a particular element has a particular type from an XML Schema. In the fragment of the schema you showed, "Answer" is the only schema type. "AnswerBool" and "AnswerMsc2DTO" are values that an attribute declared type could have, but that "type" isn't the same as the xsi:type.

So in your file, you should use type if the XML file and the schema are both in the same (default) namespace.

Hope that helps.

lavinio