【问题标题】:XML Schema - Roots elements cannot find declaration of elementXML Schema - 根元素找不到元素的声明
【发布时间】:2021-09-07 04:29:49
【问题描述】:

概述

我从 Enterprise Architecture Software 下载了一个 XML 模式。我想做的一件事是开始开发符合这些以 XML 模式呈现的模型的 XML 文件。

问题

在 XML 文件中,我链接到架构。然而,我得到一个错误cannot find the declaration of element。架构中的根元素是<xs:complexType name="Product">,但我确实注意到它有一个名为注解的兄弟元素,但它没有被命名。我仍然尝试将此注释用作根,但遇到了同样的问题。

我也尝试使用“产品类型”作为根元素作为模式中类型元素的第一个标记,但也遇到了同样的错误。好像知道的元素可以作为根。

问题

我在这里做错了什么?我无法更改架构,因为这是软件的输出。

架构

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://www.w3schools.com/product"
    elementFormDefault="qualified"
    xmlns="http://www.w3schools.com/product product.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:annotation>
        <xs:documentation>The Product defines the product(s) which are in focus for the Integrated Product Support (IPS) program. Once defined, a Product will then come in one or many Product variants. </xs:documentation>
    </xs:annotation>
    <xs:complexType name="Product">
        <xs:annotation>
            <xs:documentation>Product is &lt;&lt;class&gt;&gt; that represents a family of items which share the same underlying design purpose.</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="productIdentifier" minOccurs="1" maxOccurs="unbounded">
                <xs:annotation>
                    <xs:documentation>productIdentifier is an identifier that establishes a unique designator for a Product and to differentiate it from other instances of Product.</xs:documentation>
                </xs:annotation>
            </xs:element>
            <xs:element name="productName" minOccurs="0" maxOccurs="unbounded">
                <xs:annotation>
                    <xs:documentation>productName is a name by which the Product is known and can be easily referenced.</xs:documentation>
                </xs:annotation>
            </xs:element>
            <xs:element name="ProductVariant" minOccurs="1" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="ProductVariant">
        <xs:annotation>
            <xs:documentation>A productVariant is a &lt;&lt;class&gt;&gt; that defines a member of a Product family which is configured for a specific purpose and is made available to the market.</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="productVariantIdentifier" minOccurs="1" maxOccurs="unbounded">
                <xs:annotation>
                    <xs:documentation>productVariantIdentifier is an identifier that establishes a unique designator for a ProductVariant and to differentiate it from other instances of ProductVariant. </xs:documentation>
                </xs:annotation>
            </xs:element>
            <xs:element name="productVariantName" minOccurs="0" maxOccurs="unbounded">
                <xs:annotation>
                    <xs:documentation>productVariantName is a name by which the ProductVariant is known and can be easily referenced.</xs:documentation>
                </xs:annotation>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

XML

<?xml version="1.0" encoding="utf-8"?>
<Product xmlns="http://www.w3schools.com/product"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://www.w3schools.com/product product.xsd">
</Product>

注意

xml 和 .xsd 文件都在同一个文件夹中。

【问题讨论】:

  • 你能发布XSDSchema的图表吗?您只声明了两种复杂类型,但没有声明元素。所以这个 xsd 没有有效的 xml。
  • 您究竟从哪里下载了该架构?

标签: xml xsd schema enterprise-architect


【解决方案1】:

您是认真地说某些生产软件在生产模式中使用命名空间http://www.w3schools.com/product 吗?这是一个非常令人震惊的设计选择。

这是一个非常奇怪的模式,因为它没有定义全局元素声明。因此,编写有效 XML 的唯一方法是让根元素包含引用全局类型声明之一的 xsi:type 属性:例如

<Widget xsi:type="p:Product" xmlns:p="http://www.w3schools.com/product">  
  <p:productIdentifier>...

有一些众所周知的模式使用这种方法,例如 FPML,但它非常不寻常,在我看来是有问题的。

【讨论】:

  • 我不在那个特定领域,但对于 EA,我不会惊讶有人会说出你的所作所为 ;-)
猜你喜欢
  • 2015-07-04
  • 1970-01-01
  • 2012-07-02
  • 1970-01-01
  • 1970-01-01
  • 2015-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多