how to avoid inheritance when using JAXB schemagen?
I'm using JAXB annotations and schemagen maven plugin to create an xsd. I need to process that xsd with wsdl2py to create a Python's client. But as I have inheritance in my classes, schemagen creates something like this:
开发者_JAVA百科<xs:complexType name="b">
<xs:complexContent>
<xs:extension base="a">
<xs:sequence>
<xs:element name="field1" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
for class:
class B extends A{
@XmlElement(required="true")
private String field1;
}
The problem is that wsdl2py doesn't understand xs:complexContent and xs:extension. So I'd like to generate the xsd without that inheritance.
Thanks in advance
This is a shortcoming of wsdl2py
rather than JAXB, but it's so easy to fix, using XSLT or XQuery. A quick attempt to fix this in XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsl:template match="xsd:complexType[xsd:complexContent/xsd:extension]">
<xsd:complexType>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="xsd:annotation" />
<xsd:sequence>
<xsl:variable name="typeQName" select="string(xsd:complexContent/xsd:extension/@base)" />
<xsl:variable name="typeName"><xsl:choose>
<xsl:when test="contains($typeQName, ':')">
<xsl:value-of select="substring-after($typeQName, ':')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$typeQName" />
</xsl:otherwise>
</xsl:choose></xsl:variable>
<xsl:comment>Included from <xsl:value-of select="$typeQName" />):
</xsl:comment>
<xsl:apply-templates select="//xsd:complexType[@name=$typeName]/*" />
<xsl:comment>Original extension:</xsl:comment>
<xsl:apply-templates select="xsd:complexContent/xsd:extension/*" />
</xsd:sequence>
<xsl:apply-templates
select="xsd:attribute | xsd:attributeGroup | xsd:attributeGroup" />
</xsd:complexType>
</xsl:template>
<!-- General copy rule -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
A couple of notes: This only works for extension, not restrictions, and uses a nested sequence which wsdl2py
may or not support (should be easy to fix).
Currently, it only supports the content model, but could be easyly to extend to copy attributes and attributeGroups.
Also, the stylesheet only works when the extended element exists in the same schema file as the base.
Good luck!
精彩评论