0

我正在使用 JAXB 注释和 schemagen maven 插件来创建一个 xsd。我需要使用 wsdl2py 处理该 xsd 以创建 Python 客户端。但是由于我在我的类中有继承,schemagen 创建了这样的东西:

<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>

上课:

class B extends A{
  @XmlElement(required="true")
  private String field1;
}

问题是 wsdl2py 不理解 xs:complexContent 和 xs:extension。所以我想在没有继承的情况下生成 xsd。

提前致谢

4

1 回答 1

0

这是 JAXB 而不是 JAXB 的一个缺点wsdl2py,但使用 XSLT 或 XQuery 很容易解决。快速尝试在 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>

几点注意事项:这仅适用于扩展,不适用于限制,并使用wsdl2py可能支持或不支持的嵌套序列(应该很容易修复)。目前,它只支持内容模型,但可以很容易地扩展为复制属性和attributeGroups。

此外,样式表仅在扩展元素存在于与基础相同的模式文件中时才有效。

祝你好运!

于 2011-01-09T13:38:41.030 回答