为 XSD 中的元素指定命名空间 [英] Specify namespace for an element in XSD

查看:23
本文介绍了为 XSD 中的元素指定命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML,我需要为其生成 XSD.我的 XML 如下:

I've got an XML for which I need to generate an XSD. My XML goes as follows:

实例:

    <mes:GetInboundResponseGetInboundSMS 
          xmlns:mes="http://abcd.com">
         <response>
            <messages>
               <item>
                  <date>15/04/2014 00:00:00</date>
               </item>
               <item>
                  <date>01/07/2014 10:01:32</date>
               </item>
            </messages>
         </response>
    </mes:GetInboundResponseGetInboundSMS>

请注意,只有最外面的元素 GetInboundResponseGetInboundSMS 属于命名空间 http://abcd.com - 其余元素不属于.我如何在 XSD 中指定它?

Please note that only the outermost element GetInboundResponseGetInboundSMS belongs to a namespace http://abcd.com - the rest of the elements don't. How do I specify this in the XSD?

我已经尝试过以下 XSD,但这给了我错误:

I've tried with following XSD but that gives me error:

XSD:

<xs:schema attributeFormDefault="unqualified" 
           elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="GetInboundResponseGetInboundSMS">
    <xs:complexType>
      <xs:sequence>

<xs:element name="response">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="messages">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="item" 
                          maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="date"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

但是当我尝试使用在线验证器使用 XSD 验证实例时,我最终遇到了这个错误:

But when I tried to validate the instance with the XSD using an online validator, I ended up with this error:

无效.
错误 - 第 1、95 行:org.xml.sax.SAXParseException;行号:1;列数:95;cvc-elt.1:找不到元素mes:GetInboundResponseGetInboundSMS"的声明.

Not valid.
Error - Line 1, 95: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 95; cvc-elt.1: Cannot find the declaration of element 'mes:GetInboundResponseGetInboundSMS'.

这是合乎逻辑的,因为我没有在 XSD 中提供命名空间.请帮助我理解如何仅为 XSD 中的最外层元素提供此命名空间.

Which is logical, as I did not provide the namespace in the XSD. Please help me understanding how do I provide this namespace ONLY FOR THE OUTERMOST ELEMENT in my XSD.

推荐答案

你有两件事不是你想要的.

You've got two things that aren't quite what you want.

  • 您希望 GetInboundResponseGetInboundSMS 元素位于名称空间 http://abcd.com 中.

因此将 targetNamespace="http://abcd.com" 添加到您的架构元素中.

So add targetNamespace="http://abcd.com" to your schema element.

您希望该元素的子元素(它们都声明为 GetInboundResponseGetInboundSMS 元素的匿名复杂类型的本地元素)是不合格的.

You want the children of that element (which are all declared as local to the anonymous complex type of the GetInboundResponseGetInboundSMS element) to be unqualified.

因此将架构元素上的 elementFormDefault="qualified" 更改为 elementFormDefault="unqualified".

So change elementFormDefault="qualified" on the schema element to elementFormDefault="unqualified".

您的架构文档的开始标签应如下所示:

The start-tag for your schema document should look something like this:

<xs:schema targetNamespace="http://abcd.com"
           attributeFormDefault="unqualified" 
           elementFormDefault="unqualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

这篇关于为 XSD 中的元素指定命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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