簡體   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