XSD 抽象类型和多态性
我正在尝试将以下情况建模为 XSD 架构,但有点卡住了,不确定 XSD 是否支持我想要实现的目标。
我有两个复杂的元素,它们都应该是抽象的:
- Session
- SessionResult
它们的关系:一个 Session 可以包含零个或多个 SessionResults
然后我有这些其他复杂类型(非抽象):
- RaceSession (继承自 Session)
- TimedSession (继承自 Session)
- RaceSessionResult(继承自SessionResult)
- TimedSessionResult(继承自SessionResult)
我想要实现的是,当Session的类型为RaceSession时,只允许RaceSessionResult元素作为SessionResult。随后,当Session被定义为TimedSession时,其SessionResult元素应该是TimedSessionResult。
现在我无法在 XSD 中实现这一点。目前作为解决方法,我没有将 SessionResult 定义为 Session 的子元素列表。 Session 作为 TimedSession 和 RaceSession 的基础,后两者分别只有 TimeSessionResult 或 RaceSessionResult 元素。
有什么方法可以实现我上面在 XSD 中描述的内容吗?目的是从中生成 C# 类。
这是我正在尝试做的 C# 等效项。
public class Event
{
public List<Session> Sessions { get; set; }
}
public abstract class Session
{
public string Weather { get; set; }
public List<SessionResult> SessionResults { get; set; }
}
public abstract class SessionResult
{
public int Position { get; set; }
}
public class RaceSession : Session
{
public new List<RaceSessionResult> SessionResults { get; set; }
}
public class TimedSession : Session
{
public new List<TimedSessionResult> SessionResults { get; set; }
}
public class RaceSessionResult : SessionResult
{
public int StartPosition { get; set; }
}
public class TimedSessionResult : SessionResult
{
public decimal BestLapTime { get; set; }
}
非常感谢您的任何意见!
I'm trying to model the following situation into an XSD schema, but kinda stuck and not sure if XSD supports what I'm trying to achieve.
I have two complex elements, which both should be abstract:
- Session
- SessionResult
Their relationship: one Session can contain zero or more SessionResults
Then I have these other complex types (non-abstract):
- RaceSession (inherits from Session)
- TimedSession (inherits from Session)
- RaceSessionResult (inherits from SessionResult)
- TimedSessionResult (inherits from SessionResult)
What I want to achieve is that, when a Session is of type RaceSession, only RaceSessionResult elements are allowed as SessionResult. Subsequently, when a Session is defined as a TimedSession, its SessionResult elements should be of TimedSessionResult.
Right now I'm not able to achieve this in XSD. For now as a workaround, I did not define SessionResult as a subelement-list of Session. Session serves as base for TimedSession and RaceSession, and these latter two just have TimeSessionResult or RaceSessionResult elements respectively.
Is there any way to achieve what I described above in XSD ? The purpose is then to generate c# classes out of them.
Here's the C# equivalent of what I'm trying to do.
public class Event
{
public List<Session> Sessions { get; set; }
}
public abstract class Session
{
public string Weather { get; set; }
public List<SessionResult> SessionResults { get; set; }
}
public abstract class SessionResult
{
public int Position { get; set; }
}
public class RaceSession : Session
{
public new List<RaceSessionResult> SessionResults { get; set; }
}
public class TimedSession : Session
{
public new List<TimedSessionResult> SessionResults { get; set; }
}
public class RaceSessionResult : SessionResult
{
public int StartPosition { get; set; }
}
public class TimedSessionResult : SessionResult
{
public decimal BestLapTime { get; set; }
}
Thanks a ton for any input !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论