You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用XML Schema定义由文档作者指定校验码的item元素?

Can XML Schema Enforce <item> code Attributes to Match <validCodes> Values?

Great question! The short answer is yes—but only if you use XML Schema 1.1. Here's the breakdown:

XSD 1.0 Limitation

Standard XSD 1.0 can't handle this requirement natively. It's a static schema language, meaning validation rules are defined upfront and can't reference dynamic values from the instance document itself. So you can't write an XSD 1.0 rule that says "only allow code values that appear in the <validCodes> section of this specific XML file."

XSD 1.1 Solution: xs:assert

XSD 1.1 introduced the <xs:assert> element, which lets you write XPath expressions to validate cross-document conditions. This is exactly what you need to enforce that every <item>'s code attribute matches one of the values defined in <validCodes>.

Example XSD 1.1 Schema

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <!-- Define the validCodes section -->
        <xs:element name="validCodes">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="code" type="xs:string" maxOccurs="unbounded"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <!-- Define the item elements with code attribute -->
        <xs:element name="item" maxOccurs="unbounded">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute name="code" type="xs:string" use="required"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <!-- Assertion to validate code values -->
      <xs:assert test="every $item in ./item satisfies $item/@code = ./validCodes/code/text()"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

Valid Instance Example

<root>
  <validCodes>
    <code>AA</code>
    <code>BB</code>
  </validCodes>
  <item code="AA">VALID</item>
  <item code="BB">VALID</item>
</root>

Invalid Instance Example

This would fail validation because CC isn't in <validCodes>:

<root>
  <validCodes>
    <code>AA</code>
    <code>BB</code>
  </validCodes>
  <item code="AA">VALID</item>
  <item code="CC">INVALID</item>
</root>

Notes

  • Not all XML tools support XSD 1.1. Make sure your validator (like Xerces-J, Saxon, or Oxygen XML Editor) is compatible.
  • If you're stuck with XSD 1.0, workarounds include:
    • Dynamically generating an XSD from the <validCodes> section before validation.
    • Using Schematron (a complementary validation language) alongside XSD 1.0 to handle cross-document rules.

内容的提问来源于stack exchange,提问作者Kevin Shea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:33:37