Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

xml - how to create primary key with xsd and how to use him

I want to know how to create a primary key and use it here is my xsd code you can notice that I created two primary keys and a referential key

<?xml version="1.0" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <!-- car primary key -->
            <xsd:key name="carKey">
                <xsd:selector xpath="car"/>
                <xsd:field xpath="codeV"/>
            </xsd:key>
    <!-- person primary key -->
            <xsd:key name="personKey">
                <xsd:selector xpath="person"/>
                <xsd:field xpath="codeP"/>
            </xsd:key>
     <!--  personne referential key -->
              <xsd:keyref name="codeOwner" refer="carKey">
                <xsd:selector xpath="car"/>
                <xsd:field xpath="codeP"/>
            </xsd:keyref>
    <xsd:element name="Parking">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="car" />
                <xsd:element name="person" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    
    <xsd:complexType name="car">
        <xsd:sequence>
            <xsd:element name="codeV" ref="car"/>
            <xsd:element name="model" type="xsd:string"/>
            <xsd:element name="mark" type="xsd:string"/>
            <xsd:element name="owner" ref="person"/>
        </xsd:sequence>
    </xsd:complexType>
    
    <xsd:complexType name="person">
        <xsd:sequence>
            <xsd:element name="codeP" ref="person"/>
            <xsd:element name="name" type="xsd:string"/>
            <xsd:element name="address" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

I want to make a car park with several cars each car has an owner for that we must use a referential constraints

my xml code

<?xml version="1.0"?>
<Parking xmlns="http://www.w3schools.com" xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="park.xsd"> 
    <car>
        <codeV> number </codeV>
        <mark> Toyota </mark>
        <model> corolla</model>
        <owner> fichale</owner>
    </car>
    <person>
        <codeP> fichale</codeP>
        <name> KEZIRE </name>
        <address> ESP </address>
    </person>
</Parking>

When I tried to run my code i got this error

S4s-elt-invalid-content.1: The Content Of 'schema' Is Invalid. Element 'key' Is Invalid, Misplaced, Or Occurs Too Often.

and I don't know why.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...