xml - 如何定义 XSD 元素具有多个子元素,所有子元素都很简单并且只有属性?

标签 xml xsd

我正在尝试定义 XSD 的一个部分,其中以下 XML 有效:

<machine>
    <timing offset="3s" period="20s"/>

    <!-- <machine> actually has many child elements, all of them are like <timing>
         and only have attributes -->
</machine>

这是我到目前为止所拥有的:

<xs:element name="machine">
    <xs:complexType>
        <xs:all>
            <!-- Definition for the <timing> child element. -->
            <xs:element name="timing" type="timing_type"/>
            <xs:complexType name="timing_type">
                <xs:attribute name="offset" type="xs:string"/>
                <xs:attribute name="period" type="xs:string"/>
            </xs:complexType>

            <!-- Definitions for the other <machine> child elements... -->
        </xs:all>
    </xd:complexType>
</xs:element>

在显示 <xs:complexType name="timing_type"> 的行上,我收到以下错误消息:

Error resolving component 'timing_type'. It was detected that 'timing_type' is in namespace 'http://www.w3.org/ 2001/XMLSchema', but components from this namespace are not referenceable from schema document 'file:////mySchema.xsd'. If this is the incorrect namespace, perhaps the prefix of 'timing_type' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file:////mySchema.xsd'.

你知道我在这里做错了什么吗?我如何为 <machine> 定义一大堆 child ?都是简单的,没有 child ,只有属性?提前致谢!

最佳答案

由于您似乎正在学习 XSD,因此构建更完整的 XML 示例可能会更容易,然后使用某些工具从所有这些示例 XML 生成 XSD。从生成的 XSD 中,您可以学到很多东西,并且它应该使您可以更轻松地逐步更改它以更好地满足您的目标。

错误在于,除了 xs:schemaxs:redefine 之外,您不能将命名类型嵌套在任何其他内容下。

因此,如果您如下所示,您的截图将是正确的:

<?xml version="1.0" encoding="utf-8" ?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="machine">
        <xs:complexType>
            <xs:all>
                <!-- Definition for the <timing> child element. -->
                <xs:element name="timing" type="timing_type"/>
                <!-- Definitions for the other <machine> child elements... -->
            </xs:all>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="timing_type">
        <xs:attribute name="offset" type="xs:string"/>
        <xs:attribute name="period" type="xs:string"/>
    </xs:complexType>
</xs:schema>

您可能遇到的问题与使用 xs:all 而不是 xs:sequencexs:choice 有关。在 XSD 1.0 中,xs:all 非常挑剔,因为 timing 元素不能出现多次。

下面的 XSD 是由工具生成的。它使用序列合成器(而不是全部),这将允许我添加的变化(maxOccurs =“unbounded”)

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="machine">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="timing" maxOccurs="unbounded">
          <xsd:complexType>
            <xsd:attribute name="offset" type="xsd:string" use="required" />
            <xsd:attribute name="period" type="xsd:string" use="required" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

如果您向混合中添加另一种类型的元素(如您所建议的),并且您希望允许任意数量的此类元素,并且以任何顺序单独出现多次,那么下面的模型将起作用。这里需要注意的是xsd:choice maxOccurs="unbounded"

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="machine">
    <xsd:complexType>
      <xsd:choice maxOccurs="unbounded">
        <xsd:element name="timing">
          <xsd:complexType>
            <xsd:attribute name="offset" type="xsd:string" use="required" />
            <xsd:attribute name="period" type="xsd:string" use="required" />
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="cycle">
          <xsd:complexType>
            <xsd:attribute name="duration" type="xsd:positiveInteger" use="required" />
            <xsd:attribute name="period" type="xsd:positiveInteger" use="required" />
          </xsd:complexType>
        </xsd:element>          
      </xsd:choice>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

关于xml - 如何定义 XSD 元素具有多个子元素,所有子元素都很简单并且只有属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12656509/

相关文章:

xml - 如何忽略未知标签的验证?

ios - 如何在 iOS RSS 应用程序中显示解析后的 XML 中的图像?

java - xml 的文档生成器给出错误

xml - XMlSchema 中的扩展,如何?

xsd - 文字的 xsd 数据类型有子类吗?

c# - 基于 XSD 变化的动态 XSLT 生成

java - 在哪里可以找到 Netbeans RCP maven 项目中的主要 layer.xml 文件?

android - 创建 xml android 布局的多个实例

xml - 如何使用 schemaLocation 或 noNamespaceSchemaLocation 将 XML 链接到 XSD?

java - cvc-数据类型-valid.1.2.1 : 'mylns:xsi' is not a valid value for 'NCName'