DEV Community

Cover image for Fix SAXParseException; src-resolve: Cannot resolve the name '…' to a(n) 'type definition'
Adrian Matei for Codever

Posted on • Edited on • Originally published at codever.dev

7 1

Fix SAXParseException; src-resolve: Cannot resolve the name '…' to a(n) 'type definition'

TLDR; - include also the imported xsd file in the schema sources in xml unit validator

When trying to validate an xml document against an xsd schema with xmlunit I get the following exception

org.xmlunit.XMLUnitException: The schema is invalid

    at org.xmlunit.validation.JAXPValidator.validateInstance(JAXPValidator.java:81)
    at ch.mobi.siefs.connector.helper.XmlUnitComparisonTest.givenXml_whenValidatesAgainstXsd_thenCorrect(XmlUnitComparisonTest.java:48)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
.....
Caused by: org.xml.sax.SAXParseException; lineNumber: 89; columnNumber: 98; src-resolve: Cannot resolve the name 'mobits:HistoryTimestamp' to a(n) 'type definition' component.
    at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:204)
    at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:135)
....
Enter fullscreen mode Exit fullscreen mode

The unit test looked like the following:

@Test
public void givenXml_whenValidatesAgainstXsd_thenCorrect() {
  Validator v = Validator.forLanguage(Languages.W3C_XML_SCHEMA_NS_URI);
  v.setSchemaSource(Input.fromStream(
          XmlUnitComparisonTest.class.getResourceAsStream("xsd/bookmark.xsd")).build());
  ValidationResult r = v.validateInstance(Input.fromStream(
          XmlUnitComparisonTest.class.getResourceAsStream("message/bookmark-example.xml")).build());
  Iterator<ValidationProblem> probs = r.getProblems().iterator();
  while (probs.hasNext()) {
    probs.next().toString();
  }
  Assertions.assertThat(r.isValid()).isTrue();
}

Enter fullscreen mode Exit fullscreen mode

The problem element mentioned in the stack trace comes from the imported xsd, which xml wise everything seems ok:

<?xml version="1.0" encoding="utf-8" ?>
<xsd:schema
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:common="http://xml.bookmarks.dev/datatype/common/Commons/v3">
    <xsd:import namespace=""http://xml.bookmarks.dev/datatype/common/Commons/v3" schemaLocation="common-Commons-3.0.xsd"/>
Enter fullscreen mode Exit fullscreen mode

The solution was to add also the imported xsd schema when building the validator in xmlunit

  @Test
  public void givenXml_whenValidatesAgainstXsd_thenCorrect() {
    Validator v = Validator.forLanguage(Languages.W3C_XML_SCHEMA_NS_URI);
    v.setSchemaSources( // order of xsd sources is important
        Input.fromStream(
                getClass().getClassLoader().getResourceAsStream("xsd/bookmarks.xsd"))
            .build(),
        Input.fromStream(
                getClass().getClassLoader().getResourceAsStream("xsd/common-Commons-3.0.xsd"))
            .build());
    ValidationResult r =
        v.validateInstance(
            Input.fromStream(
                    getClass()
                        .getClassLoader()
                        .getResourceAsStream("message/bookmark-example.xml"))
                .build());
    Iterator<ValidationProblem> probs = r.getProblems().iterator();
    while (probs.hasNext()) {
      probs.next().toString();
    }
    assertThat(r.isValid()).isTrue();
  }
Enter fullscreen mode Exit fullscreen mode

Shared with ❤️ from Codever. Use 👉 copy to mine functionality to add it to your personal snippets collection.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay