0

I have an images sitemap xml file and I am trying to validate it against a schema using Java code:

URL schemaFile = new URL("https://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd");

Source xmlFile = new StreamSource(new File("web.xml"));
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

Schema schema = schemaFactory.newSchema(schemaFile);

Validator validator = schema.newValidator();
validator.validate(xmlFile);

and the XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<url>
    <loc>https://company.com/abc/is/images/A/AA/AAA/AAAA/A/product-name</loc>
    <image:image>
        <image:loc>https://company.com/abc/is/image//images/A/AA/AAA/AAAA</image:loc>
    </image:image>
</url>


The Java validation error I receive is:

line: 2
column: 123
message: Cannot find the declaration of element 'urlset'.
Ilyas Patel
  • 400
  • 2
  • 11
  • 27
  • Turns out, the answer is here = https://stackoverflow.com/questions/7009285/xml-validation-using-multiple-xsds/7009977 – Ilyas Patel Jun 24 '19 at 18:40

1 Answers1

0

Yes, in the XML, the urlset element has no prefix, so it's considered to be in the default namespace: http://www.sitemaps.org/schemas/sitemap/0.9. However, in the Java code, you are using the wrong schema (different version/namespace): https://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd.

So either change the namespace of urlset XML element (with prefix image maybe?) or load the other schema (v0.9) in your java code.

cdan
  • 3,470
  • 13
  • 27