XML 元素的值取决于 XSD 中其他元素的值? [英] Value of XML element depends on values of other elements in XSD?

查看:22
本文介绍了XML 元素的值取决于 XSD 中其他元素的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,假设橙色 GMC 卡车价值 20,000 美元,而白色和黑色 GMC 卡车价值 10,000 美元.

As example, let's say that orange GMC trucks are worth $20,000 while white and black GMC trucks are worth $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 美元的价格出售卡车.

The XML is telling me that my sales staff is selling an orange GMC short-bed truck with a regular cab for $10,000. I want to use a schema to prevent my staff from selling the truck for less than $20,000.

我可以创建一个 XSD 文件来强制执行以下限制:汽车必须是 GMC、卡车、橙色并且售价 20,000 美元.换句话说,我可以基于四个单独元素的值来限制吗?

Can I create an XSD file to enforce the restriction that the car must be a GMC, a truck, orange, and priced at $20,000. In other words can I base a restriction on the values of four separate elements?

示例 XML 将无法验证,因为价格低于 20,000 美元,或者因为颜色是橙色而不是白色或黑色.取决于你想如何看待它.

The example XML would fail to validate because the price is less than $20,000 or because the color is orange instead of white or black. Depends on how you want to look at it.

更新

根据 http://www.ibm.com/developerworks/library/x-xml11pt2/

不幸的是,XML Schema 1.0 没有提供强制执行这些的方法规则.要实施此类约束,您需要

Unfortunately, XML Schema 1.0 did not provide a way to enforce these rules. To implement such constraints, you would

  • 在应用程序级别编写代码(在 XML 架构验证之后)
  • 使用样式表检查(也是一个验证后过程)
  • 使用不同的 XML 模式语言,例如 RelaxNG 或 Schematron

随着对共现约束检查的不断请求来自 XML Schema 1.0 用户社区的支持,XML Schema 1.1工作组引入了断言和类型的概念XML Schema 1.1 中的替代方案,允许 XML 模式作者表达此类约束.

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.

OK, so looking at my current environment I'm using lxml which does not support XSD 1.1. So, I will have to use Schematron or RelaxNG.

推荐答案

XSD 1.0

您的约束无法在 XSD 1.0 中表达.

XSD 1.0

Your constraints cannot be expressed in XSD 1.0.

您的约束可以使用 XSD 1.1 中的断言来表达:

Your constraints can be expressed using assertions in 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>

这篇关于XML 元素的值取决于 XSD 中其他元素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆