开发者

XSD abstract types and polymorphism

开发者 https://www.devze.com 2023-02-13 09:19 出处:网络
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'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 latt开发者_开发知识库er 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 !

0

精彩评论

暂无评论...
验证码 换一张
取 消