验证 XML against XSD schema
XSD (XML 模式 Definition) defines 结构, 元素 types, 和 constraints 的 XML document. Validate documents 针对 schemas 到确保 correctness.
- 1
XSD defines: 允许 elements 和 attributes, 其 types (字符串, 整数, 日期, 布尔值), occurrence constraints (minOccurs, maxOccurs), nesting rules, 和自定义 types (simpleType, complexType). Validation checks 该 XML document conforms 到模式 — catching structural 错误, missing 需要 fields, 和类型 mismatches.
- 2 XML 格式化器 →
格式化 the XML document to verify basic well-formedness (all tags closed, proper nesting, valid characters). Well-formed XML is the minimum requirement — schema validation checks conformance to a specific structure.
- 3 JSON 验证器 →
为 JSON alternatives, use JSON Schema validation. 转换 XSD 到 JSON Schema (或写入 one directly) 和验证 JSON converted 从 XML. JSON Schema 是 simpler than XSD 和更多通常 used 在现代 APIs.
- 4
XSD validation catches: missing 需要 elements, elements 在错误 order (如果序列是 specified), 无效 types (字符串 where 整数 expected), too 许多或 too 少数 occurrences, 无效属性 values. Use 它为 B2B 数据 exchange where strict contracts 是需要.
常见问题
- XSD vs DTD vs RelaxNG?
- XSD (W3C) 是最多宽 supported — rich 类型系统, namespaces. DTD 是 older 和 simpler 但 limited (否 namespaces, limited types). RelaxNG 是更多 expressive 但更少 adopted. Use XSD 为最大值 compatibility.
- 应该 I use XSD 或 JSON Schema?
- 为 XML systems, use XSD. 为 JSON APIs, use JSON Schema. 如果 migrating 从 XML 到 JSON, 转换 XSD 到 JSON Schema as 开始 point, 但审查 — 映射是 不总是 direct. JSON Schema 是 simpler 和更多 web-friendly.