这是一个 XSLT 转换 binding.xsl,用于从模板创建这些绑定文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mark="/m/a/r/k"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" >
<xsl:param name = "schema" as = "xs:string" />
<xsl:param name = "type" as = "xs:string" />
<xsl:param name = "element" as = "xs:string" />
<xsl:output indent="yes"/>
<xsl:variable name = "apo" select = "''''" />
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="jaxb:bindings">
<xsl:copy>
<xsl:apply-templates select="*|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="jaxb:bindings[@mark:mark = 'schema']">
<xsl:copy>
<xsl:attribute name="schemaLocation" select='concat("../", $schema, ".xsd")'/>
<xsl:apply-templates select="*|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="jaxb:bindings[@mark:mark = 'type']">
<xsl:copy>
<xsl:attribute name="node"
select='concat("//xs:complexType[@name=", $apo, $type, $apo, "]")'/>
<xsl:apply-templates select="*|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="jaxb:bindings[@mark:mark = 'element']">
<xsl:copy>
<xsl:attribute name="node"
select='concat("//xs:element[@name=", $apo, $element, $apo, "]")'/>
<xsl:apply-templates select="*|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
这是模板 binding.xml:
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
xmlns:mark="/m/a/r/k"
jaxb:version="2.1"
jaxb:extensionBindingPrefixes="xjc inheritance">
<!-- schemaLocation="../Specific.xsd" -->
<jaxb:bindings mark:mark="schema" node="/xs:schema">
<!-- node="//xs:complexType[@name='entity'] -->
<jaxb:bindings mark:mark="type" >
<inheritance:implements>test.IEntity</inheritance:implements>
</jaxb:bindings>
<!-- node="//xs:element[@name='entityContainer']" -->
<jaxb:bindings mark:mark="element">
<jaxb:class/>
<inheritance:implements>test.IEntityContainer</inheritance:implements>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
您可以使用 Saxon HE 执行此操作,像这样调用(单个 shell 命令),最后三个参数根据您的许多 XML 模式文件而变化。
java -cp /extra/saxon/saxon9he.jar net.sf.saxon.Transform \
-s:binding.xml -xsl:binding.xsl \
schema=Specific type=entity element=entityContainer
从 maven 调用它应该不是问题