繁体   English   中英

将XML属性的值限制为其他属性/元素的值

[英]Restrict value of an XML attribute to different attribute's/element's value

我将从一个例子开始

<template>
  <components>
    <component name="switch" />
    <component name="server" />
  </components>
  <layout>
    <grid>
      <position componentName="switch" positionX="0" positionY="0" />
    </grid>
  </layout>
</template>

我想要的是限制componentName属性中的值以匹配上面在components指定的名称之一。 JAXB有可能吗? 因为我需要带注释的类,然后将其用于生成XSD。

根据您的情况,XSD 1.0可以通过key / keyref组合来强制执行“参照完整性”。 但是,我不知道JAXB中这些构造有什么注释(听起来好像您正在从Java类中生成XSD)。 至少我从未遇到过这样的注释(请参阅此处的列表)

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="template">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="components">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element maxOccurs="unbounded" name="component">
                <xsd:complexType>
                  <xsd:attribute name="name" type="xsd:string" use="required" />
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
        <xsd:element name="layout">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="grid">
                <xsd:complexType>
                  <xsd:sequence>
                    <xsd:element name="position">
                      <xsd:complexType>
                        <xsd:attribute name="componentName" type="xsd:string" use="required" />
                        <xsd:attribute name="positionX" type="xsd:unsignedByte" use="required" />
                        <xsd:attribute name="positionY" type="xsd:unsignedByte" use="required" />
                      </xsd:complexType>
                    </xsd:element>
                  </xsd:sequence>
                </xsd:complexType>
              </xsd:element>
            </xsd:sequence>
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
    <xsd:key name="ComponentsKey">
        <xsd:selector xpath="components/component"/>
        <xsd:field xpath="@name"/>
    </xsd:key>
    <xsd:keyref name="MatchComponent" refer="ComponentsKey">
        <xsd:selector xpath="layout/grid/position"/>
        <xsd:field xpath="@componentName"/>
    </xsd:keyref>
  </xsd:element>
</xsd:schema>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM