JAXB可以接受的XSD Schema

时间:2012-06-11 13:22:55

标签: xsd jaxb

我有一个REST XML响应,其中包含大量数据:

<tag>
  <total-pages type="integer">5</total-pages>
  <previous-page nil="true"></previous-page>
  <next-page nil="true"></next-page>
  <offset type="integer">5</offset>
</tag>

现在,有时数据会像这样回来:

<tag>
  <total-pages type="integer">5</total-pages>
  <previous-page type="integer">0</previous-page>
  <next-page type="integer">1</next-page>
  <offset type="integer">5</offset>
</tag>

我一直在努力想出一个XSD架构结构,它将解释JAXB可接受的两种可能性。

我试过了:

<tag>
  <total-pages type="numeric-type" />
  <previous-page type="numeric-type" />
  <next-page type="numeric-type" />
  <offset type="numeric-type" />
</tag>

<xsd:complexType>
    <xsd:simpleContent>
        <xsd:extension base="xsd:integer">
            <xsd:attribute type="xsd:string" name="type" use="optional">
            <xsd:attribute type="xsd:boolean" name="nil" use="optional">
        </xsd:extension>
    </xsd:simpleContent>
</xsd:complexType>

但JAXB因解组错误而崩溃。

关于我可以使用哪些XSD架构结构来解释返回XML的可变性的想法(我无法更改XML,它来自我无法控制的第三方)。

谢谢,

佩里

1 个答案:

答案 0 :(得分:0)

我开始从您的XML示例生成一个XSD(在您的情况下为两个):

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="tag">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="total-pages">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:unsignedByte">
                <xs:attribute name="type" type="xs:string" use="required" />
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
        <xs:element name="previous-page">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute name="type" type="xs:string" use="optional" />
                <xs:attribute name="nil" type="xs:boolean" use="optional" />
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
        <xs:element name="next-page">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute name="type" type="xs:string" use="optional" />
                <xs:attribute name="nil" type="xs:boolean" use="optional" />
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
        <xs:element name="offset">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:unsignedByte">
                <xs:attribute name="type" type="xs:string" use="required" />
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

当您与XSD进行比较时,您会注意到使用字符串而不是整数作为基本类型。这解释了为什么你有解组问题:空字符串不是有效数字。

如果不是“nil”而是获得xsi:nil属性,那么XSD将以不同的方式生成,即它对于您的元素将具有nillable =“true”。

由于您无法更改XML,因此您不得不使用字符串;或者,您可以在空字符串和数字值之间创建一个联合...至于在JAXB中看起来如何,您可以尝试...