XSD - 允许元素类型为整数或空

发布于 2024-11-30 09:45:58 字数 152 浏览 4 评论 0原文

我需要能够将简单元素类型设置为整数,但允许它也为空。如果该示例为空并且空白字段不是整数,则此示例会发送错误。我该如何解决这个问题?

<xsd:element name="weight" type="xsd:integer"/>

I need to be able to set a simple element type as an integer but allow it to also be empty. This example sends an error if its empty and a blank field is not an integer. How can I get round this?

<xsd:element name="weight" type="xsd:integer"/>

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

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

发布评论

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

评论(5

dawn曙光 2024-12-07 09:45:58

您所要做的就是对同一元素分配限制并对其进行联合,如下例所示:

<xs:element name="job_code">
  <xs:simpleType>
    <xs:union>
      <xs:simpleType>
        <xs:restriction base='xs:string'>
          <xs:length value="0"/>
        </xs:restriction>
      </xs:simpleType>
      <xs:simpleType>
        <xs:restriction base='xs:integer' />
      </xs:simpleType>
    </xs:union>
  </xs:simpleType>
</xs:element>

通过使用此限制,您可以告诉 xml 验证允许任何整数值,并允许该元素为空。

What you have to do is assign restrictions on the same element plus make a union on them such as the following example:

<xs:element name="job_code">
  <xs:simpleType>
    <xs:union>
      <xs:simpleType>
        <xs:restriction base='xs:string'>
          <xs:length value="0"/>
        </xs:restriction>
      </xs:simpleType>
      <xs:simpleType>
        <xs:restriction base='xs:integer' />
      </xs:simpleType>
    </xs:union>
  </xs:simpleType>
</xs:element>

By using this restriction, you tell the xml validation to allow any integer value and allowing the element if it is empty.

梦醒时光 2024-12-07 09:45:58

我们可以通过在 SimpleType 中

<xs:simpleType name="NullOrInteger">
    <xs:restriction base="xs:string">
         <xs:pattern value="\d*|\s{0}" />
    </xs:restriction>
</xs:simpleType>

添加 NullOrInteger 作为您想要限制整数或空值的类型来实现此目的。

例如:

<xs:element name="null_or_int" type="NullOrInteger" />

We can achieve this by making a SimpleType

<xs:simpleType name="NullOrInteger">
    <xs:restriction base="xs:string">
         <xs:pattern value="\d*|\s{0}" />
    </xs:restriction>
</xs:simpleType>

Add NullOrInteger as the type where you want restriction for an integer or null value.

for example:

<xs:element name="null_or_int" type="NullOrInteger" />
橘寄 2024-12-07 09:45:58

您需要将“nillable”属性设置为 true:

<xsd:element name="weight" type="xsd:integer" nillable="true"/>

请参阅 XML Schema Primer

You need to set the "nillable" attribute as true:

<xsd:element name="weight" type="xsd:integer" nillable="true"/>

See the XML Schema Primer.

过期情话 2024-12-07 09:45:58
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <product>
        <weight xsi:nil="true"/>
    </product>
</products>

尝试上面的方法,应该可以工作;您很可能忘记添加 xsi:nil 属性。另外,请确保权重元素没有作为子元素的字符(空格仍然是不可接受的)。如果您确实必须传递一些字符而不是整数,则必须定义一个联合类型以允许两者。

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <product>
        <weight xsi:nil="true"/>
    </product>
</products>

Try the above, should work; most likely you forgot to add the xsi:nil attribute. Also, make sure that the weight element has no character as children (a white space would still not be acceptable). If you do have to pass some characters instead of an integer, than you have to define a union type to allow for both.

家住魔仙堡 2024-12-07 09:45:58

我通过搜索如何使用 xsd 模式使属性成为可为空整数并基于生成的 JAXB 类得出了这个答案。我在这里没有找到答案,所以在找到答案后,我决定分享它。以下 xsd 部分将生成不可为 null 类型的 int (int):

<xsd:attribute name="length" type="xsd:int" use="required"/>

在 Java 代码中,这将导致:

@XmlAttribute(name = "length", required = true)
protected int length;

如果我们放弃使用 required,我们将得到:

@XmlAttribute(name = "length")
protected Integer length;

希望这对某人有帮助。即使这个答案可能与实际问题无关,我发现对于那些通过进行与我相同的搜索来到达这里的人来说,这里很有用!

I came up to this answer by searching how to get an attribute to be a nullable Integer, using xsd schema, and based on which JAXB classes are generated. I found no answer here, so after I discovered the answer, I decided to share it. The following xsd portion will generate a not nullable type of int (int):

<xsd:attribute name="length" type="xsd:int" use="required"/>

In Java code this will result:

@XmlAttribute(name = "length", required = true)
protected int length;

If we drop the use required, we will get:

@XmlAttribute(name = "length")
protected Integer length;

Hope this helps someone. And even if this answer might not be related to the actual question, I find it useful to be here for those who will get here by doing the same search I did!

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