XSD design patterns

纵然是瞬间 提交于 2021-02-19 03:43:27

问题


I start with XML and almost all everything that is involved, working in XML Oxygen editor. I have met 3 basic design patterns of XSD schema and I don't know which one should suit my needs the most and in general.

I want to create a large schema validating a XML file containing teams with players.

My question is which one is the best and is considered to be the most well-arranged, user-friendliest and professional for my purpose and in general? Which one is practically the most common and what do you recommend me as to the beginner with XML to the future?

Here are my samples of all designs I know:


Matryoshka

I have started with this one, because it was simply understandable. No references at referencing to types all. I am sure this one is suitable for tiny files.

<xsd:element name="player">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="skill" type="xsd:float"/>
            <xsd:element name="nationality">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="\d{3}"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
        </xsd:sequence> 
    </xsd:complexType>
</xsd:element>  

Venetian Blind

This is my the most favourite one, because of defining the sctructire in the beginning and then all types of elements.

<xsd:element name="player" type="playerType"/>

<xsd:complexType name="playerType">
        <xsd:sequence>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="skill" type="xsd:float"/>
            <xsd:element name="nationality" type="nationalityType"/>
        </xsd:sequence> 
</xsd:complexType>

<xsd:simpleType name="nationalityType">
    <xsd:restriction base="xsd:string">
        <xsd:pattern value="\d{3}"/>
    </xsd:restriction>
</xsd:simpleType>

Salami Slice

This one was the most recommended me, although I see that XML file can contain only one of elements as well as all of them. I am quite confused of this design.

<xsd:element name="player">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element ref="name"/>
            <xsd:element ref="skill"/>
            <xsd:element ref="nationality"/>
        </xsd:sequence> 
    </xsd:complexType>
</xsd:element>  

<xsd:element name="name" type="xsd:string"/>

<xsd:element name="skill" type="xsd:float"/>

<xsd:element name="nationality">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:pattern value="\d{3}"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:element>

回答1:


My question is which one is the best and is considered to be the most well-arranged, user-friendliest and professional for my purpose and in general?

There is no best XSD design pattern in general — just better ones per particular purposes.

For a good treatment of the relative advantages XSD design patterns, see

  • Global versus Local: A Collectively Developed Set of Schema Design Guidelines
  • Schema scope: Primer and best practices.

If you're working within a sector that has established XSDs, follow the pattern used there. If your company has established conventions, follow those.

If you're learning, have no driving outside influences, and are wondering...

...what do you recommend me as to the beginner with XML to the future?

Here's a reasonable approach for a beginner:

  1. Start with the Matryoshka Russian doll pattern. It is simple, compact, and often used for small examples.
  2. When you come to need to share a type between elements, define it separately as is done in Venetian Blind.
  3. When you come to need to share an element in multiple places, define it separately as is done in Salami Slice.

This way you'll develop a working understanding of the advantages of each approach and, should you need to adopt a uniform strategy on a larger scale in the future, you'll have developed the experience and intuition to make an informed decision.



来源:https://stackoverflow.com/questions/35950540/xsd-design-patterns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!