[xsd-users] Example of Anonymous Types

Lester Memmott lmemmott at yahoo.com
Thu May 31 18:11:32 EDT 2007


Dear xsd-users,

I'm CodeSynthesis XSD on Windows using Microsoft Visual Studio 2005 and found that the schema creation (i.e. Tools -> "Create Schema") tool in VS creates a nice schema (see example 1) which I can readily edit but it always creates the schema using anonymous types.  I can convert the anonymous type schema to named types as shown in Example 2.  Once converted I am able to readily use the Tree parser in Code Synthesis XSD.  (See source code in example 3)

So my question is:  I've seen some info about "--morph-anonymous" and have seen some info in the docs about using anonymous types but I've not found any examples in order to complete a working copy showing this in action.  The key idea is that I would like to still use VS2005 to create the schema but I don't want to go to all the work and convert the anonymous types to named types.  Is it possible?  Can someone point in to the right documentation and examples?

Thanks,
LM


Example 1 - Anonymous Types:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="AkodGuiConfig">
<xs:complexType>
<xs:sequence>
<xs:element name="PredefinedIdentifiers">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="IdentifierDescription">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="Description" type="xs:string" use="required" />
<xs:attribute name="UnitOfMeasure" type="xs:string" use="required" />
<xs:attribute name="IntrinsicDataType" type="xs:string" use="required" />
<xs:attribute name="Multivalued" type="xs:boolean" use="required" />
<xs:attribute name="SupportsEvents" type="xs:boolean" use="required" />
<xs:attribute name="CanBeProjected" type="xs:boolean" use="required" />
<xs:attribute name="DefaultValue" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CurrentlyUsedIdentifiers">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Identifier">
<xs:complexType>
<xs:attribute name="Name" type="xs:string" use="required" />
<xs:attribute name="LastKnownValue" type="xs:string" use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Example 2 - Names Types:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:complexType name="IdentifierDescription_type">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="Description" type="xs:string" use="required" />
<xs:attribute name="UnitOfMeasure" type="xs:string" use="required" />
<xs:attribute name="IntrinsicDataType" type="xs:string" use="required" />
<xs:attribute name="Multivalued" type="xs:boolean" use="required" />
<xs:attribute name="SupportsEvents" type="xs:boolean" use="required" />
<xs:attribute name="CanBeProjected" type="xs:boolean" use="required" />
<xs:attribute name="DefaultValue" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>

<xs:complexType name="Identifier_type">
<xs:attribute name="Name" type="xs:string" use="required" />
<xs:attribute name="LastKnownValue" type="xs:string" use="optional" />
</xs:complexType>

<xs:complexType name="CurrentlyUsedIdentifiers_type">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Identifier" type="Identifier_type">
</xs:element>
</xs:sequence>
</xs:complexType>

<xs:complexType name="PredefinedIdentifiers_type">
<xs:sequence>
<xs:element maxOccurs="unbounded" name="IdentifierDescription" type="IdentifierDescription_type">
</xs:element>
</xs:sequence>
</xs:complexType>

<xs:complexType name="AkodGuiConfig_type">
<xs:sequence>
<xs:element name="PredefinedIdentifiers" type="PredefinedIdentifiers_type">
</xs:element>
<xs:element name="CurrentlyUsedIdentifiers" type="CurrentlyUsedIdentifiers_type">
</xs:element>
</xs:sequence>
</xs:complexType>

<xs:element name="AkodGuiConfig" type="AkodGuiConfig_type">
</xs:element>
</xs:schema>


Example 3 - Code Synthesis Driver Code:
void Test(char * argv[])
{
    std::auto_ptr<AkodGuiConfig_type> h(AkodGuiConfig(argv[1]));

    //Get the PredefinedIdentifiers info within the AkodGuiConfig tag
    for (PredefinedIdentifiers_type::IdentifierDescription::iterator pii(h->PredefinedIdentifiers().IdentifierDescription().begin()); 
        pii != h->PredefinedIdentifiers().IdentifierDescription().end(); 
        ++pii)
    {
        //Get the XML attributes
        cout << "PredefinedIdentifiers Info: " << endl;
        cout << pii->Description() << endl;
        cout << pii->UnitOfMeasure() << endl;
        cout << pii->IntrinsicDataType() << endl;
        cout << pii->Multivalued() << endl;
        cout << pii->SupportsEvents() << endl;
        cout << pii->CanBeProjected() << endl;
        cout << pii->DefaultValue() << endl;
        
        //Get the XML Value (i.e. CDATA / content)
        cout << *pii << endl; 
        cout << endl;
    }

    cout << endl << endl;

    //Get the CurrentlyUsedIdentifiers info within the AkodGuiConfig tag
    for (CurrentlyUsedIdentifiers_type::Identifier::iterator cuii(h->CurrentlyUsedIdentifiers().Identifier().begin()); 
    cuii != h->CurrentlyUsedIdentifiers().Identifier().end(); 
    ++cuii)
    {
        //Get the XML attributes
        cout << "CurrentlyUsedIdentifiers Info: " << endl;
        cout << cuii->Name() << endl;
        if (cuii->LastKnownValue().present())   //check for existance since it is an optional parameter
            cout << cuii->LastKnownValue().get() << endl;
        cout << endl;
    }
}

Example 4 - Example XML document to parse:
<?xml version="1.0" encoding="utf-8"?>
<AkodGuiConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="AkodGuiConfig2.xsd">
<PredefinedIdentifiers> <!-- This section used to describe Identifiers that may commonly be used making it easy to select one in the GUI. -->
<IdentifierDescription Description="Room Temperature"
UnitOfMeasure="Fahrenheit"
IntrinsicDataType="Float32"
Multivalued="false"
SupportsEvents="true"
CanBeProjected="false"
DefaultValue="100">Temperature</IdentifierDescription>
<IdentifierDescription Description="Room Humidity"
UnitOfMeasure="Percent Humidity"
IntrinsicDataType="Float32"
Multivalued="false"
SupportsEvents="false"
CanBeProjected="false"
DefaultValue="99.9">Humidity Level</IdentifierDescription>
<IdentifierDescription Description="Atmospheric pressure outside"
UnitOfMeasure="Inches of Mercury"
IntrinsicDataType="Float32"
Multivalued="false"
SupportsEvents="true"
CanBeProjected="true" 
DefaultValue="1.1">Atmospheric Pressure</IdentifierDescription>
</PredefinedIdentifiers>
<CurrentlyUsedIdentifiers> <!-- The following identifiers are -->
<Identifier Name="Temperature" LastKnownValue="110"/>
<Identifier Name="Humidity Level"/>
</CurrentlyUsedIdentifiers>
</AkodGuiConfig>


More information about the xsd-users mailing list