aspose file tools*
The moose likes Product and Other Certifications and the fly likes Unqualified elment - but still have prefix at the root Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Product and Other Certifications
Bookmark "Unqualified elment - but still have prefix at the root" Watch "Unqualified elment - but still have prefix at the root" New topic
Author

Unqualified elment - but still have prefix at the root

avihai marchiano
Ranch Hand

Joined: Jan 10, 2007
Posts: 342
Hey,

i have two schemas A,B.
B import A.
Both of them defined as unqualified element, but the xml that generated from B has n1: as a prefix for the root elements.
Is there a way to have no prefix at all?

Thank you
Tom Silverman
Greenhorn

Joined: Nov 01, 2008
Posts: 24
This depends on a) What generated the instance and b) Are elements (or attributes) with the same name defined both in A.xsd and the imported schema B.xsd. If you generated the sample instance document with, say, XMLSpy, then it typically assigns namespaces to generated elements - required or not. In the second case, there is no way to use elements with the same name, originating in different schemas (vocabularies) in a none prefixed way in the same instance document.


Tom Silverman: SCJP5, SCJD6, SCWCD5, SCBCD5, IBM-142, ScrumMaster
avihai marchiano
Ranch Hand

Joined: Jan 10, 2007
Posts: 342
Exampel : lib.xsd import book.xsd (both with unqualified)

------ LIB -------
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.library.org" xmlns:b="http://www.book.org" targetNamespace="http://www.library.org" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xs:import namespace="http://www.book.org" schemaLocation="Book.xsd"/>
<xs:element name="Library" type="Library"/>
<xs:complexType name="Library">
<xs:sequence>
<xs:element name="BookCatalogue">
<xs:complexType>
<xs:sequence>
<xs:element name="test" type="b:BookType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>



--------------------- Book --------------
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.book.org" targetNamespace="http://www.book.org" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xs:complexType name="BookType">
<xs:sequence>
<xs:element name="Title" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>



When i generate xml in xmlspy i get the following xml (with n1 prefix , but i dont want prefix!!!)

------------------------ XML -------------------
<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2008 sp1 (http://www.altova.com)-->
<n1:Library xsi:schemaLocation="http://www.library.org lib.xsd" xmlns:n1="http://www.library.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<BookCatalogue>
<test>
<Title>String</Title>
</test>
</BookCatalogue>
</n1:Library>



I dont want prefix!!!
When i try to have instance without the 'n1' prefix and with xmlns="http://www.library.org" instead -xmlns:n1="http://www.library.org" its not valid.

I didnt find any way to change the schema or the xml in order to have no prefix at all.



Thank you
Tom Silverman
Greenhorn

Joined: Nov 01, 2008
Posts: 24
Try it like this:

<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2008 (http://www.altova.com)-->
<Library xsi:noNamespaceSchemaLocation="lib.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<BookCatalogue>
<test>
<Title>Title</Title>
</test>
</BookCatalogue>
</Library>
Tom Silverman
Greenhorn

Joined: Nov 01, 2008
Posts: 24
One more thing:

Notice the change I made to the complexType 'Libray' name. It is now 'LibraryType' - just for readability.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:b="http://www.book.org">
<xs:import namespace="http://www.book.org" schemaLocation="Book.xsd" />
<xs:element name="Library" type="LibraryType"/>
<xs:complexType name="LibraryType">
<xs:sequence>
<xs:element name="BookCatalogue">
<xs:complexType>
<xs:sequence>
<xs:element name="test" type="b:BookType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
avihai marchiano
Ranch Hand

Joined: Jan 10, 2007
Posts: 342
I check the validation in XMLSpy and i get validation error due to :
File Untitled43.xml could not be validated because of an error in XML Schema/DTD (see below)
Schema at location 'c:\proffesional\jaxb\test-import\import\src\main\lib.xsd' has target namespace 'http://www.library.org' rather than ''.
Error location: Library / @xsi:noNamespaceSchemaLocation
Details
schema_reference: Schema at location 'c:\proffesional\jaxb\test-import\import\src\main\lib.xsd' has target namespace 'http://www.library.org' rather than ''.
Jim Janssens
Ranch Hand

Joined: Sep 24, 2004
Posts: 210
As far as I can see this is all normal.
When the setting in your schema 'elementFormDefault' is set to 'unqualified' your elements in the resulting XML document may :

a) NOT have a prefix (besides the root element)
b) NOT be in a default namespace (as that would make them qualified as well)

This example validates to 'unqualified':



This example validates as 'qualified':



and is equivalent to a different notation with a default namespace:


So if you do not want prefixes, use qualified and work with a default namespace.
Of course, if you use other 'components' from schema's in a different namespace, you will have to declare a namespace with prefix and these parts will be fully prefixed when used in your xml
(you could get away with overriding the default namespace though, but that is just a big NO NO)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unqualified elment - but still have prefix at the root