Need help on xml-schema

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Need help on xml-schema

Taranisen Mohanta
Hi,
 I need a help on the following xml to have a schema.
Looked into the followin link but did not make work
 
<?xml version="1.0" encoding="UTF-8"?>
<overall_operation>
 <operation type="list_products">
                <ops_description>Listing all the products of a company</ops_description>
                    <module>powesystem</module>
                          <comp_name>APC</comp_name>
                          <prod_price>50K$</prod_pric>
                          <manf_date>2001</manf_date>
                          <pool_name>Electrical</pool_name>
                          <fail_retry>2</fail_retry>
                        <storage_type>avialble</storage_type>
                        <storage_check>false</storage_check>
                        <api_type>sync</api_type>
                        <product_name>transformer</product_name>
 </operation>
 <operation type="search_product">
                <ops_description>Search the products of a company from the repository</ops_description>
                    <module>high-voltage</module>
                         <module>powesystem</module>
                          <comp_name>APC</comp_name>
                          <pool_name>Electrical</pool_name>
                          <fail_retry>2</fail_retry>
                        <storage_type>avialble</storage_type>
                        <storage_check>false</storage_check>
                        <api_type>sync</api_type>
                        <product_name>setup-transformer</product_name>
 </operation>
        <oeration>
   .
.
.
.     
        </operation>
 </overall_operation>
 
 
Here different elements with operation like list_products,search_products and so on.
Each elements will have some comman attributes such as ops_description,module and so on.
Also some of the uniquie attributes for each elements such as prod_price,manf_date etc.
I want to have a xml schema to validate. Some of the attributes also optional.
I tried using abstract and derived but could not make it work.
Thanks in advance.
Reply | Threaded
Open this post in threaded view
|

Re: Need help on xml-schema

Michael Kay
On 18/02/2011 07:37, Taranisen Mohanta wrote:
> Hi,
>  I need a help on the following xml to have a schema.
>
If you're prepared to change the "type" attribute to "xsi:type" this
becomes straightforward; you can define different subtypes of some
common type, and choose which one the instance should be validated
against using the xsi:type attribute. (But then, if you can do that,
you're probably better changing the element name so <operation
type="list_products"> becomes <operation_list_products>).

If you're prepared to use XSD 1.1, you can do the same using any
attribute, using the new feature of Conditional Type Assignment.

Otherwise, it can't be done in XSD 1.0 alone.

Michael Kay
Saxonica


> <?xml version="1.0" encoding="UTF-8"?>
> <overall_operation>
> <operation type="list_products">
> <ops_description>Listing all the products of a company</ops_description>
> <module>powesystem</module>
> <comp_name>APC</comp_name>
> <prod_price>50K$</prod_pric>
> <manf_date>2001</manf_date>
> <pool_name>Electrical</pool_name>
> <fail_retry>2</fail_retry>
> <storage_type>avialble</storage_type>
> <storage_check>false</storage_check>
> <api_type>sync</api_type>
> <product_name>transformer</product_name>
> </operation>
> <operation type="search_product">
> <ops_description>Search the products of a company from the
> repository</ops_description>
> <module>high-voltage</module>
> <module>powesystem</module>
> <comp_name>APC</comp_name>
> <pool_name>Electrical</pool_name>
> <fail_retry>2</fail_retry>
> <storage_type>avialble</storage_type>
> <storage_check>false</storage_check>
> <api_type>sync</api_type>
> <product_name>setup-transformer</product_name>
> </operation>
> <oeration>
>    .
> .
> .
> .
> </operation>
> </overall_operation>
> Here different elements with operation like
> list_products,search_products and so on.
> Each elements will have some comman attributes such as
> ops_description,module and so on.
> Also some of the uniquie attributes for each elements such as
> prod_price,manf_date etc.
> I want to have a xml schema to validate. Some of the attributes also
> optional.
> I tried using abstract and derived but could not make it work.
> Thanks in advance.


Reply | Threaded
Open this post in threaded view
|

Re: Need help on xml-schema

xmlplus custodians
One option, as Mike suggested,  is to keep common elements/attributes in a type and derive types from there as needed to add additional elements/attributes in derived types. Then "xsi:type" can be used in instance document to specify the actual type.

Another option, IMO, is to use model-group eg. sequence with minOccurs="0" at appropriate place in operation's type and add all optional elements there.
A sample pair of xsd and xml  files, is attached.
The xsd is simplfied to use xsd:string in most places, however it should be modified to use simpleTypes constrained by facets like enumerations, patterns etc for several types like manf_date, comp_name, storage_type, api_type etc.

HTH,
Satya

On Fri, Feb 18, 2011 at 8:33 PM, Michael Kay <[hidden email]> wrote:
On 18/02/2011 07:37, Taranisen Mohanta wrote:
Hi,
 I need a help on the following xml to have a schema.

If you're prepared to change the "type" attribute to "xsi:type" this becomes straightforward; you can define different subtypes of some common type, and choose which one the instance should be validated against using the xsi:type attribute. (But then, if you can do that, you're probably better changing the element name so <operation type="list_products"> becomes <operation_list_products>).

If you're prepared to use XSD 1.1, you can do the same using any attribute, using the new feature of Conditional Type Assignment.

Otherwise, it can't be done in XSD 1.0 alone.

Michael Kay
Saxonica



<?xml version="1.0" encoding="UTF-8"?>
<overall_operation>
<operation type="list_products">
<ops_description>Listing all the products of a company</ops_description>
<module>powesystem</module>
<comp_name>APC</comp_name>
<prod_price>50K$</prod_pric>
<manf_date>2001</manf_date>
<pool_name>Electrical</pool_name>
<fail_retry>2</fail_retry>
<storage_type>avialble</storage_type>
<storage_check>false</storage_check>
<api_type>sync</api_type>
<product_name>transformer</product_name>
</operation>
<operation type="search_product">
<ops_description>Search the products of a company from the repository</ops_description>
<module>high-voltage</module>
<module>powesystem</module>
<comp_name>APC</comp_name>
<pool_name>Electrical</pool_name>
<fail_retry>2</fail_retry>
<storage_type>avialble</storage_type>
<storage_check>false</storage_check>
<api_type>sync</api_type>
<product_name>setup-transformer</product_name>
</operation>
<oeration>
  .
..
..
..
</operation>
</overall_operation>
Here different elements with operation like list_products,search_products and so on.
Each elements will have some comman attributes such as ops_description,module and so on.
Also some of the uniquie attributes for each elements such as prod_price,manf_date etc.
I want to have a xml schema to validate. Some of the attributes also optional.
I tried using abstract and derived but could not make it work.
Thanks in advance.





--
Best Regards,
Satya Prakash Tripathi



ops.xsd (1K) Download Attachment
ops.xml (1K) Download Attachment