specify type for IDREF in XML schema(在 XML 模式中指定 IDREF 的类型)
问题描述
我正在使用 xjc 从 XML 模式生成 Java 对象.我想使用 IDREF 在文档中多次引用同一个元素.我还想将 IDREF 引用的对象限制为特定类型.我想这样做是出于模式验证的目的,但也是为了在 Java 代码中,引用的对象作为特定类型而不是 Object 类型返回.例如,假设我想要一个模式来描述以下内容:
I am generating Java objects from an XML schema using xjc. I would like to reference the same element multiple times within the document using IDREF. I would also like to constrain the objects referenced by IDREF to a specific type. I'd like to do this for the purposes of schema validation, but also so that in the Java code, the referenced object is returned as a specific type instead of type Object. For example, say I want a schema to describe the following:
我可以这样定义player
:
但是在 Java 代码中,当我尝试检索一个播放器时,它会作为类型对象返回,我必须将它转换为一个人.那时,如果有人错误地引用了 Team 对象,我就有可能在验证时发现的错误要处理.我想指定如下内容:
but then in the Java code, when I try to retrieve a player it will come back as type object, and I have to cast it to a person. At that point, if someone has mistakenly referenced a Team object, I have errors to deal with that could have been caught at validation. I want to specify something like this:
<xs:element name="player" type="xs:IDREF"
reftype="person"
maxOccurs="无界"/>
但据我所知,没有办法像我在此处使用人为属性reftype"所做的那样指定类型.这可以使用 IDREF 完成吗?如果没有,还有其他方法吗?
But as far as I can tell, there is no way to specify a type as I have done here with the contrived attribute 'reftype'. Can this be done, using IDREF? If not, is there another method?
推荐答案
您可以简单地将 baseType
绑定应用到您的 player
元素.比如:
You can simply apply baseType
binding to your player
element. Something like:
您可能需要为您的架构找出正确的绑定位置.
You may need to figure out the correct binding location for your schema.
我的代码示例:
架构:
绑定:
生成的代码:
参见:https://svn.java.net/svn/hj3~svn/trunk/ejb/tests/issues-jpa2/src/main/resources/
这篇关于在 XML 模式中指定 IDREF 的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!