繁体   English   中英

这个XML / XSD有什么问题?

[英]What's wrong with this 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 = qualified添加到您的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”属性控制本地元素是否也属于目标名称空间,即“合格”。 显然,有些人喜欢您无意中创建的“不合格”样式。 但是,我从未见过有任何理由支持它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM