Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
247
Selecting Elements in Schemas
By default, the XSD tool creates classes for each top-level element in a
schema. When we are not interested in creating classes for all top-level
elements in a schema, we can explicitly select the top-level elements we would
like to generate classes for. We have to add a /e option for each element to
select to the command-line. Starting with the schema in listing C.2 we can
select the garage element with the following command-line:
xsd.exe garage.xsd /c /l:CS /e:garage
The output file garage.cs only contains the garage and garageCar classes –
the garage class because we specified the garage element on the command line,
the garageCar because it is reference by the cars field of the garage class.
The NewDataSet class is no longer present in the code file:
Selecting individual elements with the /e switch is not the only option to
narrow down the output of the XSD tool. We can select all elements belonging to
an XML namespace with the /u switch. The /u switch takes the namespace URI of
the elements we want to export. We can also combine the /u option with one or
more /e options if we need to further narrow down the selection to specific
elements in a schema. Section C.3.4 explains the use of the /e option in
detail.
To illustrate the use of the /u switch, I broke up the schema from listing
C.2 into two schema files, each describing a different targetNamespace. The
first schema, car.xsd, declares the complexTypecar_t and declares an element
car of this type.
5 Listing C.5
Schema describing a car type
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Cars"
xmlns="urn:christoph-cars"
| #1
targetNamespace="urn:christoph-cars"
|
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType
name="car_t">
| #2
<xs:attribute name="make"
type="xs:string" /> |
<xs:attribute name="model"
type="xs:string" /> |
<xs:attribute name="year"
type="xs:string" /> |
</xs:complexType>
<xs:element name="car"
type="car_t"/>
| #3
</xs:schema>
(annotation) <#1 This schema describes the urn:christoph-cars namespace
>
(annotation) <#2 The namespace contains one type car_t.>
(annotation) <#3 The namespace decares a top-level element car.>
The second schema, garage.xsd, declares a garage type with child elements
desc and car. The car element’s type is car_t from the urn:christoph-cars
namespace described by the first schema. The first schema is <import>ed
by the second schema to make the car_t type available to the elements in the
urn:christoph-cars namespace. Listing C.6 shows the second schema:
6 Listing C.6
Schema for the garage class importing the first schema
(annotation) <#1 This schema describes a different namespace than the
imported schema.>
(annotation) <#2 Declare a prefix for the imported namespace so we can
easily reference it.>
(annotation) <#3 Import the first schema so we have access to the car_t
type.>
(annotation) <#4 Declare an element of the imported type.>
(annotation) <#5 Declare another element named “car” of type car_t in the
urn:christoph-garage namespace.>
To instruct the schema definition tool to generate classes from these two
schemas we have to specify both, the importing schema and the imported schema
on the command-line:
xsd.exe car.xsd garage.xsd /c /l:CS
The output file is one source code file with the two base names
concatenated. In this example the output file is: car_garage.xsd.