开发者

Reference to an ID

In order to validate XML documents, I would like to know if was it possible to make a reference on an attribute value.

I have states with a numeric value in ID attribute. In these states, a son is here in order to give a transition to another state. I would like to put here a reference in order to be sure the specified ID in this son is an existant ID in a state.

The main goal is to validate the document only if all ID are specified in the document.

There is my XSD document :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="2.0">
    <xs:eleme开发者_JAVA百科nt name="scxml">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" name="state">
                    <xs:complexType>
                        <xs:choice maxOccurs="1">
                                <xs:element name="nextstate" type="xs:nonNegativeInteger"/>
                            </xs:sequence>
                        </xs:choice>
                        <xs:attribute name="id" use="required" type="xs:nonNegativeInteger"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="version" type="xs:string" use="optional"/>
        </xs:complexType>
        <xs:unique name="uniqueStateID">
            <xs:selector xpath="./state"/>
            <xs:field xpath="@id"/>
        </xs:unique>
    </xs:element>
</xs:schema>

So I would like the value of NEXTSTATE node is a reference to the value of id attribute from node STATE.

Valid Document Example (two states, one transition to an existant ID) :

<sxml version="1.0">
    <state id="1">
        <nextstate>5</nextstate>
    </state>
    <state id="5" />
</scxml>

Invalid Document Example (two states, one transition to an no-existant ID) :

<sxml version="1.0">
    <state id="1">
        <nextstate>5</nextstate>
    </state>
</scxml>

Do you know if is it possible ?

Thanks for answers :)


You can validate that the nextstate attribute refers to an existing state. Instead of <unique>, use <key>in combination with <keyref>. Example here: http://www.w3.org/TR/xmlschema-0/#report.xsd

However, you cannot validate with XML schema that every state is referred to by a nextstate attribute, if that was what you wanted to achieve with "The main goal is to validate the document only if all ID are specified in the document".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜