xml - 递归键值对的 XML 模式
问题描述
考虑以下 XML 示例文档,该文档包含具有键值对的变量,这些变量也可以是递归的:
<?xml version="1.0" encoding="UTF-8"?>
<environments>
<variable>
<key>Variable 1</key>
<value>Value</value>
</variable>
<variable>
<value>B</value>
<key>Variable 2</key>
</variable>
<variable>
<value></value>
<key>Variable 2</key>
</variable>
<variable>
<key>Variable 2</key>
<value>
<variable>
<key>Foo</key>
<value>Bar</value>
</variable>
</value>
</variable>
<variable>
<key>Variable 2</key>
<value>
<variable>
<key>Foo</key>
<value>
<variable>
<key>Foo</key>
<value>Bar</value>
</variable>
</value>
</variable>
</value>
</variable>
</environments>
我想创建一个可以验证此结构的 XML 模式:零个或多个variable
元素,key
元素仅为字符串,元素仅为value
字符串或嵌套变量。
到目前为止,我想出了这个:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
<!-- Element: Environments -->
<xs:element name="environments">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element ref="variable"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- Element: variable_type -->
<xs:element name="variable">
<xs:complexType>
<xs:all>
<xs:element ref="key"/>
<xs:element ref="value"/>
</xs:all>
</xs:complexType>
</xs:element>
<!-- Element: key -->
<xs:element name="key" type="xs:string"/>
<!-- Element: value -->
<xs:element name="value">
<xs:complexType mixed="true">
<xs:sequence>
<xs:choice>
<xs:element minOccurs="0" maxOccurs="unbounded" ref="variable"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
此架构适用于我的示例文档。但是,当涉及到 value 元素时,我非常不确定:<xs:complexType mixed="true">
. 这意味着variable
像这样的元素也将被视为有效(foo
嵌套variable
元素之前的额外字符):
<variable>
<key>Variable 2</key>
<value>
foo
<variable>
<key>Foo</key>
<value>Bar</value>
</variable>
</value>
</variable>
我的问题:如何确定该value
元素是另一个variable
元素(复杂类型)还是只是一个字符串?
解决方案
XSD 中的混合内容实际上只适用于叙述性文本文档。除了使用 XSD 1.1 断言之外,您可以对混合内容施加的有效约束很少。如果可以的话,最好避免这种内容模型。
推荐阅读
- javascript - 为什么在使用 Google Maps Geolocation 时某些地址没有所有组件?
- c# - 在自己的线程上运行窗口
- razor - 动态改变 asp.net core razor 页面表单动作
- rest - 如何将参数传递给 Xpages 中的 Rest 服务
- ssl - IIS 10 和 HTTP/2 - 需要客户端证书
- php - 如何为后端控制器执行单元测试
- c# - 如何使用 C# 中的参数设置 RDLC 中文本框的字体大小
- tsql - CodeTo128 函数可用性
- java - 当配置 org.hibernate.SQL 记录器时,Logback 显示带有根级别 INFO 的 DEBUG 输出
- c - sprintf 在带中断的 32 位 MCU 中重新进入 64 位操作