2

today i've bumped in the following problem. I have the following xml:

<c:docschema xmlns:c="http://www.otr.ru/sufd/document/desc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
   ...
</c:docschema>

And it's validating fina against it's schema. But I don't want namespace prefixes in my xml, so i try to write it like this:

<docschema xmlns="http://www.otr.ru/sufd/document/desc"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
       ...
</docschema>

And it's giving me a validation error. My XSD schema i'm validating against is compound of two XSD's, here is the headers:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        attributeFormDefault="unqualified"
        elementFormDefault="unqualified"
        xmlns="http://www.otr.ru/sufd/document/desc"
        targetNamespace="http://www.otr.ru/sufd/document/desc"
        xmlns:fieldset="http://www.otr.ru/sufd/document/fieldset"
        xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore">


<xsd:import namespace="http://www.otr.ru/sufd/document/fieldset" schemaLocation="fieldset.xsd"/>

and

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        attributeFormDefault="unqualified"
        elementFormDefault="unqualified"
        targetNamespace="http://www.otr.ru/sufd/document/fieldset"
        xmlns="http://www.otr.ru/sufd/document/fieldset">

What's wrong there?

EDIT: The question is now, how to change my XSD's in order to make instance document valid?

2 Answers 2

3

Given what you write, I imagine that the problem is the following.

Let's consider that there is an a element under your root element.

This first example below is valid because a is unqualified and because you set elementFormDefault to unqualified :

First example

<c:docschema xmlns:c="http://www.otr.ru/sufd/document/desc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
   <a>...</a>
</c:docschema>

In the second example the file is not valid because you set elementFormDefault to unqualified and you have an element a that is qualified (in the default namespace) :

Second example

<docschema xmlns="http://www.otr.ru/sufd/document/desc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
   <a>...</a>
</docschema>

The correct XML could be :

<docschema xmlns="http://www.otr.ru/sufd/document/desc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.otr.ru/sufd/document/desc http://otr-sufd/xmlschema/docschema.xsd">
   <a xmlns="">...</a>
</docschema>

EDIT

If the children of the root element are defined in the same namespace than the root in your schemas, you just have to change elementFormDefault="unqualified" to elementFormDefault="qualified" to have a schema that validates the XML. If it's not the case : you will surely have to reshape your schema more deeply, in this case, maybe you should post another question dedicated to that with more code (including more part of the schemas and instances).

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, i've understood the problem. The question now is how to change my XSD's in order to make my instance document valid without the additional namespace declarations?
0

Looks like you are making the mistake of assuming that the nested elements inside your root <docschema> will inherit the namespace defined on that root. They will not.

If you want to get rid of namespace prefixes you will then have to explicitly declare the namespace at every sub-node in your instance document.

Eg

<Root xmlns="http://www.myns.com">
  <MyElement1 xmlns="http://www.myns.com">
  ... etc
  </MyElement1>
</Root>

or

<p:Root xmlns:p="http://www.myns.com">
  <p:MyElement1>
  ... etc
  </p:MyElement1>
</p:Root>

Which is nicer? I think the second option.

2 Comments

thatk you for the answer. I've took a look on this article: xfront.com/HideVersusExpose.html, here is an example with hidden namespaces. That (with the only prefix and namespace declaration at the root element) works fine, all i want is to use default prefix (e.g. without prefix) as my elements are not colliding withing the namespace.
Sorry my mistake - I didn't see your elementFormDefault setting there. It's not usual to work with XSD like that. However, the sample from the link you posted still uses a prefix in the root node. I don't think you can do what you want to do, but I can't explain exactly why

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.