xsd:enumeration inheritance fails

  • Thread starter Thread starter aevans1108
  • Start date Start date
A

aevans1108

Greetings

I can't seem to inherit enumerated values from a globally defined type
in my XML schema. XmlSchema.Compile() doesn't like it.

Here's the schema.

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xsd:simpleType name="MYTYPE">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Option1" />
<xsd:enumeration value="Option2" />
<xsd:enumeration value="Option3" />
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="Element1" default="Option1">
<xsd:simpleType>
<xsd:restriction base="MYTYPE">
<xsd:enumeration value="Option4" />
<xsd:enumeration value="Option5" />
<xsd:enumeration value="Option6" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:schema>

Here's the C# source.

const String FILENAME = @"d:\work\schemaTest\schemaTest2.xsd";

static protected void ValidationCallbackOne(object sender,
ValidationEventArgs args)
{
Console.WriteLine( args.Message );
}

static void Main()
{
XmlSchema schema = XmlSchema.Read( new XmlTextReader( FILENAME ),
null );
schema.Compile(new ValidationEventHandler(ValidationCallbackOne));
}

Here's the output.

The Enumeration constraint failed. An error occurred at
file:///d:/work/schemaTest/schemaTest2.xsd, (13, 10).

Does anyone have any clues? As always, guesses are welcome.
Thanks!
Tony
 
Hi Tony,

When you create new datatypes by restriction, you add constraints to the
possible values of the base type. Your base type "MYTYPE" only allows three
values - "Option1", "Option2", and "Option3". Values "Option4", "Option5",
and "Option6" are not allowed by this datatype. This is why your
restriction is invalid.

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thank you for your reply, Stan.

So what you're saying is that my second simpleType can further restrict
MYTYPE, but it cannot extend it using xsd:restrictiions, right?

T
 
Right. If you want to extend it, you need to use xsd:extension

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
 
I've got this schema that a big company shipped with one of their
popular products and it is riddled with bugs like this. I should have
known that just because they're a big company doesn't mean that they're
infallible.

Okay, thanks for the info.

T
 
Would you demonstrate, Stan? I'm confused on the xsd:extension syntax.
I tried this:

<xsd:simpleType name="MYTYPE">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Option1" />
<xsd:enumeration value="Option2" />
<xsd:enumeration value="Option3" />
</xsd:restriction>
</xsd:simpleType>

<xsd:element name="Element1" default="Option1">
<xsd:simpleType>
<xsd:extension base="MYTYPE">
<xsd:enumeration value="Option4" />
<xsd:enumeration value="Option5" />
<xsd:enumeration value="Option6" />
</xsd:extension>
</xsd:simpleType>
</xsd:element>

.... but now the XmlSchema object won't even read it in. It tells me
that "extension is invalid in this context".

In case it's unclear, I want element1 to allow the values "Option1",
"Option2", "Option3", "Option4", "Option5", and "Option6".
Thanks
Tony
 
Hello again.

I changed the subject line to reflect this new, but related problem.

I can see that you can extend (ie: add values to) an xsd:enumeration
with an xsd:union, but XmlSchemaSimpleTypeUnion doesn't seem to expose
unioned types that are defined inline.

Sample schema:

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">

<xsd:simpleType name="MYTYPE">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Option1" />
</xsd:restriction>
</xsd:simpleType>

<xsd:element name="Element1" default="Option1">
<xsd:simpleType>
<xsd:union memberTypes="MYTYPE">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Option2" />
</xsd:restriction>
</xsd:simpleType>
</xsd:union>
</xsd:simpleType>
</xsd:element>

</xsd:schema>

The following C# code only prints the name of the first member type in
the union (ie: the one defined globally). The second member type,
which is declared inline and nameless (the XmlSchema class requires it
to be nameless), doesn't seem to be accessible. Specifically, I want
to get at the facets of the inline type.

const String FILENAME = @"schemaTest3.xsd";

static protected void ValidationCallbackOne(object sender,
ValidationEventArgs args)
{
Console.WriteLine( args.Message.Substring(0,34) );
}

static void Main()
{
XmlSchema schema = XmlSchema.Read( new XmlTextReader( FILENAME ), null
);
schema.Compile(new ValidationEventHandler(ValidationCallbackOne));

XmlSchemaSimpleTypeUnion u =
(XmlSchemaSimpleTypeUnion)((XmlSchemaSimpleType)((XmlSchemaElement)schema.Items[1]).SchemaType).Content;

foreach( XmlQualifiedName entry in u.MemberTypes )
{
Console.WriteLine( entry.Name );
}

}

I tried giving the inline type a name, then using that name in the
memberTypes attribute of the xsd:union element, but the XmlSchema class
didn't like that. Yes, I know I could define the inline type globally,
but I don't have that much control over the schema.

Surely there must be a way to access this inline type. Can anyone help
me? As always, all suggestions are welcome.

Thanks
Tony
 
Argh! Figured it out.

Where MemberTypes contains the names of the types in the memberTypes
list, BaseTypes contains a list of the types defined inline.
<sigh>

Disregard.

Thanks
Tony
 
Hi Tony,

Sorry for the late reply - I just got back from the holiday break. Glad you
figured it out.

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top