xml 架构验证错误“前缀未绑定”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9473380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 15:14:04  来源:igfitidea点击:

xml schema validation error "prefix is not bound"

xmlxsdxsd-validation

提问by iggy2012

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 Schema 完全陌生,正在尝试了解基础知识。这是我的 xml 架构代码(文件名: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 文档。

<?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]

在尝试验证 xml 文件时,我从 netbeans 收到一条错误消息:未绑定元素“sample:school”的前缀“sample”。[9]

回答by Daniel Haley

In your XML, you either need to:

在您的 XML 中,您需要:

A. Remove the sample:prefix from sample:school

一个。去掉sample:前缀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.)

。改变xmlns="http://www.example.com"xmlns:sample="http://www.example.com"与添加sample:前缀的元素的其余部分(<sample:element1><sample:element2>等)

回答by Petru Gardea

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.

此外,这与XML namespaces相当相关。

回答by Artemis

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.

从 Weblogic 10.3.6 升级到 12.2.1.2 后,出现了我自己的错误“前缀 ns2 未绑定”。

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

在更高版本中,默认 JAXB 实现是 Moxy,所以我不得不将 javax.xml.bind.JAXBContext 更改为 com.sun.xml.bind.v2.ContextFactory。

How to specify JAXBContext implementation in weblogic 12.1.3

如何在 weblogic 12.1.3 中指定 JAXBContext 实现