【问题标题】:XSD keyref or number literalXSD keyref 或数字文字
【发布时间】:2012-07-26 02:21:48
【问题描述】:

是否可以定义一个可以是 keyref 或数字文字的类型?就像在任何类型化的编程语言中一样,您可以将数字文字或另一个变量的名称分配给数字变量。我正在制作一个模式来定义绘图 api 元素(对于另一种编程语言),并且想定义一个 color 类型,它可以是十六进制文字(例如 0xFF0000 代表鲜红色)或者是对颜色在别处定义。所以你可以这样做(在 XML 文档中):

<color key="dialogBorder1">0x222222</color>
<color key="dialogFill1">0xCCCCCC</color>

<!-- later... -->
<windowTheme name="warningWindow">
  <border>
    <color>0xFF0000</color> <!-- defined literally -->
  </border>
  <fill>
    <solid>
      <color>dialogFill1</color>  <!-- defined by keyref -->
    </solid>
  </fill>
</windowTheme>

如果可以对属性施加 choice 限制,我可以执行以下操作,但我的印象是当前 (1.0) 版本的 XSD 规范无法做到这一点。

<!-- I wish: -->
<xs:complexType name="colorType" >
  <xs:attrchoice>
    <xs:attribute name="value" type="HexLiteral" /> <!-- literal -->
    <xs:attribute name="ref" type="xs:string" />    <!-- keyref (defined elsewhere) -->
  </xs:attrchoice>
</xs:complexType>

这将允许 either lieral value 或 keyref ref

<color value="0xFF0000" />     <!-- OK -->
<color ref="dialogBorder1" />  <!-- OK -->

但不是两者兼有:

<color value="0xFF0000" ref="colorXYZ" /> <!-- NOT OK -->

【问题讨论】:

    标签: xsd


    【解决方案1】:

    帖子与描述的内容有些不一致。第一个 XML 显示不带属性使用的颜色,然后 colorType 的 xsd 带有一些属性。我假设 XML 是你想要的。

    所以以下适用于:

    定义一种颜色类型,它可以是十六进制文字(例如 0xFF0000 表示鲜红色),也可以是对其他地方定义的颜色的引用

        <xsd:simpleType name="ColorHex">
            <xsd:union memberTypes="xsd:string">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:pattern value="0x[0-9a-fA-F]{6}"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:union>
        </xsd:simpleType>
    

    上面使用了与 XHTML 中的 Color 类型相同的模式(我展示这个是为了给你灵感来源):

        <!-- sixteen color names or RGB color expression-->
        <xsd:simpleType name="Color">
            <xsd:union memberTypes="xsd:NMTOKEN">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:token">
                        <xsd:pattern value="#[0-9a-fA-F]{3}([0-9a-fA-F]{3})?"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:union>
        </xsd:simpleType>
    

    这里的想法是使用联合。不利的一面可能是,由于所有不匹配 HEX 模式的内容都将被字符串匹配,无效的 HEX 语法(例如缺少数字)将作为引用传递。

    其他缺点可能在于您所针对的编程语言对 xsd:union 的支持程度。

    【讨论】:

    • 您在这里指的颜色键在哪里?看起来问题要求 either 十六进制值 keyrefs。
    • @unhammer,我没有提供 key/keyref 示例。你可以找到一个here。假设 XSD 是使用不同的属性设计的,那么它是有道理的;不幸的是,这个问题没有得到澄清,并在第一个示例 XML 是目标状态的假设下被接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    • 2011-01-02
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多