xsd 相同的元素,不同的类型? [英] xsd same element, different types?

查看:32
本文介绍了xsd 相同的元素,不同的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,XML 文件中的元素在 XSD 中可以是两种不同的类型.

我想要做的是首先将输入的值验证为更严格的类型(如果是填写表单的人),如果没有通过,则将其验证为较不严格的类型(如果是填写表格的组织),否则如果根本不验证,则验证失败.

基本上就是这样:

`<xsd:序列><xsd:element name="formname" type="xsd:string"/><xsd:element name="timestamp" type="xsd:dateTime"/><xsd:element name="sender" type="PersonType" minOccurs="0"/><xsd:element name="receiver" type="OrganizationType" minOccurs="0"/><xsd:element name="signature" type="xsd:string" minOccurs="0"/></xsd:sequence></xsd:complexType>

PersonType 所声明的基本上是一个根据特定模式构造的 12 位数字.组织类型声明的内容基本上只是一个 10 位数字.

所以我逻辑上想做的是把这个:

到序列中,但不能有两个具有相同名称和不同类型的元素.所以我想我必须以其他方式解决它.

我发现的是:http://xsd.stylusstudio.com/2007Oct/post05003.htm

这几乎是我想做的,但我不确定这是否可能.有没有人对如何解决这个问题有任何进一步的想法?

提前致谢.

解决方案

不,你真的不能这样做.您可以选择按照以下方式创建PersonOrOrgSender"复杂类型:

 <xsd:选择><xsd:element minOccurs="0" name="PersonSender" type="PersonType"/><xsd:element minOccurs="0" name="OrgSender" type="OrganizationType"/></xsd:choice></xsd:complexType>

然后使您的发送者"成为该类型的实例:

但是你会得到这样的 XML 数据:

<表格名称>字符串</表格名称><发件人><OrgSender><名称>字符串</名称></OrgSender></发件人>...</FormInfo>

<表格名称>字符串</表格名称><发件人><PersonSender><名称>字符串</名称></PersonSender></发件人>...</FormInfo>

您不能让单个标签(例如发件人")在一种情况下成为一件事,而在另一种情况下成为另一件事 - 一个标签必须恰好具有一种明确定义的类型,并且一劳永逸地具有该类型.

马克

I have a situation where an element in the XML-file can be of two different types in the XSD.

What I want to do is first to validate the entered value to the more strict type (if it's a person that fills out the form) and if that doesn't pull through, validate it to the lesser strict type (if it's an organization that fills out the form), otherwise let the validation fail if it doesn't validate at all.

So this is basically it:

<xsd:complexType name="ForminfoType"> `
    <xsd:sequence>
        <xsd:element name="formname" type="xsd:string" />
        <xsd:element name="timestamp" type="xsd:dateTime" />
        <xsd:element name="sender" type="PersonType" minOccurs="0" />
        <xsd:element name="receiver" type="OrganizationType" minOccurs="0" />
        <xsd:element name="signature" type="xsd:string" minOccurs="0" />
    </xsd:sequence>
</xsd:complexType> 

What the PersonType declares is basically a 12-digit number constructed according to a specific pattern. What the Organization type declares is basically just a 10-digit number.

So what I logically would like to do is to put this:

<xsd:element name="sender" type="OrganizationType" minOccurs="0" />

into the sequence, but you can't have two elements with the same name and different types. So I reckon I must solve it in some other way.

What I have found was this: http://xsd.stylusstudio.com/2007Oct/post05003.htm

Which is pretty much what I want to do, but I'm not sure it's possible. Has anyone got any further ideas on how to solve this?

Thanks in advance.

解决方案

No, you can't really do this. One option you have it to create a "PersonOrOrgSender" complex type along the lines of :

  <xsd:complexType name="PersonOrOrgType">
    <xsd:choice>
      <xsd:element minOccurs="0" name="PersonSender" type="PersonType" />
      <xsd:element minOccurs="0" name="OrgSender" type="OrganizationType" />
    </xsd:choice>
  </xsd:complexType>

and then make your "sender" an instance of that type:

<xsd:element name="sender" type="PersonOrOrgType" minOccurs="0" />

but then you'll have XML data like this:

<FormInfo>
  <formname>string</formname>
  <sender>
    <OrgSender>
      <Name>string</Name>
    </OrgSender>
  </sender>  
  ...
</FormInfo>

or

<FormInfo>
  <formname>string</formname>
  <sender>
    <PersonSender>
      <Name>string</Name>
    </PersonSender>
  </sender>  
  ...
</FormInfo>

You cannot make a single tag (e.g. "sender") be one thing in one case and another in a different case - one tag must have exactly one well-defined type and has that type once and for all times.

Marc

这篇关于xsd 相同的元素,不同的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆