I am trying to use Axiom to validate the pom file, but found that except the project can be verified successfully, the rest of the nodes have reported errors
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>library</artifactId>
<build>
<plugins>
<plugin>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<configuration>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
<sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
</configuration>
<goals>
<goal>schema</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
<groupId>org.apache.avro</groupId>
<version>1.11.1</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<groupId>org.apache.maven.plugins</groupId>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<artifactId>axiom-api</artifactId>
<groupId>org.apache.ws.commons.axiom</groupId>
<version>1.4.0</version>
</dependency>
<dependency>
<artifactId>axiom-impl</artifactId>
<groupId>org.apache.ws.commons.axiom</groupId>
<scope>runtime</scope>
<version>1.4.0</version>
</dependency>
</dependencies>
<groupId>org.test</groupId>
<modelVersion>4.0.0</modelVersion>
<name>library</name>
<properties>
<junit.version>5.8.2</junit.version>
<logback.version>1.2.3</logback.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<version>1.0-SNAPSHOT</version>
</project>
here's my code
@Test
public void commonValidate() throws JaxenException, SAXException, FileNotFoundException {
OMXMLParserWrapper parser = OMXMLBuilderFactory.createOMBuilder(
new FileInputStream("pom.xml"));
AXIOMXPath xpath = new AXIOMXPath("//*");
List<OMElement> elements = xpath.selectNodes(parser.getDocumentElement());
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
StreamSource schemaSource = new StreamSource(new File("maven-4.0.0.xsd"));
Schema schema = schemaFactory.newSchema(schemaSource);
Validator validator = schema.newValidator();
for (OMElement element : elements) {
try {
validator.validate(element.getSAXSource(true));
System.out.printf("Pom element: %s is valid\n", element.getQName());
} catch (SAXException e) {
//e.printStackTrace();
System.out.println("Pom element is not valid: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error reading pom element: " + e.getMessage());
}
}
}
the xsd file: https://maven.apache.org/xsd/maven-4.0.0.xsd
This is the log printed on the console
Pom element: {http://maven.apache.org/POM/4.0.0}project is valid
Pom element is not valid: cvc-elt.1.a: Cannot find the declaration of element 'modelVersion'.
Pom element is not valid: cvc-elt.1.a: Cannot find the declaration of element 'artifactId'.
Pom element is not valid: cvc-elt.1.a: Cannot find the declaration of element 'build'.
Pom element is not valid: cvc-elt.1.a: Cannot find the declaration of element 'plugins'.
Pom element is not valid: cvc-elt.1.a: Cannot find the declaration of element 'plugin'.
Pom element is not valid: cvc-elt.1.a: Cannot find the declaration of element 'artifactId'.
...
Only the first node is verified, and the rest of the nodes are thrown SAXException.