Solved

Generate validated XML

Posted on 2007-04-02
14
780 Views
Last Modified: 2012-05-05
Hi experts,

I'm having a slight problem ;)

For a client of ours I need to create validated XML based upon a schema they provided me with. Now it wouldn't have to be a problem, if the XSD file would contain a root element, but it doesn't contain that.

I won't post the entire XSD here, as that is very lengthy (about 7.000 lines long) but I think I will need to post a part of it for you guys to be able to help me...

The Schema:
-------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2006 U (http://www.altova.com) by Exact ADC (Exact ADC) -->
<!-- edited with XML Spy v4.4 U (http://www.xmlspy.com) by Ronald Voets (Exact Asia Development Centre) -->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified" version="4.0.310.1">
      <!-- General attributes -->
      <!-- MAATWERK: List of supported topics put in a type definition so it can be expanded in another schema -->
      <xsd:complexType name="rootNode">
            <xsd:sequence>
                  <xsd:element ref="Settings" minOccurs="0"/>
                  <xsd:element ref="ExchangeRates" minOccurs="0"/>
                  <xsd:element ref="Accounts" minOccurs="0"/>
                  <xsd:element ref="Assortments" minOccurs="0"/>
                  <xsd:element ref="Items" minOccurs="0"/>
                  <xsd:element ref="ItemPrices" minOccurs="0"/>
                  <xsd:element ref="Resources" minOccurs="0"/>
                  <xsd:element ref="GLAccounts" minOccurs="0"/>
                  <xsd:element ref="Journals" minOccurs="0"/>
                  <xsd:element ref="VATs" minOccurs="0"/>
                  <xsd:element ref="FinEntries" minOccurs="0"/>
                  <xsd:element ref="GLEntries" minOccurs="0"/>
                  <xsd:element ref="Budgets" minOccurs="0"/>
                  <xsd:element ref="InternalUses" minOccurs="0"/>
                  <xsd:element ref="DocumentTypes" minOccurs="0"/>
                  <xsd:element ref="DocumentGroups" minOccurs="0"/>
                  <xsd:element ref="Documents" minOccurs="0"/>
                  <xsd:element ref="Projects" minOccurs="0"/>
                  <xsd:element ref="Assets" minOccurs="0"/>
                  <xsd:element ref="Invoices" minOccurs="0"/>
                  <xsd:element ref="Orders" minOccurs="0"/>
                  <xsd:element ref="ProductionOrders" minOccurs="0"/>
                  <xsd:element ref="Contracts" minOccurs="0"/>
                  <xsd:element ref="Requests" minOccurs="0"/>
                  <xsd:element ref="Messages" minOccurs="0"/>
                  <xsd:element ref="ResourceRoles" minOccurs="0"/>
                  <xsd:element ref="PayrollComponentTypes" minOccurs="0"/>
                  <xsd:element ref="PayrollResources" minOccurs="0"/>
                  <xsd:element ref="PayrollTransactions" minOccurs="0"/>
                  <xsd:element ref="Costcenters" minOccurs="0"/>
                  <xsd:element ref="Costunits" minOccurs="0"/>
                  <xsd:element ref="Currencies" minOccurs="0"/>
                  <xsd:element ref="Topics" minOccurs="0"/>
            </xsd:sequence>
            <xsd:attribute name="schemaVersion" type="xsd:string" use="optional"/>
      </xsd:complexType>
      <!-- End Maatwerk -->
      <xsd:attribute name="code" type="xsd:string">
            <xsd:annotation>
                  <xsd:documentation>Alpha numeric identification of the entity</xsd:documentation>
            </xsd:annotation>
      </xsd:attribute>
      <xsd:attribute name="number" type="xsd:integer">
            <xsd:annotation>
                  <xsd:documentation>Numeric identification of the entity</xsd:documentation>
            </xsd:annotation>
      </xsd:attribute>
                ..............
</xsd:schema>

Normally I would expect the root element and then be able to generate my XML like:

<?xml version="1.0"?>
<rootNode  xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com mySchema.xsd">
              <FirstName>Max</FirstName>
              ......
</rootNode>

However, rootNode doesn't exist.

Can anyone tell me how I can validate my XML based on the schema when it DOESN'T have a rootnode? Cause I cannot figure it out. I've looked everywhere.

Thanks in advance.

Regards,
Max.
0
Comment
Question by:DreamMaster
  • 7
  • 6
14 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 18835038
Every globally defined element in an xsd can serve as the root element

So you could use any element that is defines in an xsd:element that is top level
(so not nested in another element definition)

There should be at least one in the schema, or your schema is not meant to be used standalone

cheers

Geert
0
 
LVL 6

Expert Comment

by:Niraj_Singh
ID: 18835047
hi,
 well as far as i have understood from your post that you have an xml file from where you will have to validate the user input.
where  do you have the xml file? in database or in a seperate file.
if its in a seperate file then you can create an object of XmlDocument and use its Load method to load the file. then you can move to what eve node you want.

   XmlDocument document = new XmlDocument();
   document.Load("C:\\Q317664.xml");  -- your file path here
   
Now you can use the document.GetElementsByTagName("ValidationNodeName") to go to the node directly.
please let me know if you had more issues.
0
 
LVL 19

Author Comment

by:DreamMaster
ID: 18835073
So if I have

<!-- General elements -->
<xsd:element name="Description" type="xsd:string"/>
<xsd:element name="Abbreviation" type="xsd:string"/>
<xsd:element name="Reference" type="xsd:string"/>
<xsd:element name="OurRef" type="xsd:string"/>
<xsd:element name="YourRef" type="xsd:string"/>
<xsd:element name="SalesOrderNo" type="xsd:string"/>
<xsd:element name="OrderNumber" type="xsd:string"/>
<xsd:element name="BackFlush" type="xsd:boolean"/>
<xsd:element name="ExternalCode" type="xsd:string"/>
<xsd:element name="Text" type="xsd:string"/>

Then any of these would do?

I tried that using the Element Description and I get the error "Can not have element children within a simple type content" which I figure is logical, since it is a simple type and is not meant for anything other than just a description.

I must admit my experience with this is next to nothing, but I figured it would be a bit easier than this ;)

Regards,
Max.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 18835084
Max,

note that a schema should have an indication (look for the annotations) to show how it should be used.
Have you tried creating a diagram of the schema, using Stylus Studio or Oxygen (or Visual Studio)
If you do that, it shows which elements can be used as a root.
A properly designed schema makes sure that you can only use the intended elements as root elements
by making all the other elements local, or hidden in a named type

If you post a link to the schema, I can giveyou some more indications

cheers

Geert
0
 
LVL 19

Author Comment

by:DreamMaster
ID: 18835087
Hi Niraj_Singh,

No, I think you misunderstand me. Let me try and explain it a bit more thoroughly...

For one of our clients we are building a complex application that connects with Exact. This application will accept registrations of people that want to buy something. In order to make sure the user is validated, the application will have to pass validated XML to the Exact database using the Schema provided to us. This validated XML will be created upon submit of the form where the user registers. So I will have to create this programmatically.

In other words, I will have to dynamically create an XML document that validated against the XSD file that was delivered to me.

Regards,
Max.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 18835094
All the general elements you posted are simple type elements
They can be used as a root element, but that doesn't make much sense
if for example Description is a root element, it can't have child elements, so the full XML would be something like this
<Description>Some desc text</Description>
... I assume that is not what you are looking for

You have to dig deeper and look for globally defined elements, that have complex content

cheers

Geert
0
 
LVL 19

Author Comment

by:DreamMaster
ID: 18835107
Hi Gertone,

http://test.digibiz.nl/allebatterijen/eExact-Schema.xsd

I hope you can help me. :)

Regards,
Max.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 60

Expert Comment

by:Geert Bormans
ID: 18835145
If it can be a comfort to you...
this schema s@#ks :-)
Too many global elements that should not serve as a rootnode.
This could have been designed a bit more elegantly
You might search for some documentation on the eExact website,
not sure it is there, the schema annotations don't really help

At line 6600 or something, I found this
      <!-- MAATWERK: eExact node is linked to a type definition instead of having the topic list explicitly under it. -->
      <xsd:element name="eExact" type="rootNode"/>
      <!-- END MAATWERK -->

I assume "eExact" would work as a rootnode
it has the type rootnode as its type,
so you ll have access to all the topics listed below that

cheers

Geert
0
 
LVL 19

Author Comment

by:DreamMaster
ID: 18835166
No kidding? ;)

I looked at this for many hours and then decided I needed someone else to look at this. My colleague knew even less about validated XML than I do, so that was no help. ;)

Well...let me check...

Using:

<?xml version="1.0"?>
<eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///c:/Inetpub/wwwroot/Allebatterijen/App_Data/eExact-Schema.xsd">
      <Description>Test</Description>
</eExact>

Returns:

Element 'Description' is not valid for content model: '((Settings,ExchangeRates,Accounts,Assortments,Items,ItemPrices,Resources,GLAccounts,Journals,VATs,FinEntries,GLEntries,Budgets,InternalUses,DocumentTypes,DocumentGroups,Documents,Projects,Assets,Invoices,Orders,ProductionOrders,Contracts,Requests,Messages,ResourceRoles,PayrollComponentTypes,PayrollResources,PayrollTransactions,Costcenters,Costunits,Currencies),Topics)'

If that is right..and I understand this correctly, I can't get to the nodes that would contain just the information I need.. ;)

Although I am not sure. Thanks for your patience Geert...

Regards,
Max.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 18835224
well, that is the difficulty with using an undocumented schema
(I have to be carefull, because maybe there is a documentation in a seperate document)

A schema describes the structure of an XML document

eg. eExact can have Settings, ExchangeRates,... as a child
Accounts can have multiple Account elements as a child etc.

      <xsd:element name="Language">
            <xsd:complexType>
                  <xsd:sequence>
                        <xsd:element ref="Description" minOccurs="0">
                              <xsd:annotation>
                                    <xsd:documentation>taal.oms30</xsd:documentation>
                              </xsd:annotation>
                        </xsd:element>
                  </xsd:sequence>
                  <xsd:attribute ref="code" use="required">
                        <xsd:annotation>
                              <xsd:documentation>*taal.taalcode</xsd:documentation>
                        </xsd:annotation>
                  </xsd:attribute>
            </xsd:complexType>
      </xsd:element>

means that Language has a Description child and a code attribute (that is required)

So you can find all of that out from the schema

Maybe working th eother way round would work for you
See what needs to be expressed... Person?
maybe Person is allowed as a root (if it is global, it is)
if not, find a possible parent, that is allowed as a root,
It requires some reverse engineering, but it will lead to the smallest possible XML
that is valid and contains your information

what do you need to express?

cheers

Geert
0
 
LVL 19

Author Comment

by:DreamMaster
ID: 18835268
Well I need to get a possible client to register, using his personal information, Title, FirstName, MiddleName, LastName etc...and possibly a delivery address (which can differ from the information placed in the personal information)

So I figured, after some digging, that I would get this kind of structure:

<?xml version="1.0"?>
<eExact xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///c:/Inetpub/wwwroot/Allebatterijen/App_Data/eExact-Schema.xsd">
      <Contracts>
            <Contract>
                  <Account code="123345">
                        <Contacts>
                              <Contact>
                                    <Title>Dhr.</Title>
                                    <FirstName>Max</FirstName>
                                    <MiddleName></MiddleName>
                                    <LastName>Davidse</LastName>
                              </Contact>
                        </Contacts>
                  </Account>
            </Contract>
      </Contracts>
</eExact>

However, it doesn't like that when I validate, eventhough Stylus Studio recognizes the elements in each part that I started to type.

This is going to give me headaches...I predict that now.. ;)

Regards,
Max.
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 18835768
Should not be too much of a headache...

you have some difficulties
- Title has some sort of standardised structure
- Lastname should be the first element in the sequence

This validates well
<Accounts>
    <Account code="123345">
        <Contacts>
            <Contact default="true">
                <LastName>Davidse</LastName>
                <FirstName>Max</FirstName>
                <MiddleName></MiddleName>
                <Title code="dhr">
                    <Description>De Heer</Description>
                </Title>
            </Contact>
        </Contacts>
    </Account>
</Accounts>                

cheers

Geert
0
 
LVL 19

Author Comment

by:DreamMaster
ID: 18835789
Geert, you are officially amazing. ;) It would have taken me much longer if I had to find this all out on my own. I'm pretty sure I'll get there now..and if not...I will just ask another question.. ;)

Thanks!
Regards,
Max.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 18835831
You are welcome Max.
I am not monitoring the .net zones actively
so if you have another question,
make sure the words schema and XML are in it,
or send me an email (see profile) to prompt me for it

cheers

Geert
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now