• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 513
  • Last Modified:

Wildcard in XSD

Hello,

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="IntegrationPointData" targetNamespace="http://www.manh.com/ILSNET/CustomizeWaveLabelsData" xmlns="http://www.manh.com/ILSNET/CustomizeWaveLabelsData"

xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="IntegrationPointData">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element name="Session">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="UserName" type="xs:string" minOccurs="0" />
              <xs:element name="Warehouse" type="xs:string" minOccurs="0" />
              <xs:element name="Environment" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

I want the XML to atleast have a Session with those 3 elements. It can have additional elements inside Session and additional elements at the same level as Session. I dont want those to be bound by any xsd.

Please advice the xsd changes to be done.

Thanks.
0
rajivraj
Asked:
rajivraj
  • 4
  • 2
1 Solution
 
Geert BormansCommented:
Hi,

variations on the following theme should help you

            <xs:choice maxOccurs="unbounded">
                <xs:element name="Session">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="UserName" type="xs:string" minOccurs="0" />
                            <xs:element name="Warehouse" type="xs:string" minOccurs="0" />
                            <xs:element name="Environment" type="xs:string" minOccurs="0" />
                            <xs:any minOccurs="0" namespace="##other" processContents="skip"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:any minOccurs="0" namespace="##other" processContents="skip"/>
            </xs:choice>

<xs:any> allows any element
since this could also be elements from the targetNamespace, you exclude the targetnamespace elements like this namespace="##other"
this is important otherwise you would have an ambiguous schema

to tell the parser that it should ignore the elements content... processContents="skip"

cheers
0
 
rajivrajAuthor Commented:
I dont think I am missing anything, but I get the following error:

"The element 'http://www.abc.com/NET/CustomizeData:Session' has invalid content. An error occurred at , (1, 231).The 'http://www.abc.com/NET/CustomizeData:Abc' element is not declared. An error occurred at , (1, 231).;"

This is when I use

            <xs:choice maxOccurs="unbounded">
                <xs:element name="Session">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="UserName" type="xs:string" minOccurs="0" />
                            <xs:element name="Warehouse" type="xs:string" minOccurs="0" />
                            <xs:element name="Environment" type="xs:string" minOccurs="0" />
                            <xs:any minOccurs="0" namespace="##other" processContents="skip"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:choice>

I am using the using .NET XmlValidatingReader.
0
 
Geert BormansCommented:
mmh, you want this "any" element to be in the same namespace...
that is a lot more difficult.
I will try to explain why

CustomizeData:Abc is currently not allowed, because the schema explicitely says: namespace="##other"
meaning the "any" element can be any element not in the CustomizeData namespace

First question:
do you want this "abc" to explicitely be in the same namespace,
or did it just happen to be there because it inherited the default namespace from its parent
If you don't care in which namespace the Abc element is, do something like this
<Abc xmlns="">...
If you put Abc in the null namespace like the example the XML will be correct according to your schema

why is it so difficult to have the any element in the same namespace
well, it would not be a problem if you had this
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="UserName" type="xs:string" minOccurs="1" maxOccurs="1"/>
                            <xs:element name="Warehouse" type="xs:string" minOccurs="1" maxOccurs="1"/>
                            <xs:element name="Environment" type="xs:string" minOccurs="1" maxOccurs="1"/>
                            <xs:any minOccurs="0" processContents="skip"/>
                        </xs:sequence>
                    </xs:complexType>
Because then the parser knows that every element starting at number 4 in the sequence and further more is an "any" element that needs no further processing
Now let's look at this example
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="UserName" type="xs:string" minOccurs="1" maxOccurs="1"/>
                            <xs:element name="Warehouse" type="xs:string" minOccurs="1" maxOccurs="1"/>
                            <xs:element name="Environment" type="xs:string" minOccurs="0" maxOccurs="1"/>
                            <xs:any minOccurs="0" processContents="skip"/>
                        </xs:sequence>
                    </xs:complexType>
if you had this XML
<Session><UserName/><Warehouse/><Environment/></Session>
with the above schema the parser cannot know that the "Environment" element is meant to be the "Environment" element from the schema,
or an instantiation of the "any" element... and it is important to know which element you have in mind there, to see wheither it needs processing or not
(even if processContents wasn't 'skip', the parser demands to know which element is which in the schema)
So without the namespace="##other" your parser will tell you your schema is not unambiguous

I would recommend you in your case to put all the "any" elements in the null namespace, by explicitely adding xmlns=""
and leaving the schema as it is now

if that is unacceptable, we will need a tougher schema, please come back then

Geert
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
rajivrajAuthor Commented:
"did it just happen to be there because it inherited the default namespace from its parent"

you are right in this.

I got it right now by adding "<Abc xmlns="">..."

Now, it works fine when i have this <abc xmlns=".."> element. But when I dont have any additional elements and only UserName, Warehouse & Environment, it says

"The element 'http://www.abc.com/NET/CustomizeData:Header' has incomplete content. An error occurred at , (1, 243)."

Whats missing now!!!
0
 
Geert BormansCommented:
are you sure you have a minOccurs="0" on the xs:any?
0
 
Geert BormansCommented:
it is also weird that the parser complains about the Header element, not about the Session element
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now