[英]Generate XSD from XML using xmlbeans, inst2xsd and Maven
I have an XML file I want to generate an XSD schema from, using xmlbeans , specifically inst2xsd .我有一个 XML 文件,我想从中生成一个 XSD 模式,使用xmlbeans ,特别是inst2xsd 。 I'd like to package the script so it can be run via Maven. I could not find any documentation how to run inst2xsd when installing xmlbeans using Maven. This is my pom.xml
so far:我想要 package 脚本,以便它可以通过 Maven 运行。在使用 Maven 安装 xmlbeans 时,我找不到任何如何运行 inst2xsd 的文档。到目前为止,这是我的pom.xml
:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>de.wolkenarchitekt</groupId>
<artifactId>xml-to-xsd</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
</project>
Installing this via mvn install
works.通过mvn install
可以安装它。 Just for the reference - not important for the answer - I'm building it via Docker, so I'm using OpenJDK14:仅供参考 - 对于答案并不重要 - 我正在通过 Docker 构建它,所以我使用的是 OpenJDK14:
FROM maven:3.6.3-openjdk-14-slim
RUN mkdir -p /opt/workspace
WORKDIR /opt/workspace
COPY pom.xml .
RUN mvn install
Now how do I run the executable for inst2xsd
after installing xmlbeans via Maven?现在如何在通过 Maven 安装inst2xsd
后运行 inst2xsd 的可执行文件?
You can use the Exec Maven Plugin to invoke the class Inst2Xsd
.您可以使用Exec Maven 插件调用 class Inst2Xsd
。 This class is the one actually called from the inst2xsd
shell script.这个 class 实际上是从inst2xsd
shell 脚本中调用的。
If you do not need xmlbeans
in your project - once your XSD is generated - you can event define this dependency only for that task.如果您的项目中不需要xmlbeans
- 一旦生成 XSD - 您可以仅为该任务定义此依赖项。
Consider the following XML document:考虑以下 XML 文档:
<?xml version="1.0" encoding="UTF-8" ?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
In the example we will name it food-menu.xml
and save it in src/main/resources
.在示例中,我们将其命名为food-menu.xml
并将其保存在src/main/resources
中。
You can generate the XML schema as follows (the following example is derived from the code that you can find in the plugin documentation ):您可以按如下方式生成 XML 架构(以下示例源自您可以在插件文档中找到的代码):
<project>
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includeProjectDependencies>false</includeProjectDependencies>
<includePluginDependencies>true</includePluginDependencies>
<mainClass>org.apache.xmlbeans.impl.inst2xsd.Inst2Xsd</mainClass>
<arguments>
<!-- Add as many arguments as you need -->
<argument>-outDir</argument>
<argument>${project.build.outputDirectory}</argument>
<argument>-validate</argument>
<argument>${project.basedir}/src/main/resources/food-menu.xml</argument>
</arguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<!-- ... -->
</project>
Just run mvn exec:java
from your terminal or command line and the schema will be generated according to the arguments passed to Inst2Xsd
.只需从终端或命令行运行mvn exec:java
,就会根据传递给Inst2Xsd
的 arguments 生成模式。
The use of docker should not be a problem.使用docker应该没有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.