[xsd-users] Custom Types

Ellinger (EXT), Klaus ellinger.klaus.ext at siemens.com
Mon May 23 10:30:52 EDT 2011


Hi Experts,

I am  trying to create custom XSD types but I am not able to do so ... Hopefully some of you can give me an hint what I am doing wrong:

Following my Schema:

########################################################################################################################
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:ppl="http://www.example.org/Condition"
            targetNamespace="http://www.example.org/Condition">
    <xsd:complexType name="condition">
        <xsd:sequence>
            <xsd:element name="name" type="xsd:string" />
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="extract">
        <xsd:complexContent>
            <xsd:extension base="ppl:condition">
                <xsd:attribute name="target" type="xsd:string" use="required" />
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>
    <xsd:complexType name="catalog">
        <xsd:sequence>
            <xsd:element name="condition" type="ppl:condition" maxOccurs="unbounded" />
        </xsd:sequence>
    </xsd:complexType>

    <xsd:element name="catalog" type="ppl:catalog" />
</xsd:schema>
########################################################################################################################


Command to generate:

xsd.exe cxx-tree\
      --root-element catalog\
      --generate-ostream\
      --generate-serialization\
      --output-dir\
      --generate-polymorphic\
      --custom-type "condition=condition_impl<condition_base>/condition_base"\
      --custom-type "extract=extract_impl<extract_base>/extract_base"\
      --generate-forward\
      --fwd-prologue "#include \"Condition-custom_fwd.hxx\""\
      --hxx-prologue "#include \"Condition-custom.hxx\""\
      --output-dir $(XSD_OUTPUT_DIR)\
      $(XSD_INPUT_DIR)\Condition.xsd


Condition-custom_fwd.hxx

namespace Condition
{
      template <typename base>
      class condition_impl;

      template <typename base>
      class extract_impl;
}


Condition-custom.hxx

########################################################################################################################
#include "Condition-fwd.hxx"

namespace Condition {

template<typename base>
class condition_impl: public base {

public:
      condition_impl(const ::xml_schema::string& name);

      condition_impl(const ::xercesc::DOMElement& e, ::xml_schema::flags f = 0, ::xml_schema::container* c = 0);

      condition_impl(const condition_impl& x, ::xml_schema::flags f = 0, ::xml_schema::container* c = 0);

      virtual condition_impl* _clone(::xml_schema::flags f = 0, ::xml_schema::container* c = 0) const;

public:
      virtual void sayHello();

};

template<typename base>
class extract_impl: public base {

public:
      extract_impl(const ::xml_schema::string& name, const ::xml_schema::string& target);

      extract_impl(const ::xercesc::DOMElement& e, ::xml_schema::flags f = 0, ::xml_schema::container* c = 0);

      extract_impl(const extract_impl& x, ::xml_schema::flags f = 0, ::xml_schema::container* c = 0);

      virtual extract_impl* _clone(::xml_schema::flags f = 0, ::xml_schema::container* c = 0) const;

public:
      virtual void sayHello();
};

}
########################################################################################################################


And my Implementation Condition-custom.cxx:


########################################################################################################################
#include "Condition.hxx"

namespace Condition {

      template<typename base>
      condition_impl<base>::condition_impl(const ::xml_schema::string& name) : base(name) { printf("Condition created\n"); }

      template<typename base>
      condition_impl<base>::condition_impl(const ::xercesc::DOMElement& e, ::xml_schema::flags f = 0, ::xml_schema::container* c = 0) : base(e, f, c) { printf("Condition created\n"); }

      template<typename base>
      condition_impl<base>::condition_impl(const condition_impl& x, ::xml_schema::flags f = 0, ::xml_schema::container* c = 0) : base(x, f, c) { printf("Condition created\n"); }

      template<typename base>
      condition_impl<base>* condition_impl<base>::_clone(::xml_schema::flags f = 0, ::xml_schema::container* c = 0) const {
            return new condition_impl(*this, f, c);
      }

      template<typename base>
      void condition_impl<base>::sayHello() {
            printf("I am a Condition\n");
      }

      template class condition_impl<condition_base>;

      template<typename base>
      extract_impl<base>::extract_impl(const ::xml_schema::string& name, const ::xml_schema::string& target) : base(name, target) { printf("Extractor created\n"); }

      template<typename base>
      extract_impl<base>::extract_impl(const ::xercesc::DOMElement& e, ::xml_schema::flags f = 0, ::xml_schema::container* c = 0) : base(e, f, c) { printf("Extractor created\n"); }

      template<typename base>
      extract_impl<base>::extract_impl(const extract_impl& x, ::xml_schema::flags f = 0, ::xml_schema::container* c = 0) : base(x, f, c) { printf("Extractor created\n"); }

      template<typename base>
      extract_impl<base>* extract_impl<base>::_clone(::xml_schema::flags f = 0, ::xml_schema::container* c = 0) const {
            return new extract_impl(*this, f, c);
      }

      template<typename base>
      void extract_impl<base>::sayHello() {
            printf("I am a special Condition\n");
      }

      template class extract_impl<extract_base>;
}
########################################################################################################################


Everything compiles fine without any Erros but if I invoke my sample application:


########################################################################################################################
#include <stdio.h>
#include <stdlib.h>
#include <memory>   // std::auto_ptr
#include <iostream>

#include "Condition.hxx"

int main(int argc, char *argv[]) {
      try {
            using namespace Condition;

            std::auto_ptr < catalog > c(catalog_(".\\Condition.xml"));

            for (catalog::condition_iterator i(c->condition().begin()); i != c->condition().end(); ++i) {
                  i->sayHello();
            }
      } catch (const xml_schema::exception& e) {
            std::cerr << e << std::endl;
            return 1;
      }
}
########################################################################################################################


With following XML:

########################################################################################################################
<?xml version="1.0" encoding="UTF-8"?>
<ppl:catalog xmlns:ppl="http://www.example.org/Condition" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/Condition Condition.xsd ">

  <condition>
    <name>name</name>
  </condition>

  <condition target="Hello1" xsi:type="ppl:extract">
    <name>name1</name>
  </condition>

  <condition target="Hello2" xsi:type="ppl:extract">
    <name>name2</name>
  </condition>

</ppl:catalog>
########################################################################################################################


I got the following result:

Condition created
Condition created
Condition created
I am a Condition
I am a Condition
I am a Condition


I expected following:

Condition created
Extractor created
Extractor created
Hello
I am a Condition
I am a special Condition
I am a special Condition


Any Help appreciated!!!

Thx allot

Klaus








More information about the xsd-users mailing list