Xml XSD-I';当使用抽象机制和来自其他名称空间的替换组时,我遇到了一个奇怪的行为
我有以下spml-example.xsd,它定义了一个包含action元素的command元素:Xml XSD-I';当使用抽象机制和来自其他名称空间的替换组时,我遇到了一个奇怪的行为,xml,xsd,Xml,Xsd,我有以下spml-example.xsd,它定义了一个包含action元素的command元素: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.myexample.com/command" xmlns:tns="http://www.myexample.com/command
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.myexample.com/command" xmlns:tns="http://www.myexample.com/command"
elementFormDefault="qualified">
<xs:element name="Action" type="tns:ActionType" abstract="true" />
<xs:complexType name="ActionType" abstract="true">
<xs:attribute name="result" type="xs:string" />
</xs:complexType>
<xs:element name="Command">
<xs:complexType>
<xs:sequence>
<xs:element ref="tns:Action" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
正如您所看到的,操作是抽象的,因此具体的实现可以替代它。
所以我有两种类型的动作来扩展这个动作:
第一个是spml-test-actions.xsd,它定义了读取操作:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.myexample.com/actions/data"
xmlns:tns="http://www.myexample.com/actions/data" xmlns:command="http://www.myexample.com/command"
elementFormDefault="qualified">
<xs:import namespace="http://www.myexample.com/command"
schemaLocation="../../spml-example.xsd" />
<xs:element name="read" type="tns:readType"
substitutionGroup="command:Action" />
<xs:complexType name="readType">
<xs:complexContent>
<xs:extension base="command:ActionType">
<xs:attribute name="a" type="xs:string"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
第二个是spml-local-actions.xsd,它定义了一个弹出操作:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.myexample.com/actions/local"
xmlns:tns="http://www.myexample.com/actions/local" xmlns:command="http://www.myexample.com/command"
elementFormDefault="qualified">
<xs:import namespace="http://www.myexample.com/command"
schemaLocation="../../spml-example.xsd" />
<xs:element name="popup" type="tns:PopupType"
substitutionGroup="command:Action" />
<xs:complexType name="PopupType">
<xs:complexContent>
<xs:extension base="command:ActionType" >
<xs:attribute name="width" type="xs:float"/>
<xs:attribute name="height" type="xs:float"/>
<xs:attribute name="view" type="xs:string"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
请注意,这些操作位于不同的名称空间中
现在,我创建了以下xml实例,效果非常好:
<?xml version="1.0" encoding="UTF-8"?>
<Command xmlns="http://www.myexample.com/command" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:crud="http://www.myexample.com/actions/data"
xmlns:local="http://www.myexample.com/actions/local"
xsi:schemaLocation="http://www.myexample.com/command spml-example.xsd
http://www.myexample.com/actions/data actions/data/spml-test-actions.xsd
http://www.myexample.com/actions/local actions/local/spml-local-actions.xsd">
<local:popup/>
</Command>
但这里有一个怪癖,现在如果我添加crud:read操作,它将失败,并显示消息“cvc复杂类型。2.4.a:发现以元素“crud:read”开头的无效内容。{”http://
www.myexample.com/command“:Action}”应为“
这并不意味着crud:read不正常,因为如果现在我删除local:popup操作,xml将再次有效,即它只允许添加一个命名空间中的操作
我确实有一个设计上不正确的解决方案,我可以添加到spml-test-actions.xsd和spml-local-actions.xsd的spml-example.xsd导入中
请帮助,谢谢。它适合我(使用Saxon EE 9.4作为模式处理器)。这可能是架构处理器中的错误,或者与配置方式有关。我认为您的问题缺少的是如何验证XML;与Michael不同,我认为您的问题与模式处理器无关,而是与如何验证XML有关 如果您的XML编辑器或您正在使用的任何API依赖于xsi:schemaLocation提示,那么很明显,它只加载第一个XML模式文件。您可以通过删除
http://www.myexample.com/command 来自schemaLocation提示的spml example.xsd
;我猜接下来会发生什么,第一个列出的XSD和导入的XSD中的替换组成员将正常工作;之后的一切都不会发生
如果您确实认为这是罪魁祸首,那么您可以做些什么来解决它——如果您希望坚持schemaLocation提示的话——就是创建一个XSD来导入您的crud和本地XSD(忘记spml example.XSD
),并使用该XSD作为您的提示
否则,为了得到合理的建议,您必须更多地描述如何进行验证(我从错误消息中猜测,很可能是基于Java的Xerces)。我发现了上面提到的问题。只需要进入eclipse中的窗口->首选项 然后在xml验证首选项中取消标记复选框“尊重所有xml模式位置”。
请看bug-是的,你说得对,我发现了问题。我需要在首选项中配置xml验证。