java – 这个XML / XSD有什么问题?

我有一个简单的XSD和更简单的
XML.但是
Java 2 XML验证失败了. (使用javax.xml.validation)

这是我的XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
     xmlns:tns="http://foo.com/darnit"
     targetNamespace="http://foo.com/darnit">

   <xsd:element name="Person" type="tns:PersonType"/>

   <xsd:simpleType name="nameType">
     <xsd:restriction base="xsd:string"/>
   </xsd:simpleType>

   <xsd:complexType name="PersonType">
     <xsd:sequence>
       <xsd:element minOccurs="1" maxOccurs="2" name="FirstName" type="tns:nameType"/>
       <xsd:element minOccurs="1" maxOccurs="1" name="LastName" type="tns:nameType"/>
     </xsd:sequence>
   </xsd:complexType>
</xsd:schema>

以下是XML示例:

<?xml version="1.0" encoding="UTF-8"?>
<Person xmlns="http://foo.com/darnit">
  <FirstName>John</FirstName>
  <FirstName>Michael</FirstName>
  <LastName>Smith</LastName>
</Person>

这是我收到的错误消息:

org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'FirstName'. One of '{FirstName}' is expected.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.reportSchemaError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.jaxp.validation.StreamValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.XMLSchemaValidator.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Unknown Source)

如果我使用名称空间前缀限定XML,它就可以了!

<?xml version="1.0" encoding="UTF-8"?>
<foo:Person xmlns:foo="http://foo.com/darnit">
  <FirstName>John</FirstName>
  <FirstName>Michael</FirstName>
  <LastName>Smith</LastName>
</foo:Person>

但我的XSD允许不合格的元素!

我是否必须在SchemaFactory,Schema或Validator上设置属性?

谢谢.

最佳答案 添加elementFormDefault =符合你的schame,如下:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:tns="http://foo.com/darnit"
 targetNamespace="http://foo.com/darnit"
 elementFormDefault="qualified">

然后,默认情况下,所有元素都将位于目标命名空间中.

所有全局声明的元素都属于目标命名空间.然而,“elementFormDefault”属性控制是否本地元素也属于目标命名空间,即“是合格的”.有些人显然更喜欢你无意中创造的“不合格”风格.但是,我从来没有看到过支持它的好论据.

点赞