XSD模式 - 基于文档中的值的枚举 [英] XSD Schemas - Enumeration based on value in document

查看:131
本文介绍了XSD模式 - 基于文档中的值的枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个正确的长镜头。

This is a right long shot.

如果我的XSD中有一个定义的元素,包含两个属性,比如说NAME和VALUE,可以限制基于名称中的值的VALUE枚举?

If I have a defined element in my XSD, containing two attributes, say NAME and VALUE, is it possible to restrict the VALUE enumeration based on the value in the Name?

例如:

<xsd:complexType name="ConfigDef">
    <xsd:attribute name="NAME" type="xsd:string">
        <xsd:annotation>
            <xsd:documentation>The name of this Config setting.</xsd:documentation>
        </xsd:annotation>           
    </xsd:attribute>
    <xsd:attribute name="VALUE" type="xsd:string">
        <xsd:annotation>
            <xsd:documentation>The value of this Config setting.</xsd:documentation>
        </xsd:annotation>
    </xsd:attribute>
</xsd:complexType>

我不认为可以做到。可以做吗是否有XSD黑客让它工作?

I don't think it can be done. Can it be done? Is there XSD hacks to get it to work?

推荐答案

您可以使用嵌入式schematron

You could do this with embedded schematron

<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="ConfigDef">
    <xsd:annotation>
      <xsd:appinfo>
        <sch:pattern name="Test constraints on the ConfigDef element"
          xmlns:sch="http://purl.oclc.org/dsdl/schematron">
          <sch:rule
            context="ConfigDef">
            <sch:assert test="@NAME eq 'foo' and @VALUE = ('bar','baz')">Error message when the assertion condition is broken...</sch:assert>
            <sch:assert test="@NAME eq 'qux' and @VALUE = ('teh','xyz')">Error message when the assertion condition is broken...</sch:assert>
          </sch:rule>
        </sch:pattern>
      </xsd:appinfo>
    </xsd:annotation>
    <xsd:attribute name="NAME" type="xsd:string">
      <xsd:annotation>
        <xsd:documentation>The name of this Config setting.</xsd:documentation>
      </xsd:annotation>           
    </xsd:attribute>
    <xsd:attribute name="VALUE" type="xsd:string">
      <xsd:annotation>
        <xsd:documentation>The value of this Config setting.</xsd:documentation>
      </xsd:annotation>
    </xsd:attribute>
  </xsd:complexType>
</xsd:schema>

或者您可以使用放松模式来执行此操作

Or you could do this with a Relax Schema

element ConfigDef {
  ((
  attribute NAME {'foo'},
  attribute VALUE {'bar'|'baz'}) |  
  (
  attribute NAME {'qux'},
  attribute VALUE {'teh'|'xyz'}))
}

这篇关于XSD模式 - 基于文档中的值的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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