xml schema validation error "prefix is not bound"
Asked Answered
T

3

31

I am entirely new to XML Schema and am trying to get the basics down. Here is my xml schema code (filename: example1.xsd):

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sample="http://www.example" 
targetNamespace="http://www.example.com" 
elementFormDefault="qualified">

<xs:element name="school">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="element1" type="xs:string"/>
      <xs:element name="element2" type="xs:string"/>
      <xs:element name="element3" type="xs:string"/>
      <xs:element name="element4" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

</xs:schema> 

Here is the XML document.

<?xml version="1.0" encoding="UTF-8"?>

<sample:school xmlns="http://www.example.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="./example1.xsd">

      <element1>hello</element1>
      <element2>hello</element2>
      <element3>hello</element3>
      <element4>hello</element4>

</sample:school>

Upon trying to validate the xml file, I get an error from netbeans that says: The prefix "sample" for element "sample:school" is not bound. [9]

Township answered 27/2, 2012 at 22:29 Comment(0)
M
35

In your XML, you either need to:

A. Remove the sample: prefix from sample:school

or

B. Change the xmlns="http://www.example.com" to xmlns:sample="http://www.example.com" and add the sample: prefix to the rest of the elements (<sample:element1>, <sample:element2>, etc.)

Margaretamargarete answered 27/2, 2012 at 22:41 Comment(0)
G
17

When you get an error like this, the simplest way is to add the prefix declaration; in your case, as per schema, just add to your root element the following attribute:

xmlns:sample="http://www.example.com"

Also, this is rather related with XML namespaces.

Glim answered 28/2, 2012 at 12:14 Comment(0)
R
1

It's not direct answer to your question, but I stumbled here while trying to solve my own problem, so maybe it will help someone too.

My own error 'The prefix ns2 is not bound' surfaced after upgrading from Weblogic 10.3.6 to 12.2.1.2.

In higher version default JAXB Implementation is Moxy, so I had to change javax.xml.bind.JAXBContext to com.sun.xml.bind.v2.ContextFactory.

How to specify JAXBContext implementation in weblogic 12.1.3

Redpencil answered 29/3, 2018 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.