Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
| Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 4th, 2012, 09:50 AM
Authorized User
Points: 82, Level: 1
Points: 82, Level: 1 Points: 82, Level: 1 Points: 82, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to declare some elements as optional in xsd.

Hi,
I have xml file like this
Code:
<?xml version="1.0" encoding="UTF-8"?>
<SWService xsi:schemaLocation="file:///V:/main/service.xsd"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://axis.com/service"  
   Version="1.0">

 <Service Id="CTRL" Name="Controlunit">
  <Description>Provides some information </Description>
  <ServiceState>
    <State Id="NOM">
      <Required>
        <SupplierState SN="1" Id="NOM"/>
        <SupplierState SN="2" Id="NOM"/>
      </Required>
    </State>
  </ServiceState>
 <ServiceCustomer/>
 <ServiceSupplier>
   <SW Service="EEC3" Path="/work/service.xml" SN="1"/>
   <SW Service="PCTRL" Path="/work/service1.xml" SN="2"/> 
 </ServiceSupplier>
</Service>

<Service Id="MEC3" Name="MEC3 request">
  <Description>Choose request to use </Description>
  <ServiceCustomer>
    <SW Service="CTRL" Path="/work/service.xml"/>
  </ServiceCustomer>
 <ServiceSupplier>
  <SW Service="INJ" Path="/work/service.xml" SN="2"/>
  <SW Service="P3" Path="/work/service1.xml" SN="3"/>
  <SW Service="NOS" Path="/work/service2.xml" SN="1"/>
 </ServiceSupplier>
</Service>
</SWService>
I cant understand how to represent ServiceState tag in xsd
Code:
  <ServiceState>---</ServiceState>
some times this element is there but some times in some service nodes there is no ServiceState element , how to represent it in xsd. I represented remaining elements as like below
Code:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wmh="http://www.wmhelp.com/2003/eGenerator" elementFormDefault="qualified">

 <xs:element name="SWService" type="SWServiceType"/>

  <xs:complexType name="SWServiceType"> 
    <xs:sequence>
  <xs:element name="Service" type="ServiceType" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ServiceType">
  <xs:complexContent>
  <xs:extension base="IdType">
  <xs:sequence>
    <xs:element name="Description" type="xs:string" maxOccurs="1" minOccurs="0"/>
    <xs:element name="ServiceCustomer" type="ServiceCustomerType" maxOccurs="1"
    minOccurs="0"/>
    <xs:element name="ServiceSupplier" type="ServiceSupplierType" maxOccurs="1"  
    minOccurs="0"/>
  </xs:sequence>
  <xs:attribute name="Name" type="xs:string" use="required"/>
  </xs:extension>
  </xs:complexContent>
   </xs:complexType>
   ---------------------------
   --------------------
  </xs:schema>
but I am confusing to represent ServiceState element because some times present and some times not present , how to declare this element as optional.

thanks in advance
  #2 (permalink)  
Old January 4th, 2012, 09:59 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

To declare an element at optional you set minOccurs="0" - which you already appear to be doing for other elements.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
  #3 (permalink)  
Old January 4th, 2012, 01:17 PM
Authorized User
Points: 82, Level: 1
Points: 82, Level: 1 Points: 82, Level: 1 Points: 82, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi samjudson,
Thanks for your reply, but small doubt I need declare only minOccurs="0" or I need to declare minOccurs="0" maxOccurs="1" both. and also I have small question I am using xmlpad to validate xsd , xmlpad will generate documentation for xsd, I write some xml comments in the xsd that comments are not appeared in documentation , how to do that, if you can you give me some suggestion.

Last edited by pavanibiji; January 4th, 2012 at 01:21 PM..
  #4 (permalink)  
Old January 4th, 2012, 06:21 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You don't need to specify maxOccurs="1" unless you only want a maximum of 1 items of that element.

XSD can be documented using xs:annotation elements so that's probably what you want.

http://www.w3schools.com/schema/el_annotation.asp
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?


Similar Threads
Thread Thread Starter Forum Replies Last Post
Remove Namespaces and Process Optional Elements?! jgec1981 XSLT 5 July 19th, 2010 11:41 AM
Optional element exists if has inner elements 2BOrNot2B XML 0 May 9th, 2008 02:11 PM
Inserting into a tree with optional elements iceandrews XSLT 2 April 24th, 2008 08:59 AM
how to control optional elements and attributes NEO1976 XSLT 5 September 4th, 2006 02:58 AM
making records optional in an XSD cmiller66 XML 2 August 29th, 2006 08:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.