1

I don't get this xml:

<mappings>
 <mapping>
  <protobufMessage>pb::Header</protobufMessage>
  <rosPackage>std_msgs</rosPackage>
  <rosMessage>Header</rosMessage>
 </mapping>
</mappings>

validated against this schema, which is defined inline in the source code:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
>
<xs:element name="mappings">
    <xs:complexType>
        <xs:sequence>
                <xs:element name="mapping" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="protobufMessage" type="xs:string"/>
                            <xs:element name="rosPackage" type="xs:string"/>
                            <xs:element name="rosMessage" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>

I get the error message

org.xml.sax.SAXParseException; cvc-elt.1: Cannot find the declaration of element 'mappings'. at [...]

The code for validation (xtend code):

def private validate(InputStream is) {
    val parserF = DocumentBuilderFactory.newInstance
    val parser = parserF.newDocumentBuilder 
    val document = parser.parse(is)
    val validatorF = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
    val schemaSource = new StreamSource(new StringReader(schema)) // schema is a String member which contains the schema
    val valSchema = validatorF.newSchema(schemaSource)
    val validator = valSchema.newValidator
    validator.validate(new DOMSource(document))
}

What am I doing wrong here?

8
  • Do you have a header in your xml? You should add a xs:schema ... targetNamespace="myns" to the xsd and have <xml xmlns="myns" ... > in the xml. Commented May 15, 2018 at 13:06
  • 1
    @daniu: Not exactly. OP's XML is not using namespaces. See duplicate link for noNamespaceSchemaLocation help. Commented May 15, 2018 at 13:14
  • @kjhughes I defined the schema inline in the code, is noNamespaceSchemaLocation still applicable? Commented May 15, 2018 at 13:24
  • Sorry, didn't notice that. Ok, I've add duplicate link that shows a ResourceResolver example. Commented May 15, 2018 at 13:31
  • @kjhughes I think I see. What you are saying is that I should use noNamespaceSchemaLocation to provide a URL for the schema, and use LSResourceResolver to resolve that URL to a Stream on the schema. Commented May 15, 2018 at 14:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.