BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
Schemas, xsd, xdr
Specifying Namespaces
XSD targetNamespace
XSD Schemas
Convert XDR to XSD
Learn XSD Schemas .NET Tutorial
Conversion of genealogical files to XML with a DTD
AccessXML
Sorting by date without using a schema
Validating and enforcing a list of attribute values
Entity name support without DTDs
The Loss of Formatting When Adding a Schema
simple xml notepad
Schemas using DBMS_XMLQuery Package
Tips for Designing a DTD - Part 1 of 4
Tips for Designing a DTD- Part 2 of 4 : Semi-Structured Documents
Tips for Designing a DTD- Part 3 of 4 : Database Oriented Data
Tips for Designing a DTD- Part 4 of 4 : Object Oriented Data
Getting the value of an element with optional subelements
XML Code Generator: Generating Wrapper Classes from an XML Schema
What is WDDX?
<< reBlogger
SEO >>

By :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:

4           C.4 Code file for the selected garage element

using System.Xml.Serialization;

[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]

public class garage {   

    public string desc;

    [System.Xml.Serialization.XmlElementAttribute("car")]

    public garageCar[] car;

}

public class garageCar {   

    [System.Xml.Serialization.XmlAttributeAttribute()]

    public string make;

    [System.Xml.Serialization.XmlAttributeAttribute()]

    public string model;

    [System.Xml.Serialization.XmlAttributeAttribute()]

    public string year;

}

Selecting Classes by their XML Namespace

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

<?xml version="1.0" encoding="utf-8"?>

<xs:schema id="NewDataSet"

  xmlns="urn:christoph-garage"                          | #1

  targetNamespace="urn:christoph-garage"                |

  xmlns:xs="http://www.w3.org/2001/XMLSchema"

  xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"

  xmlns:c="urn:christoph-cars">                         | #2

  <xs:import namespace="urn:christoph-cars" schemaLocation="cars.xsd" />    | #3

  <xs:element name="garage">

    <xs:complexType>

      <xs:sequence>

        <xs:element name="desc" type="xs:string" minOccurs="0" />

        <xs:element name="car" type="c:car_t"           | #4

          minOccurs="0" maxOccurs="unbounded">

        </xs:element>

      </xs:sequence>

    </xs:complexType>

  </xs:element>

  <xs:element name="car" type="c:car_t"/>               | #5

</xs: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.


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

An immediate job opportunity as a B
Software Developers Needed in Charl
Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C

View all Jobs (Add yours)
View all CV (Add yours)



answering service
it help desk
swimming pool contractor
international conference call
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP