鉴于以下代表性片段:XML没有验证对XSD
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:j="http://foo" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://foo" elementFormDefault="qualified" attributeFormDefault="unqualified" version="3.2">
<xs:element name="Event">
<xs:annotation>
<xs:documentation>The Incident beginning and end date and time</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:complexContent>
<xs:extension base="j:EventType"/>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:complexType name="EventType">
<xs:sequence>
<xs:element ref="j:EventDate" minOccurs="0"/>
<xs:element ref="j:EventTime" minOccurs="0"/>
</xs:sequence>
<xs:attribute ref="j:EventType" use="required"/>
</xs:complexType>
<xs:attribute name="EventType">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Beginning"/>
<xs:enumeration value="Ending"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
下面的XML不验证。具体来说,它不会验证EventType属性。
<?xml version="1.0" encoding="utf-8"?>
<SomeDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://hostedbykarpel.com/Schemas/ReferralDocument_2">
<Event EventType="TypeOne">
<EventDate>2013-12-18</EventDate>
<EventTime>00:15:28</EventTime>
</Event>
</SomDoc>
但是,如果我明确添加元素中的命名空间它的工作原理:
<Event a:EventType="TypeOne" xmlns:a="http://foo">
<EventDate>2013-12-18</EventDate>
<EventTime>00:15:28</EventTime>
</Event>
命名空间在文档的根已声明。为什么我需要再次指定它才能让属性显示出来? Event元素本身验证就好,只是EventType属性不会。
请发布一个“代表性片段”,它可以自己站立以便于复制您的问题。您的XSD引用不存在的元素“j:EventDate”和“j:EventTime”。 – kjhughes
@kjhuges这会为样本增加50行。这些可以很容易地用字符串类型替换。问题与此无关。它也证明这不是一个问题,更多的是与XML规范相冲突。见Michael Gunter的回答。 – kettch
我没有声称这是你的问题 - 只是想帮助你提高花时间帮助你的人的几率。您应该发布展示您的问题的最小完整示例。期待我们将事情剔除出来,修正错别字('')等等,这使我们不太可能仔细观察。谢谢。 – kjhughes