JAXB XJC如何跳过导入命名空间的Java类生成?
问题:跳过导入命名空间的XML元素生成Java类
我正在为XML Schema生成对应Java类,该Schema地址为https://cwe.mitre.org/data/xsd/cwe_schema_v6.10.xsd。但生成的大部分类都属于导入的http://www.w3.org/1999/xhtml命名空间,并非我需要的内容。手动注释掉该导入语句后,生成类从149个(多为HTML元素)减少至59个(均属目标Schema领域)。
我希望通过在.xjb文件中定义自定义规则,跳过特定XML元素(此处为导入命名空间下的元素)的类生成。尝试相关方案后出现以下错误:
com.sun.istack.SAXParseException2: compiler was unable to honor this class customization. It is attached to a wrong place, or its inconsistent with other bindings.
可通过以下命令测试生成:
xjc http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd -b cwe-bindings.xjb
当前cwe-bindings.xjb内容如下:
<!-- cwe-bindings.xjb --> <jxb:bindings version="3.0" xmlns:jxb="https://jakarta.ee/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <jxb:bindings schemaLocation="http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd" node="//xsd:schema"> <jxb:schemaBindings> <jxb:package name="com.example.cwe"/> </jxb:schemaBindings> <!-- rename the 'lang' attribute on line 302 to 'langAttribute' --> <jxb:bindings node="//xsd:attributeGroup[@name='i18n']/xsd:attribute[@name='lang']"> <jxb:property name="langAttribute"/> </jxb:bindings> <!-- rename the 'lang' attribute on line 1166 to 'langAttribute' --> <jxb:bindings node="//xsd:element[@name='bdo']/xsd:complexType/xsd:complexContent/xsd:extension/xsd:attribute[@name='lang']"> <jxb:property name="langAttribute"/> </jxb:bindings> </jxb:bindings> </jxb:bindings>
内容的提问来源于stack exchange,提问作者1Z10
相关产品推荐
相关产品推荐

