XSD验证:命名空间导致找不到根元素 [英] XSD Validation : Namespace causing root element to not be found
问题描述
我正在尝试更新现有模式以使用其自己的名称空间,因此我可以稍后将其导入另一个模式,并明确使用的类型来自导入的模式.
I'm trying to a update an existing schema to use its own namespace, so I can import it into another schema later and make it clear the types being used are from the imported schema.
我尝试更改默认名称空间和目标名称空间,但是这导致架构验证中断并隐藏了根节点.从我看到的内容中,我已经将根元素隐藏在另一个名称空间中,但是我不确定如何配置它以获得期望的结果.
I tried changing the default and targeted namespaces but it's caused the schema validation to break and hide the root node. From what I can see i've hidden my root element in another namespace but I am unsure how to configure this to get my desired result.
这是我尝试过的基本示例
Here is a basic example of what I've tried
XML
<Parent Id="P">
<Child Id="C"/>
</Parent>
XSD
<xsd:schema targetNamespace="http://myNameSpace.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://myNameSpace.com"
elementFormDefault="qualified" >
<xsd:element name="Child">
<xsd:complexType>
<xsd:attribute name="Id" />
</xsd:complexType>
</xsd:element>
<xsd:element name="Parent">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Child" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="Id" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
验证
无效.
错误-第1行,第19行:org.xml.sax.SAXParseException; lineNumber:1; columnNumber:19; cvc-elt.1:找不到元素的声明 父母".
Error - Line 1, 19: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 19; cvc-elt.1: Cannot find the declaration of element 'Parent'.
推荐答案
您需要对 XML 进行一些更改:
You need to make a few changes to your XML:
- 实际上将根元素放置在target给定的名称空间中
通过将
xmlns="http://myNameSpace.com"
添加到P
来XSD的名称空间. - (可选)使用
xsi:schemaLocation
向XSD提供提示 使用.
- Actually place the root element in the namespace given by target
namespace of the XSD by adding
xmlns="http://myNameSpace.com"
toP
. - Optionally use
xsi:schemaLocation
to provide a hint to the XSD to use.
并添加到您的 XSD :
- 定义名称空间前缀,并使用其引用
Child
Parent
声明中的声明.
- Define a namespace prefix and use it to reference the
Child
declaration from theParent
declaration.
然后是这个XML,
<?xml version="1.0" encoding="UTF-8"?>
<Parent Id="P"
xmlns="http://myNameSpace.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://myNameSpace.com try.xsd">
<Child Id="C"/>
</Parent>
随后将针对此XSD有效,
<xsd:schema targetNamespace="http://myNameSpace.com"
xmlns:m="http://myNameSpace.com"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" >
<xsd:element name="Child">
<xsd:complexType>
<xsd:attribute name="Id" />
</xsd:complexType>
</xsd:element>
<xsd:element name="Parent">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="m:Child" minOccurs="0"/>
</xsd:sequence>
<xsd:attribute name="Id" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
根据要求.
这篇关于XSD验证:命名空间导致找不到根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!