XML模式导致一个没有名称空间的xml元素 [英] XML schema is resulting in an xml element without a namespace

查看:126
本文介绍了XML模式导致一个没有名称空间的xml元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用非常简单的架构时遇到了麻烦.在xml主体中,该架构指定了root元素的第一个子元素,该子元素被指定为具有xmlns =",这对我来说导致验证问题.

I'm having some trouble with a very simple schema. In the xml body that the schema specifies the first child of the root element is specified as having xmlns="" which is causing validation problems for me.

我经过反复搜索并试图找出原因,但没有成功.这正在导致我的服务器代码出现更大的问题,因为我正在编写xml主体,并且由于我的元素没有xmlns ="而使它的验证失败.

I've searched and searched and tried repeatedly to figure out why it's happening and I've not had any success. This is causing a larger problem with my server code as I'm composing an xml body and validation of it is failing due to my element not having xmlns="".

我想了解为什么xmlns ="是由模式定义产生的,以及如何解决此问题.

I would like to understand why the xmlns="" is resulting from the schema definition and how to fix this.

以下是架构:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org" 
    xmlns="http://www.example.org">

    <xs:simpleType name="XYZ">
        <xs:restriction base="xs:string">
            <xs:maxLength value="10"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="ABC">
        <xs:sequence>
            <xs:element name="PQR" type="XYZ"/>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="A1">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="result" type="ABC"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

基于此架构,创建以下文档:

Based off of this schema, the following document is created:

<?xml version="1.0" encoding="UTF-8"?>
<A1 xmlns="http://www.example.org">
    <result xmlns="">
        <PQR></PQR>
    </result>
</A1>

我想要这样:

<?xml version="1.0" encoding="UTF-8"?>
<A1 xmlns="http://www.example.org">
    <result>
        <PQR></PQR>
    </result>
</A1>

我一直在研究这个问题,并且已经尝试了大约一天. <result>元素是否不应该成为targetnamespace的一部分,从而使xmlns ="不必要?

I've been looking at this and trying things for about a day now. Shouldn't the <result> element just be part of the targetnamespace making xmlns="" unnecessary?

另外,当我尝试验证它时,出现以下错误:

Additionally when I try to validate it I'm getting the following error:

System ID: /Users/dev/Desktop/Untitled3.xml
Main validation file: /Users/dev/Desktop/Untitled3.xml
Schema: /Users/dev/Desktop/test.xsd
Engine name: Xerces
Severity: error
Description: cvc-complex-type.2.4.a: Invalid content was found starting with element '{"http://www.example.org":PQR}'. One of '{PQR}' is expected.
Start location: 4:10
End location: 4:13
URL: http://www.w3.org/TR/xmlschema-1/#cvc-complex-type

推荐答案

添加

elementFormDefault="qualified"

xs:schema元素

应该可以解决问题.如果我进行了小的修改,然后让Oxygen生成示例XML文档,则结果是

to your xs:schema element should solve the problem. If I make this small modification and then let Oxygen generate a sample XML document, the result is

<?xml version="1.0" encoding="UTF-8"?>
<A1 xmlns="http://www.example.org">
    <result>
        <PQR>PQR0</PQR>
    </result>
</A1>

例如参见此问题和答案解释了这样做有何帮助,但要点是:elementFormDefault="qualified"告诉验证处理器,您在XSD中提到的元素应位于目标名称空间中. elementFormDefault的默认值为不合格".

See e.g. this question and answers for an explanation why this helps, but the gist of it is: elementFormDefault="qualified" tells a validating processor that the elements you mention in your XSD are meant to be in the target namespace. The default for elementFormDefault is "unqualified".

如果将其设置为"unqualified",则默认情况下将假定所有元素都位于无名称空间中,但XML文档的最外面的元素除外,该元素将假定具有您在此处指定的名称空间:

If it is set to "unqualified", all elements will be assumed to be in no namespace by default, except for the outermost element of your XML document, which will be assumed to have the namespace you have specified here:

xmlns="http://www.example.org"

这篇关于XML模式导致一个没有名称空间的xml元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆