Featured post
xsd - XML Schema key/keyref - how to use them? -
long story short : know how use key/keyref xsd let elements have references each other. has have form of example, using simple xsd , xml.
long story : familiar usage of id/idref. use connect elements jaxb. have been told repeatedly key/keyref construct in xsd offers enhanced flexibility inter-element referencing. have consulted oreilly xml schema book, seems teach correct definition of key/keyref , how similar id/idref (but better) , doesn't give simple example of use. doesn't seem similar, because define id attribute in 1 element , idref in element , voila. key/keyref have defined in common ancestor of referencing , referenced element (afaik)...
i use xsd files generate jaxb-bound java classes xjc
i have searched how-tos, tutorials , examples, google gives me scraps. same searches on (also google , inclusive search '+' ).
in order make everyone's lives easier have prepared xsd defined key/keyref pair have understood it.
<xs:schema elementformdefault="qualified" xmlns:xs="http://www.w3.org/2001/xmlschema"> <xs:element name="root"> <xs:complextype> <xs:sequence> <xs:element name="referenced"> <xs:complextype> <xs:attribute name="id" type="xs:string" /> </xs:complextype> </xs:element> <xs:element name="owner"> <xs:complextype> <xs:attribute name="id" type="xs:string" /> </xs:complextype> </xs:element> </xs:sequence> </xs:complextype> <xs:key name="akey"> <xs:selector xpath="owner" /> <xs:field xpath="@id" /> </xs:key> <xs:keyref name="akeyref" refer="akey"> <xs:selector xpath="referenced" /> <xs:field xpath="@id" /> </xs:keyref> </xs:element>
how piece of xml like, 'owner'-element referencing 'referenced'-element?
edit : applied change proposed tom w, changing xpath attribute of key element "owner". jaxb (xjc) still doesnt care though.
thank you
there no special syntax in instance document. required keyref node matches extant key node. validation tell whether or not key constraint satisfied.
re code:
i've started dabbling keys myself, think i've spotted error in yours - akey
should like:
<xs:key name="akey"> <xs:selector xpath="owner" /> <xs:field xpath="@id" /> </xs:key>
furthermore - gotcha - key constraints don't recognise default namespace. must prefix every part of selector
xpath namespace prefix of element you're looking up. if don't have namespace prefix - tough, you'll need add one. limitation of standard.
- Get link
- X
- Other Apps
Comments
Post a Comment