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?

kutschkem
  • 7,826
  • 3
  • 21
  • 56
  • Do you have a header in your xml? You should add a `xs:schema ... targetNamespace="myns"` to the `xsd` and have `` in the xml. – daniu May 15 '18 at 13:06
  • 1
    @daniu: Not exactly. OP's XML is not using namespaces. See [**duplicate link**](https://stackoverflow.com/q/35411871/290085) for `noNamespaceSchemaLocation` help. – kjhughes May 15 '18 at 13:14
  • @kjhughes I defined the schema inline in the code, is `noNamespaceSchemaLocation` still applicable? – kutschkem May 15 '18 at 13:24
  • Sorry, didn't notice that. Ok, I've add [**duplicate link**](https://stackoverflow.com/q/2342808/290085) that shows a `ResourceResolver` example. – kjhughes May 15 '18 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. – kutschkem May 15 '18 at 14:46
  • For the record: none of the "duplicates" helped, what did in the end help was setting `namespaceAware = true` on the DocumentBuilderFactory. – kutschkem May 15 '18 at 15:17
  • What you say helped is not consistent with the XML you've posted. That XML is not in a namespace. – kjhughes May 15 '18 at 15:42
  • @kjhughes Well what do I know why it helped. I can only say that the shown code and schema + xml is exactly what I am using. – kutschkem May 16 '18 at 06:37

0 Answers0