XML 元素的值取决于 XSD 中其他元素的值?
Value of XML element depends on values of other elements in XSD?
例如,假设橙色 GMC 卡车价值 20,000 美元,而白色和黑色 GMC 卡车价值 10,000 美元。
给出以下 XML:
<example>
<car>
<make value='GMC'/>
<model value='Truck'/>
<configuration>
<color value="orange"/>
<bed value="short"/>
<cab value="regular"/>
</configuration>
<price value='10000'/>
</car>
</example>
XML 告诉我,我的销售人员正在以 10,000 美元的价格出售带有普通驾驶室的橙色 GMC 短床卡车。我想使用架构来防止我的员工以低于 20,000 美元的价格出售卡车。
我可以创建一个 XSD 文件来强制执行汽车必须是 GMC、卡车、橙色且价格为 20,000 美元的限制。换句话说,我可以对四个独立元素的值进行限制吗?
示例 XML 将无法验证,因为价格低于 20,000 美元,或者因为颜色是橙色而不是白色或黑色。就看你怎么看。
更新
根据http://www.ibm.com/developerworks/library/x-xml11pt2/
Unfortunately, XML Schema 1.0 did not provide a way to enforce these
rules. To implement such constraints, you would
- Write code at the application level (after XML schema validation)
- Use stylesheet checking (also a post-validation process)
- Use a different XML schema language such as RelaxNG or Schematron
With the constant requests for co-occurrence constraint checking
support from the XML Schema 1.0 user community, the XML Schema 1.1
working group introduced the concept of assertions and type
alternatives in XML Schema 1.1 to allow XML schema authors to express
such constraints.
好的,看看我当前的环境,我使用的是不支持 XSD 1.1 的 lxml。所以,我将不得不使用 Schematron 或 RelaxNG。
XSD 1.0
您的约束无法在 XSD 1.0 中表达。
XSD 1.1
您的约束可以使用 XSD 1.1 中的断言来表达:
<?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">
<xs:element name="example">
<xs:complexType>
<xs:sequence>
<xs:element name="car">
<xs:complexType>
<xs:sequence>
<xs:element name="make" type="valAttrType"/>
<xs:element name="model" type="valAttrType"/>
<xs:element name="configuration">
<xs:complexType>
<xs:sequence>
<xs:element name="color" type="valAttrType"/>
<xs:element name="bed" type="valAttrType"/>
<xs:element name="cab" type="valAttrType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="price" type="valAttrType"/>
</xs:sequence>
<xs:assert test="not( make/@value='GMC'
and model/@value='Truck'
and configuration/color/@value='orange')
or number(price/@value)=20000"/>
<xs:assert test="not( make/@value='GMC'
and model/@value='Truck'
and configuration/color/@value='black')
or number(price/@value)=10000"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="valAttrType">
<xs:attribute name="value" type="xs:string"/>
</xs:complexType>
</xs:schema>
例如,假设橙色 GMC 卡车价值 20,000 美元,而白色和黑色 GMC 卡车价值 10,000 美元。
给出以下 XML:
<example>
<car>
<make value='GMC'/>
<model value='Truck'/>
<configuration>
<color value="orange"/>
<bed value="short"/>
<cab value="regular"/>
</configuration>
<price value='10000'/>
</car>
</example>
XML 告诉我,我的销售人员正在以 10,000 美元的价格出售带有普通驾驶室的橙色 GMC 短床卡车。我想使用架构来防止我的员工以低于 20,000 美元的价格出售卡车。
我可以创建一个 XSD 文件来强制执行汽车必须是 GMC、卡车、橙色且价格为 20,000 美元的限制。换句话说,我可以对四个独立元素的值进行限制吗?
示例 XML 将无法验证,因为价格低于 20,000 美元,或者因为颜色是橙色而不是白色或黑色。就看你怎么看。
更新
根据http://www.ibm.com/developerworks/library/x-xml11pt2/
Unfortunately, XML Schema 1.0 did not provide a way to enforce these rules. To implement such constraints, you would
- Write code at the application level (after XML schema validation)
- Use stylesheet checking (also a post-validation process)
- Use a different XML schema language such as RelaxNG or Schematron
With the constant requests for co-occurrence constraint checking support from the XML Schema 1.0 user community, the XML Schema 1.1 working group introduced the concept of assertions and type alternatives in XML Schema 1.1 to allow XML schema authors to express such constraints.
好的,看看我当前的环境,我使用的是不支持 XSD 1.1 的 lxml。所以,我将不得不使用 Schematron 或 RelaxNG。
XSD 1.0
您的约束无法在 XSD 1.0 中表达。
XSD 1.1
您的约束可以使用 XSD 1.1 中的断言来表达:
<?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">
<xs:element name="example">
<xs:complexType>
<xs:sequence>
<xs:element name="car">
<xs:complexType>
<xs:sequence>
<xs:element name="make" type="valAttrType"/>
<xs:element name="model" type="valAttrType"/>
<xs:element name="configuration">
<xs:complexType>
<xs:sequence>
<xs:element name="color" type="valAttrType"/>
<xs:element name="bed" type="valAttrType"/>
<xs:element name="cab" type="valAttrType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="price" type="valAttrType"/>
</xs:sequence>
<xs:assert test="not( make/@value='GMC'
and model/@value='Truck'
and configuration/color/@value='orange')
or number(price/@value)=20000"/>
<xs:assert test="not( make/@value='GMC'
and model/@value='Truck'
and configuration/color/@value='black')
or number(price/@value)=10000"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="valAttrType">
<xs:attribute name="value" type="xs:string"/>
</xs:complexType>
</xs:schema>