1
votes

Why won't this xml schema validate ? Visual studio says that the simpleContent tag is an invalid tag. If I then remove the attribute it says that the base type for my restriction is undefined in http://www.w3.org/2001/XMLSchema

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >    
    <xs:complexType name="Person">
        <xs:attribute name="isProcessed" type="xs:boolean" />
        <xs:simpleContent>
            <xs:restriction base="xs:string">
                <xs:enumeration value="Male" />
                <xs:enumeration value="Female" />        
            </xs:restriction>
        </xs:simpleContent>    
    </xs:complexType>
</xs:schema>

@marc_s - I had tried the code button but it kept hiding all the xml in the preview area. I put the space in there so it would show up.

The end result of the xml should look like this

<person isprocessed="True" >Male</person>

Nothing fancy. Essentially I'm trying to create a simpleType with an attribute. But if I understand the W3 correctly, attributes can only be applied to complexTypes. So I tried to create a complexType with simple content inside.

2
It would be helpful to see the source xml you're trying to validate with this schema.Scott Saad
Also, there are spaces in this schema file that probably don't need to be there. For example: tag should read <xs:schema> instead of < xs:schema >. Was that a copy/paste problem in the question?Scott Saad
fixed the erroneous spaces - @Matt: you need to highlight your code or XML samples and click on the "code" button in the toolbar - the one with the 0001010110101011011 on itmarc_s

2 Answers

1
votes

I suggest breaking your type into two separate types. It's clearer, and validates properly:

<xs:complexType name="Person">
    <xs:simpleContent>
        <xs:extension base="Gender">
            <xs:attribute name="isProcessed" type="xs:boolean" />
        </xs:extension>
    </xs:simpleContent>    
</xs:complexType>

<xs:simpleType name="Gender">
    <xs:restriction base="xs:string">
        <xs:enumeration value="Male" />
        <xs:enumeration value="Female" />        
    </xs:restriction>
</xs:simpleType>

I'm not sure how to express this as a single type definition.

1
votes

Well, according to the W3 Schools XML Schema Tutorial, what you're trying to do is invalid in the context of XML schema.

You can only apply a <xs:restriction> to a <xs:simpleType> - not to a <xs:simpleContent> inside a <xs:complexType>.

A <xs:simpleContent> can contain a <xs:extension> to extend a base type - but not a restriction.

So the question really is: what are you trying to accomplish here?? What's the goal, what's the XML supposed to look like that you want to validate??

Marc

UPDATE:

OK, to achieve what you want, try this - define a complex type (in order to have the attribute), which contains a simple content (so you can have the string value) and use an xs:extension in the simple content - not a xs:restriction. So basically you're defining a complex type which extends a simple string contents, and adds an attribute:

      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:string">
            <xs:attribute name="isprocessed" type="xs:string" />
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>

In order to do this, I just simply created a "test.xml" file with the content you wanted, added an artificial <root>...</root> around is (so it's a valid XML document), and then I ran the Microsoft xsd.exe tool on it to create an XML schema from it (you can do the same in Visual Studio 2008, too - open an XML file and choose "XML > Create Schema" from the menu).

Hope this helps!