如何为复合类型创建模式?

发布于 2024-09-15 01:09:08 字数 513 浏览 0 评论 0原文

我有一个如下所示的 XML 规范:

<Root>
    <Directory Name="SomeName">
        <TextFile Name="ExampleDocument.txt>This is the content of my sample text file.</TextFile>
        <Directory Name="SubDirectory">
            <Speech Name="Example Canned Speech">
               ...
            </Speech>
        </Directory>
    </Directory>
</Root>

请注意,Directory 元素可以包含其他 Directory 元素。我如何使用 W3C 架构来表示它?

I have an XML spec that looks like this:

<Root>
    <Directory Name="SomeName">
        <TextFile Name="ExampleDocument.txt>This is the content of my sample text file.</TextFile>
        <Directory Name="SubDirectory">
            <Speech Name="Example Canned Speech">
               ...
            </Speech>
        </Directory>
    </Directory>
</Root>

Note that Directory elements can contain other Directory elements. How can I represent that using W3C Schema?

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

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

发布评论

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

评论(2

信仰 2024-09-22 01:09:08

您需要创建一个递归 来表示您的 类型。根据您提供的元素,以下是此技术的示例。

  <xs:complexType name="DirectoryType">
    <xs:sequence>
      <xs:element name="TextFile"/>
      <xs:element name="Speech"/>
      <xs:element name="Directory" type="DirectoryType"/>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Directory" type="DirectoryType" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

You need to create a recursive <complexType> to represent your <Directory> type. The following is an example of this technique, given the elements you provided.

  <xs:complexType name="DirectoryType">
    <xs:sequence>
      <xs:element name="TextFile"/>
      <xs:element name="Speech"/>
      <xs:element name="Directory" type="DirectoryType"/>
    </xs:sequence>
  </xs:complexType>

  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Directory" type="DirectoryType" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
通知家属抬走 2024-09-22 01:09:08

这应该可以做到(您可能希望将名称限制在 xs:string 之外):

<?xml version="1.0" encoding="utf-8"?>
<!-- Remember to change namespace name with your own -->
<xs:schema
    targetNamespace="http://tempuri.org/XMLSchema1.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Root" type="Container"/>

  <xs:element name="Directory" >
    <xs:complexType>
      <xs:complexContent>
        <xs:extension base="Container">
          <xs:attribute name="Name" type="xs:string" use="required"/>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>

  <xs:element name="TextFile">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="Name" use="required"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="Container">
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="Directory"/>
        <xs:element ref="TextFile"/>
      </xs:choice>
  </xs:complexType>
</xs:schema>

测试于:

<?xml version="1.0" encoding="utf-8" ?>
<Root xmlns="http://tempuri.org/XMLSchema1.xsd">
  <Directory Name="SomeName">
    <TextFile Name="ExampleDocument.txt">This is the content of my sample text file.</TextFile>
    <Directory Name="SubDirectory">
    </Directory>
    <TextFile Name="hej">

    </TextFile>
  </Directory>
  <TextFile Name="file.txt"/>
</Root>

This should do it (you may want to restrict names further than to xs:string):

<?xml version="1.0" encoding="utf-8"?>
<!-- Remember to change namespace name with your own -->
<xs:schema
    targetNamespace="http://tempuri.org/XMLSchema1.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema1.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Root" type="Container"/>

  <xs:element name="Directory" >
    <xs:complexType>
      <xs:complexContent>
        <xs:extension base="Container">
          <xs:attribute name="Name" type="xs:string" use="required"/>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>

  <xs:element name="TextFile">
    <xs:complexType>
      <xs:simpleContent>
        <xs:extension base="xs:string">
          <xs:attribute name="Name" use="required"/>
        </xs:extension>
      </xs:simpleContent>
    </xs:complexType>
  </xs:element>

  <xs:complexType name="Container">
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="Directory"/>
        <xs:element ref="TextFile"/>
      </xs:choice>
  </xs:complexType>
</xs:schema>

Tested on:

<?xml version="1.0" encoding="utf-8" ?>
<Root xmlns="http://tempuri.org/XMLSchema1.xsd">
  <Directory Name="SomeName">
    <TextFile Name="ExampleDocument.txt">This is the content of my sample text file.</TextFile>
    <Directory Name="SubDirectory">
    </Directory>
    <TextFile Name="hej">

    </TextFile>
  </Directory>
  <TextFile Name="file.txt"/>
</Root>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文