XSD 中的子元素和命名空间

发布于 2024-07-25 08:38:29 字数 2379 浏览 13 评论 0原文

我一直在尝试弄清楚如何在将 XML 文件加载到应用程序中时使用 XML 架构来验证它们。 我已经使该部分正常工作,但我似乎无法让架构将根元素以外的任何内容识别为有效。 例如,我有以下 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>

以下内容(诚然是从可视化编辑器生成的 - 我只是凡人) 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>

但是现在,使用 Eclipse 的内置(基于 Xerces?)验证器,我得到以下内容错误:

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

那么我该如何修复我的 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="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>

现在它可以工作了,但是该方法是否意味着我可以在文档的根目录中使用 ? 如果 ref 应该按原样替换,那么为什么我不能将 ref="actvity" 替换为 name="activity" type=" xsd:字符串"?

附加附录:一定要这样做,否则你会花上好几个小时把头撞在墙上:

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

I've been trying to figure out how to use an XML Schema to validate XML files as I load them into an application. 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:

<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:

<?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:

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? 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? And if the ref should just be substituted as-is, then why can't I replace ref="actvity" with 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏别ゝ 2024-08-01 08:38:29

此 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>

This XSD validates properly here:

<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文