使用JAXB schemagen时如何避免继承?

2024-09-27 09:27:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用JAXB注释和schemagenmaven插件来创建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>

对于班级:

^{pr2}$

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

提前谢谢


Tags: name插件base客户机extensionelementxsdsequence
1条回答
网友
1楼 · 发布于 2024-09-27 09:27:30

这是wsdl2py而不是JAXB的一个缺点,但是使用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可能支持也可能不支持的嵌套序列(应该很容易修复)。 目前,它只支持内容模型,但是可以很容易地扩展到复制属性和属性组。在

而且,样式表只在扩展元素与基元素在同一个模式文件中时才起作用。在

祝你好运!在

相关问题 更多 >

    热门问题