简体   繁体   中英

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

I will start with an example

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

What I want is to restrict values in componentName attribute to match one of the names specified above in components . Is this possible in JAXB? Because I need to have annotated classes which are then used to generate XSD.

Given your scenario, XSD 1.0 can enforce your "referential integrity" through a key/keyref combo. However, I am not aware of what annotations there are for these constructs in JAXB (it sounds as if you're looking at generating an XSD from you Java classes); at least I never ran into such annotations (see the list here )

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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