具有属性和子元素的 XSD 元素 [英] XSD element with both attributes and child elements

查看:19
本文介绍了具有属性和子元素的 XSD 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道定义包含属性和子元素的元素的正确语法(如果可能的话).例如:

I want to know the correct syntax for defining an element containing both attributes and child elements (if possible). For example:

<component type="A" binding="B">
  <operation name="X">
    <input type="C" />
  </operation>

  <event name="Y">
    <output type="D" />
  </event>
</component>

如您所见,组件、操作和事件都具有属性和子元素.是否可以在 XSD 中定义它?怎么样?

As you can see, both elements component, operation and event have both attributes and child element. Is it possible to define this in XSD? How?

非常感谢!

推荐答案

这是定义与您的 XML 匹配的 XSD 的一种可能方法;在学习 XSD 时,您可以注册一个为您推断 XSD 的工具的帮助,从一个或多个 XML 示例文件开始.

This is one possible way to define an XSD matching your XML; when learning XSD, you could enroll the help of a tool that infers the XSD for you, starting from one or more XML sample files.

<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="component">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="operation">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="input">
                <xsd:complexType>
                  <xsd:attribute name="type" type="xsd:string" use="required" />
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="name" type="xsd:string" use="required" />
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="event">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="output">
                <xsd:complexType>
                  <xsd:attribute name="type" type="xsd:string" use="required" />
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="name" type="xsd:string" use="required" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
      <xsd:attribute name="type" type="xsd:string" use="required" />
      <xsd:attribute name="binding" type="xsd:string" use="required" />
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

在您逐步​​完成此操作时,您可能会开始调整 min/maxOccurs、使用(必需/可选)、重用定义等.生成的 XSD 是一个很好的起点,但通常最终会以一种或另一种方式进行编辑...

As you walkthrough this, you may start tweaking min/maxOccurs, use (required/optional), reusing definitions, etc. A generated XSD is a good starting point, but typically ends up being edited one way or another...

这篇关于具有属性和子元素的 XSD 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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