Jump to content

schema info + attribute in root element


Bassman

Recommended Posts

Hi,I have an XML-document which I need to verify with an XSDI have added all the namespace and xsd info to the root element of the xml-fileBut the root element also has an attribute file_id (specified as required in the xsd)But when I try to validate it, it says that the attribute isn't allowed thereWhat's wrongThis is what the xml-file looks like:<?xml version="1.0" encoding="UTF-8"?><Premiums xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="C:\Development\BatchServer\Output\OUTPUT Premiums.xsd" file_id="1" > <Header> <Version>1</Version> <Source>09</Source>......

Link to comment
Share on other sites

<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http:/abvvmetaal.be/Premiums" xmlns:pr="http:/abvvmetaal.be/Premiums"><!--definition of attributes--> <xs:attribute name="file_id" type="xs:string"/> /////ALL OTHER DEFINITIONS <xs:element name="Premiums" > <xs:complexType> <xs:sequence> <xs:element ref="pr:Header" minOccurs="1" maxOccurs="1"/> <xs:element ref="pr:Premium" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute ref="pr:file_id" use="required"/> </xs:complexType> </xs:element> </xs:schema>

Link to comment
Share on other sites

You have your namespaces mixed up bad.First stop, the fact that your schema has a targetNamespace means, for most validators at least, that any XML file using it must also refer to it with it's namespace like so:

<Premiums xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http:/abvvmetaal.be/Premiums" xsi:schemaLocation="http:/abvvmetaal.be/Premiums file:///Development/BatchServer/Output/OUTPUT%20Premiums.xsd">

Notice that the value of the xsi:schemaLocation attribute is a "namespaceURI schemaLocation" pair and that I've added a namespace declaration to explicitly mark the Premiums element as part of that namespace.Next, the way you've declared your attribute, it must practically be always qualified. You forbid yourself from just writing file_id, but you do allow namespacePrefix:file_id. So in order to use that attribute you must either make your XML to something like this:

<pr:Premiums xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pr="http:/abvvmetaal.be/Premiums" xsi:schemaLocation="http:/abvvmetaal.be/Premiums file:///Development/BatchServer/Output/OUTPUT%20Premiums.xsd" pr:file_id="1">

Or if you don't wish to add a prefix to all elements too, just

<Premiums xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pr="http:/abvvmetaal.be/Premiums" xmlns="http:/abvvmetaal.be/Premiums" xsi:schemaLocation="http:/abvvmetaal.be/Premiums file:///Development/BatchServer/Output/OUTPUT%20Premiums.xsd" pr:file_id="1">

Or if you wish not to add a prefix anywhere and still allow this attribute in it's unqualified form, in the schema, change

<xs:attribute ref="pr:file_id" use="required"/>

to

<xs:attribute ref="file_id" use="required"/>

Link to comment
Share on other sites

Ok on the first partBut when I use pr:file_id="1" in my XML, it says: The prefix "pr" for attribute "pr:file_id" associated with an element type "Premiums" is not bound.And when I remove the pr in <xs:attribute ref="file_id" use="required"> it syas: Error resolving component 'file_id'. It was detected that 'file_id' has no namespace, but components with no target namespace are not referenceable from schema document 'file:/C:/Development/BatchServer/Output/OUTPUT/Premiums.xsd'.And also, I received the XSD from the customer, so i'd rather not alter it, unless not possible otherwiseTIA

Link to comment
Share on other sites

Furthermore, I removed all the "pr" references in the XSD and the XMLI get 2 errors:- Attribute 'file_id' is not allowed to appear in element 'Premiums'.- Attribute 'file_id' must appear on element 'Premiums'.I like the irony in that :-)

Link to comment
Share on other sites

If you don't wish to modify the schema, you're stuck with one of the first two forms for your XML (no editions in the schema must be made in this case).Try to just copy&paste the second form EXACTLY as shown in my previous post, or now:

<Premiums xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pr="http:/abvvmetaal.be/Premiums" xmlns="http:/abvvmetaal.be/Premiums" xsi:schemaLocation="http:/abvvmetaal.be/Premiums file:///Development/BatchServer/Output/OUTPUT%20Premiums.xsd" pr:file_id="1">

No more editions on the XML file would be needed in this case. And not on the schema either.And what validator are you using btw? I used Saxonica's validator.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...