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

发布于 2024-10-05 09:56:12 字数 638 浏览 0 评论 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>

for class:

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

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

提前致谢

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:

<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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱没勇气 2024-10-12 09:56:12

这是 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 可能支持或不支持的嵌套序列(应该很容易修复)。
目前,它仅支持内容模型,但可以轻松扩展以复制属性和属性组。

此外,仅当扩展元素与基础元素存在于同一架构文件中时,样式表才起作用。

祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文