简体   繁体   English

XSD中的子元素和名称空间

[英]Sub-elements and namespaces in XSD

I've been trying to figure out how to use an XML Schema to validate XML files as I load them into an application. 我一直在尝试找出如何在将XML文件加载到应用程序中时使用XML Schema来验证XML文件。 I've got that part working, but I can't seem to get the schema to recognise anything other than the root element as valid. 我已经完成了该部分的工作,但是我似乎无法使模式识别除根元素以外的任何内容都有效。 For instance, I have the following XML file: 例如,我有以下XML文件:

<fun xmlns="http://ttdi.us/I/am/having/fun"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://ttdi.us/I/am/having/fun
                          test.xsd">
    <activity>rowing</activity>
    <activity>eating</activity>
    <activity>coding</activity>
</fun>

with the following (admittedly generated from the visual editor—I am but a mere mortal) XSD: 带有以下(肯定是从可视编辑器生成的,我不过是凡人)XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://ttdi.us/I/am/having/fun" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ttdi.us/I/am/having/fun">
    <xsd:element name="fun" type="activityList"></xsd:element>

    <xsd:complexType name="activityList">
        <xsd:sequence>
            <xsd:element name="activity" type="xsd:string" maxOccurs="unbounded" minOccurs="0"></xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

But now, using Eclipse's built-in (Xerces-based?) validator, I get the following error: 但是现在,使用Eclipse的内置(基于Xerces?)验证器,我得到以下错误:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'activity'. One of '{activity}' is expected.

So how do I fix my XSD so that it…works? 那么,如何修复XSD使其能够正常工作呢? All the search results I've seen so far seem to say "…so I just turned off validation" or "…so I just got rid of namespaces" and that's not something I want to do. 到目前为止,我所看到的所有搜索结果似乎都在说“ ...所以我只是关闭验证”或“ ...所以我只是摆脱了名称空间”,而这并不是我想要做的。

ADDENDUM: 附录:

Now let's say I change my schema to this: 现在假设我将架构更改为此:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://ttdi.us/I/am/having/fun"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ttdi.us/I/am/having/fun">
    <xsd:element name="activity" type="xsd:string"></xsd:element>

    <xsd:element name="fun">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="activity" minOccurs="0" maxOccurs="unbounded"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Now it works, but does that method mean that I'm allowed to have <actvity> at the root of my document? 现在可以使用了,但是该方法是否意味着我可以在文档的根目录使用<actvity>了? And if the ref should just be substituted as-is, then why can't I replace ref="actvity" with name="activity" type="xsd:string" ? 而且,如果ref应该按原样替换,那么为什么我不能将ref="actvity"替换为name="activity" type="xsd:string"呢?

ADDITIONAL ADDENDUM: ALWAYS do this, or else you will spend hours and hours banging your head on a wall: 其他附录:总是这样做,否则您将花费​​数小时将头撞在墙上:

DocumentBuilderFactory dbf;
// initialize dbf
dbf.setNamespaceAware(true);

This XSD validates properly here : 此XSD 在这里正确验证:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://ttdi.us/I/am/having/fun" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ttdi.us/I/am/having/fun">

  <!-- definition of simple element(s) -->
  <xsd:element name="activity" type="xsd:string"></xsd:element>

  <!-- definition of complex element(s) -->
  <xsd:element name="fun">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="activity" maxOccurs="unbounded" minOccurs="0"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

</xsd:schema>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM