基于属性值的条件(XML 架构)

2024-02-08

是否可以在 XML 模式中定义基于属性值的条件?例如,当test@attrib="one", 我想one-element被允许和强制或当test@attrib="two", 我想two-element是允许的和强制性的。

例如,有效文件是:

<root>
    <test attrib="one"/>
    <some-element-1/>
    <some-element-2/>
    ...
    <some-element-n/>
    <one-element>
    </one-element>
</root>

or

<root>
    <test attrib="two"/>
    <some-element-1/>
    <some-element-2/>
    ...
    <some-element-n/>
    <two-element>
    </two-element>
</root>

错误的文件:

<root>
    <test attrib="one"/>
    <some-element-1/>
    <some-element-2/>
    ...
    <some-element-n/>
</root>

or

<root>
    <test attrib="two"/>
    <some-element-1/>
    <some-element-2/>
    ...
    <some-element-n/>
    <one-element>
    </one-element>
</root>

XSD 中可以吗?


不在同一类型内。您需要为每个不同的选项定义不同的类型。

UPDATE

要在架构中重用类型定义:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns="http://My.Schema.Namespace" 
           targetNamespace="http://My.Schema.Namespace">

  <xs:element name="root">
    <xs:complexType>
      <xs:choice>
        <xs:element name="test1" type="test1Type" />
        <xs:element name="test2" type="test2Type" />
      </xs:choice>
    </xs:complexType>
  </xs:element>

  <!-- define the two root types -->
  <xs:complexType name="test1Type">
    <xs:all>
      <xs:element name="some-element-1" type="some-element-1Type" />
      <xs:element name="some-element-2" type="some-element-2Type" />
      <xs:element name="some-element-3" type="some-element-3Type" />
      <xs:element name="one-element" type="one-elementType" />
    </xs:all>
    <xs:attribute name="attrib" type="xs:string" fixed="one" />
  </xs:complexType>

  <xs:complexType name="test2Type">
    <xs:all>
      <xs:element name="some-element-1" type="some-element-1Type" />
      <xs:element name="some-element-2" type="some-element-2Type" />
      <xs:element name="some-element-3" type="some-element-3Type" />
      <xs:element name="two-element" type="two-elementType" />
    </xs:all>
    <xs:attribute name="attrib" type="xs:string" fixed="two" />
  </xs:complexType>

  <!-- Define re-usable types-->
  <xs:complexType mixed="true" name="some-element-1Type"/>
  <xs:complexType mixed="true" name="some-element-2Type"/>
  <xs:complexType mixed="true" name="some-element-3Type"/>
  <xs:complexType mixed="true" name="one-elementType"/>
  <xs:complexType mixed="true" name="two-elementType"/>

</xs:schema>

这将验证:

<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://My.Schema.Namespace">
  <test1 attrib="one">
    <some-element-1>sadas</some-element-1>
    <some-element-2>sadas</some-element-2>
    <some-element-3>sadas</some-element-3>
    <one-element>sadas</one-element>
  </test1>
</root>

and

<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://My.Schema.Namespace">
  <test2 attrib="two">
    <some-element-1>sadas</some-element-1>
    <some-element-2>sadas</some-element-2>
    <some-element-3>sadas</some-element-3>
    <two-element>sadas</two-element>
  </test2>
</root>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于属性值的条件(XML 架构) 的相关文章

随机推荐

  • Java泛型如何接受泛型参数中的任何派生类型

    在下面2行代码中 HashMap
  • Eclipse 如何终止正在运行的程序?

    在 Eclipse 中 当您运行程序时 有一个漂亮的红色方形按钮可以停止它 我想知道这是如何做到的 Eclipse 是否只是残酷地杀死相应的进程 或者在温和地要求它终止自身或其他什么之前对进程状态进行一些很好的安全验证 是的 Eclipse
  • Docker postgres 无效的主检查点记录

    我一直在尝试让 postgres 在 Docker 在 Windows 上 中运行并具有持久数据存储 并认为我终于拥有了它 但现在当我尝试启动容器时 出现以下错误 LOG invalid primary checkpoint record
  • 是否可以将自定义元数据添加到 Lucene 字段?

    我已经到了需要存储一些有关 Lucene Net 索引中特定字段来自何处的附加数据的地步 具体来说 我想在将字段添加到文档时将 guid 附加到文档的某些字段 并在从搜索结果中获取文档时再次检索它 这可能吗 Edit 好吧 让我举个例子来澄
  • 如何动态更改引导程序模态体

    所以我将 Bootstrap 模态定义为 div class modal fade div class modal dialog modal sm div class modal content div class modal header
  • 如何在Android应用程序中读取和使用网站内容

    我实际上必须在 Android 应用程序中阅读和使用某些网站的内容 到目前为止 我主要使用两种不同的代码来获取网站的内容 但它们对我不起作用 public static String connect String url String re
  • 将 django 默认 pk 与 AutoField 更改为 BigAutoField

    我的模型有一个带有 AutoField 整数 的默认 pk 但后来我发现我需要使用 BigAutoField 然后我还有引用学生模型的其他模型的数据 我如何将 pk 字段更改为 BigAutoField 并反映在其他引用模型上 class
  • 当我尝试使用 vue js 显示 JSON 数据时,Selectpicker 不起作用?

    当我写 selectpicker selectpicker refresh 时在控制台中它正在加载 我应该在哪里提供此代码 我的 HTML 代码是
  • 错误 1045 (28000):用户“root”@“localhost”的访问被拒绝

    所以我第一次安装了MySQL应用程序 首先我看到命令行客户端没有打开 所以我寻找解决方案 他们说我必须进入 bin 目录并手动运行它 在我运行cmd之后mysql uroot p运行它并输入密码 它给了我错误 错误 1045 28000 用
  • 剪刀石头布 - Python 3 - 初学者

    我想模拟一个石头剪刀布游戏 这就是我到目前为止所拥有的 它不允许我在其中输入字母scoregame功能 我怎样才能解决这个问题 def scoregame player1 player2 if player1 R and player2 R
  • 防止选项卡循环浏览地址栏

    我意识到这可能是一个可访问性问题 最好不要管它 但我想弄清楚是否可以阻止选项卡在选项卡循环中访问地址栏 我的应用程序有另一种循环输入区域的方法 但许多新用户本能地尝试使用该选项卡 但它无法按预期工作 这是一个通用的 jquery 实现 您不
  • Selenium Instagram 机器人 - 单击“喜欢”按钮

    我已经编写了一个程序来访问 Instagram 探索页面 并喜欢前六张照片 现在 我正在使用这种相当复杂的方法来查找 喜欢 按钮 我更愿意将其格式化为 单击登录按钮 部分 到目前为止 我已经尝试检查各种元素 但我无法确定正确的元素以使其唯一
  • Chart.js 圆环图大小调整

    我在用着ng2 图表 https github com valor software ng2 charts在 Angular 项目中 我有该项目的两个版本 当前的生产版本是旧版本 并且使用旧版本的 ng2 charts 以及扩展的 Char
  • Rust 中与闭包和生命周期作斗争

    我正在尝试将一些基准测试从 F 移植到 Rust F 代码如下所示 let inline iterNeighbors f i j f i 1 j f i 1 j f i j 1 f i j 1 let rec nthLoop n s1 Ha
  • 休息删除线

    是否可以在重组文本中删除文本 例如呈现为
  • 将自定义控件中的编辑框动态绑定到托管 bean

    我读过许多关于在自定义控件中动态绑定字段的优秀帖子和文章 但它们都假设了文档数据源 我想允许托管 bean 数据源的可能性 我尝试将属性类型设置为com ibm xsp model DataSource or com ibm xsp ext
  • Enunciate 能否为处理泛型类型的 API 生成文档?

    给定一个抽象的通用 Resource 类和一个具体的实现 public abstract class AbstractResource
  • 如何阻止混淆整个包?

    我需要防止 Proguard 混淆包中的任何类com foo 我努力了 keep com foo
  • 如何模拟当前日期?

    我的代码中有一个方法可以验证当天是否正常工作 它会为此检查数据库日历 方法如下所示 public boolean iBusinessDayToday LocalDate today LocalDate now logic with toda
  • 基于属性值的条件(XML 架构)

    是否可以在 XML 模式中定义基于属性值的条件 例如 当test attrib one 我想one element被允许和强制或当test attrib two 我想two element是允许的和强制性的 例如 有效文件是