有没有办法在 JAXB 中处理跨多个 .xsd 文件的重复元素定义? [英] Is there a way to deal with duplicate element definitions across multiple .xsd files in JAXB?

查看:22
本文介绍了有没有办法在 JAXB 中处理跨多个 .xsd 文件的重复元素定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几十个 .xsd 文件,我想为其自动生成代码.当我尝试同时生成所有文件时,有几个文件的名称会发生​​冲突.

I have dozens and dozens .xsd files that I want to auto-generate code for. A couple of the files have duplicate names that clash when I try to generate all of them at the same time.

我专注于尝试让其中的 2 个工作.

I am focusing on just trying to get 2 of these to work.

当我让这两个工作时,我会解决其余的问题.但我现在只关注其中的 2 个文件.我无法控制它们,它们来自供应商并遵循标准",因此出于多种原因,编辑它们不是一种选择.

When I get these 2 working I will fix the rest. But I am just focusing on 2 of these files for now. I am not in control of them, they are from a vendor and follow a "standard", so editing them is not an option for multiple reasons.

我正在使用 maven-jaxb2-plugin 来处理这些文件.

I am using the maven-jaxb2-plugin to process these files.

我添加了一个 binding.xjb 文件,如 mat b 的答案中的链接和我在网上找到的其他说明中的建议.但是我收到以下错误,没有输出.

I added a binding.xjb file as suggested in the link in mat b's answer and other instructions I have found on the web. But I am getting the following errors, an no output.

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings version="2.1"
              xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
              xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
              xmlns:xs="http://www.w3.org/2001/XMLSchema"
              xsi:schemaLocation=" http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">
  <jxb:bindings schemaLocation="mac-3.4.xsd">
    <jxb:schemaBindings>
      <jxb:package name="my.company.mac"/>
    </jxb:schemaBindings>
  </jxb:bindings>
  <jxb:bindings schemaLocation="mac-stylesheet-3.4.xsd">
    <jxb:schemaBindings>
      <jxb:package name="my.company.stylesheet"/>
    </jxb:schemaBindings>
  </jxb:bindings>
</jxb:bindings>

给出以下错误

[ERROR] Error while parsing schema(s).Location [ file:/C:/Users/Jarrod%20Roberson/Projects/spa-tools/spa-lib/src/main/sc
hema/mac-stylesheet-3.4.xsd{165,33}].
org.xml.sax.SAXParseException: 'halign' is already defined

有问题的元素是:(还有很多其他的,这只是第一个冲突的)

The offending element is : ( there are many others this is just the first one that clashes )

<xsd:simpleType name="halign">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="left" />
    <xsd:enumeration value="center" />
    <xsd:enumeration value="right" />
  </xsd:restriction>
</xsd:simpleType>

并且在每个 .xsd 文件中都是相同的,我该如何解决这个重复,要么只生成一个类,要么每个类都生成到它们自己的包命名空间中?

And is identical in each of the .xsd files, how do I resolve this duplication with either only one class being generated or each one being generated in to their own package namespace?

这不是唯一一个像这样重复的元素,它们有很多,所以仅仅尝试从文件中删除它们也不是一个选项.

This isn't the only duplicate element like this there are lots of them, so just trying to remove them from the files isn't a option either.

这个 halign 位于多个 .xsd 文件中,我想将它们放在各自的包中,或者能够告诉编译器使用第一个被生成.

This halign is in multiple .xsd files and I want to either put them in their individual packages, or be able to tell the compiler to use the first one that was generated.

这是我在尝试外部 .xjb 文件之前开始的地方,只需在 pom.xml 中指定 package.

This is where I started off before I tried an external .xjb file, by just specifying the package in the pom.xml.

如何将绑定配置为忽略重复配置、将它们映射到单独的包或将它们映射到现有实现?

推荐答案

我有一个半途而废的解决方案,但它非常耗时.我必须为每个具有重复条目的文件创建一个单独的 .

I have a half-way workaround solution to this, but it is very time intensive. I had to create a separate <execution/> for every one of the files that has duplicate entries.

<executions>
  <execution>
    <id>jaxb-mac</id>
    <phase>generate-sources</phase>
    <goals>
      <goal>generate</goal>
    </goals>
    <configuration>
      <forceRegenerate>true</forceRegenerate>
      <generatePackage>my.company.mac</generatePackage>
      <schemaDirectory>src/main/schema</schemaDirectory>
      <schemaIncludes>
        <include>mac-3.4.xsd</include>
      </schemaIncludes>
    </configuration>
  </execution>
  <execution>
    <id>jaxb-stylesheet</id>
    <phase>generate-sources</phase>
    <goals>
      <goal>generate</goal>
    </goals>
    <configuration>
      <forceRegenerate>true</forceRegenerate>
      <generatePackage>my.company.stylesheet</generatePackage>
      <schemaDirectory>src/main/schema</schemaDirectory>
      <schemaIncludes>
        <include>mac-stylesheet-3.4.xsd</include>
      </schemaIncludes>
    </configuration>
  </execution>

true 很重要,否则只有第一个 会运行,其余的会认为没有需要运行,因为我生成到同一个目录.

The <forceRegenerate>true</forceRegenerate> is important or only the first <execution/> will run and the rest will think that there is no need to run because I am generating into the same directory.

我仍然想要一种劳动强度较低的解决方案来处理重复项.

I would still like a less labor intensive solution to deal with the duplicates.

我想如果我让第一个主 .xsd 成为一个单独的模块,构建到它自己的 .jar 文件中,然后我可以使用 <episode/> 标记并让它跳过生成一遍又一遍地重复相同的元素,因为它们在定义上是相同的.

I think if I make the first master .xsd a separate module that builds into its own .jar file, I could then use the <episode/> tag and have it skip generating the same duplicate elements over and over, since they are identical in definition.

从那以后,我决定尽可能放弃 XML 和 JAXB.在这次编辑中,有更新更好的方法来解析 XML 并将其映射到对象.

这篇关于有没有办法在 JAXB 中处理跨多个 .xsd 文件的重复元素定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆