Question

Consider the following XSD fragment:

<xs:element name="Persons">
    <xs:sequence>
        <xs:element name="Person" type="personType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
</xs:element>
<xs:complexType name="personType">
    <xs:sequence>
        <xs:element name="First_Name" type="xs:string"/>
        <xs:element name="Last_Name" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

Now consider an XML-based application that, when creating a new document using this XSD, should instantiate some elements, with particular values, by default. For example:

<Persons>
    <Person>
        <First_Name>Joe</First_Name>
        <Last_Name>Bloggs</Last_Name>
    </Person>
    <Person>
        <First_Name>John</First_Name>
        <Last_Name>Doe</Last_Name>
    </Person>
</Persons>

I could hard-code this behaviour as part of the business logic of my application.

However, consider that I may wish to customise those default people for different organisations that my application is deployed into.

It seems to me that a more elegant solution (than hard-coding, with some measure of configurability) would be to somehow bind default instantiation rules to particular elements described in the XSD, perhaps using some other technology or standard that I'm not aware of.

Does such a technology or standard exist? Or maybe there are other ways to accomplish this that I have not considered?

Was it helpful?

Solution

I don't know of any such standard or technology.

What you can possibly do is to add custom information within the schema using the xs:appinfo element:

<xs:complexType name="personType">
    <xs:documentation>
      <xs:appInfo>
         <xsext:sampleData xmlns:xsext="some namespace">
           <Person>
             <First_Name>Joe</First_Name>
             <Last_Name>Bloggs</Last_Name>
           </Person>
         </xsext>
      </xs:appInfo>
    </xs:documentation>
    <xs:sequence>
        <xs:element name="First_Name" type="xs:string"/>
        <xs:element name="Last_Name" type="xs:string"/>
    </xs:sequence>
</xs:complexType>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top