7

I need to author an embedded XML schema, i.e. where the schema is define within the same XML as the data.

I'm trying to understand how to do it correctly, but so far I'm failing to get a simple example to pass validation. Here's what I was trying to use as a trivial example XML with inline schema:
(Note: The XML structure (e.g. root/item) is already out in the wild so I am constrained to not be able to use a namespace on the data elements.)

<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="#mySchema">
  <xs:schema id="mySchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="item" type="xs:string"
                      maxOccurs="unbounded" minOccurs="0"/>
        </xs:sequence>
      </xs:complexType>
   </xs:element>
 </xs:schema>
 <item>String 1</item>
 <item>String 2</item>
 <item>String 3</item>
</root>

But when I run that XML through the w3.org XML Schema Validator, the XML fails validation, with the following error message saying that it wasn't expecting to see <xs:schema> as a child element!

Invalid per cvc-complex-type.1.2.4: element {http://www.w3.org/2001/XMLSchema}:schema not allowed here (1) in element {None}:root, expecting [{None}:item,$]:

Q: Can you show me an example of a simple XML document with inline schema definition that passes validation?

Daryn
  • 4,791
  • 4
  • 39
  • 52
  • Did you mean restriction? LIke an enumeration. – PSL Mar 21 '13 at 23:44
  • 1
    With respect, I don't believe that inline schemas are supported everywhere. In fact, I believe they're only rarely supported. – John Saunders Mar 22 '13 at 00:21
  • I accidentally down voted this question from my smart phone instead of UpVote and Couldn't revise and Votes are locked. How do i reverse my vote? if @Daryn edits it i can reverse it again? – PSL Mar 22 '13 at 14:46
  • @PSCoder thanks for working towards reversing that :) I've now made a trivial edit so that that hopefully your vote will be unlocked. – Daryn Mar 22 '13 at 15:51

1 Answers1

6

If your root child has an xs:schema element as a child, then the schema needs to allow it to have such a child. The easiest way to allow it would be to use a wildcard:

<xs:sequence>
  <xs:any processContents="skip" namespace="http://www.w3.org/2001/XMLSchema"
          minOccurs="0" maxOccurs="1"/>
  <xs:element name="item" type="xs:string"
          maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Thanks @Michael; I tried that too but validation fails because it's non-deterministic ("Invalid: non-deterministic content model for type None: {Wildcard: #any, skip}/{None}:item"). Nevertheless, it feels like a direction that has potential... but I don't know how to make the xsd say "match anything except ``. Anyone know if that's possible? – Daryn Mar 22 '13 at 15:45
  • (...sort of like the question http://stackoverflow.com/a/6468853/135114, which didn't get a solution) – Daryn Mar 22 '13 at 15:57
  • 2
    I was mistaken. I *almost* did the same as @Michael. But the key in you answer (that I overlooked) that made it work is the `namespace="http://www.w3.org/2001/XMLSchema"`. That makes it match only the ` – Daryn Mar 22 '13 at 16:14