XML Schema - Key constraint on two two elements












-1















My .xml file is something like this:



<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file.xsd">
<parent id="">
<child>
<part>A1</part>
</child>
</parent>
<another-parent name="AAA">
<part name="A1"/>
<part name="A2"/>
<part name="A3"/>
</another-parent>
<another-parent name="BBB">
<part name="A1"/>
</another-parent>
</root>


What I want is:





  1. name of <another-parent> must be unique. And it is ok.


  2. name of <part> must be unique within name of <another-parent> is placed. And it is ok.


  3. <part name="A1"> inside <child> must be a keyref of <another-parent>. And I can't do that.


For the first point, I used the following and it works properly.



<xsd:key name="anotherParentKey">
<xsd:selector xpath="another-parent"/>
<xsd:field xpath="@name"/>
</xsd:key>


For the second one I used this one in the element declaration:



<xsd:key name="partKey">
<xsd:selector xpath="part"/>
<xsd:field xpath="./@name"/>
</xsd:key>


It works now.



But for the third part, I tried to put this code in the (the first common ancestor) but it doesnt work:



<xsd:keyref name="roadSegmentRef" refer="roadSegmentKey">
<xsd:selector xpath="identifiedEntity/place/roadSegment" />
<xsd:field xpath="." />
</xsd:keyref>


But what I get is only:



cvc-identity-constraint.4.3: Key 'partRef' with value 'A1' not found for identity constraint of element 'parent'.   file.xml    /sheet/xsd  line 19 XML Problem









share|improve this question

























  • Please tell us what you tried, and how it failed. Otherwise the only thing we can do is to write a tutorial on key/unique/keyref, and there are plenty of those already. (StackOverflow posts should ask a question, not ask for people to write the code for you.)

    – Michael Kay
    Nov 13 '18 at 12:32













  • Okay, sorry. I edited my post above

    – Lala Cip
    Nov 13 '18 at 14:12
















-1















My .xml file is something like this:



<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file.xsd">
<parent id="">
<child>
<part>A1</part>
</child>
</parent>
<another-parent name="AAA">
<part name="A1"/>
<part name="A2"/>
<part name="A3"/>
</another-parent>
<another-parent name="BBB">
<part name="A1"/>
</another-parent>
</root>


What I want is:





  1. name of <another-parent> must be unique. And it is ok.


  2. name of <part> must be unique within name of <another-parent> is placed. And it is ok.


  3. <part name="A1"> inside <child> must be a keyref of <another-parent>. And I can't do that.


For the first point, I used the following and it works properly.



<xsd:key name="anotherParentKey">
<xsd:selector xpath="another-parent"/>
<xsd:field xpath="@name"/>
</xsd:key>


For the second one I used this one in the element declaration:



<xsd:key name="partKey">
<xsd:selector xpath="part"/>
<xsd:field xpath="./@name"/>
</xsd:key>


It works now.



But for the third part, I tried to put this code in the (the first common ancestor) but it doesnt work:



<xsd:keyref name="roadSegmentRef" refer="roadSegmentKey">
<xsd:selector xpath="identifiedEntity/place/roadSegment" />
<xsd:field xpath="." />
</xsd:keyref>


But what I get is only:



cvc-identity-constraint.4.3: Key 'partRef' with value 'A1' not found for identity constraint of element 'parent'.   file.xml    /sheet/xsd  line 19 XML Problem









share|improve this question

























  • Please tell us what you tried, and how it failed. Otherwise the only thing we can do is to write a tutorial on key/unique/keyref, and there are plenty of those already. (StackOverflow posts should ask a question, not ask for people to write the code for you.)

    – Michael Kay
    Nov 13 '18 at 12:32













  • Okay, sorry. I edited my post above

    – Lala Cip
    Nov 13 '18 at 14:12














-1












-1








-1








My .xml file is something like this:



<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file.xsd">
<parent id="">
<child>
<part>A1</part>
</child>
</parent>
<another-parent name="AAA">
<part name="A1"/>
<part name="A2"/>
<part name="A3"/>
</another-parent>
<another-parent name="BBB">
<part name="A1"/>
</another-parent>
</root>


What I want is:





  1. name of <another-parent> must be unique. And it is ok.


  2. name of <part> must be unique within name of <another-parent> is placed. And it is ok.


  3. <part name="A1"> inside <child> must be a keyref of <another-parent>. And I can't do that.


For the first point, I used the following and it works properly.



<xsd:key name="anotherParentKey">
<xsd:selector xpath="another-parent"/>
<xsd:field xpath="@name"/>
</xsd:key>


For the second one I used this one in the element declaration:



<xsd:key name="partKey">
<xsd:selector xpath="part"/>
<xsd:field xpath="./@name"/>
</xsd:key>


It works now.



But for the third part, I tried to put this code in the (the first common ancestor) but it doesnt work:



<xsd:keyref name="roadSegmentRef" refer="roadSegmentKey">
<xsd:selector xpath="identifiedEntity/place/roadSegment" />
<xsd:field xpath="." />
</xsd:keyref>


But what I get is only:



cvc-identity-constraint.4.3: Key 'partRef' with value 'A1' not found for identity constraint of element 'parent'.   file.xml    /sheet/xsd  line 19 XML Problem









share|improve this question
















My .xml file is something like this:



<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file.xsd">
<parent id="">
<child>
<part>A1</part>
</child>
</parent>
<another-parent name="AAA">
<part name="A1"/>
<part name="A2"/>
<part name="A3"/>
</another-parent>
<another-parent name="BBB">
<part name="A1"/>
</another-parent>
</root>


What I want is:





  1. name of <another-parent> must be unique. And it is ok.


  2. name of <part> must be unique within name of <another-parent> is placed. And it is ok.


  3. <part name="A1"> inside <child> must be a keyref of <another-parent>. And I can't do that.


For the first point, I used the following and it works properly.



<xsd:key name="anotherParentKey">
<xsd:selector xpath="another-parent"/>
<xsd:field xpath="@name"/>
</xsd:key>


For the second one I used this one in the element declaration:



<xsd:key name="partKey">
<xsd:selector xpath="part"/>
<xsd:field xpath="./@name"/>
</xsd:key>


It works now.



But for the third part, I tried to put this code in the (the first common ancestor) but it doesnt work:



<xsd:keyref name="roadSegmentRef" refer="roadSegmentKey">
<xsd:selector xpath="identifiedEntity/place/roadSegment" />
<xsd:field xpath="." />
</xsd:keyref>


But what I get is only:



cvc-identity-constraint.4.3: Key 'partRef' with value 'A1' not found for identity constraint of element 'parent'.   file.xml    /sheet/xsd  line 19 XML Problem






xml xsd schema xmlschemaset






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 15:34







Lala Cip

















asked Nov 13 '18 at 11:33









Lala CipLala Cip

135




135













  • Please tell us what you tried, and how it failed. Otherwise the only thing we can do is to write a tutorial on key/unique/keyref, and there are plenty of those already. (StackOverflow posts should ask a question, not ask for people to write the code for you.)

    – Michael Kay
    Nov 13 '18 at 12:32













  • Okay, sorry. I edited my post above

    – Lala Cip
    Nov 13 '18 at 14:12



















  • Please tell us what you tried, and how it failed. Otherwise the only thing we can do is to write a tutorial on key/unique/keyref, and there are plenty of those already. (StackOverflow posts should ask a question, not ask for people to write the code for you.)

    – Michael Kay
    Nov 13 '18 at 12:32













  • Okay, sorry. I edited my post above

    – Lala Cip
    Nov 13 '18 at 14:12

















Please tell us what you tried, and how it failed. Otherwise the only thing we can do is to write a tutorial on key/unique/keyref, and there are plenty of those already. (StackOverflow posts should ask a question, not ask for people to write the code for you.)

– Michael Kay
Nov 13 '18 at 12:32







Please tell us what you tried, and how it failed. Otherwise the only thing we can do is to write a tutorial on key/unique/keyref, and there are plenty of those already. (StackOverflow posts should ask a question, not ask for people to write the code for you.)

– Michael Kay
Nov 13 '18 at 12:32















Okay, sorry. I edited my post above

– Lala Cip
Nov 13 '18 at 14:12





Okay, sorry. I edited my post above

– Lala Cip
Nov 13 '18 at 14:12












1 Answer
1






active

oldest

votes


















0














You haven't shown us WHERE you put these declarations, and my guess is that you put them in the wrong place.



If you want every X within some Y to have a unique value for Z, then your key/unique declarations need to go in the element declaration for Y; the selector needs to select X starting from Y, and the fields need to select Z starting from X.






share|improve this answer
























  • I solved for the 1) and 2). But the 3) doesn't work. I edited my message with changes and added where I put them.

    – Lala Cip
    Nov 14 '18 at 15:35











  • I'm not sure precisely what your schema now looks like but I suspect that the key and keyref are not in the same element declaration. That's allowed, but the rules are incredibly complicated and it's a situation I prefer to avoid at all costs. I know it would take me half an hour to investigate, so sorry, I don't have the time.

    – Michael Kay
    Nov 15 '18 at 9:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280128%2fxml-schema-key-constraint-on-two-two-elements%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You haven't shown us WHERE you put these declarations, and my guess is that you put them in the wrong place.



If you want every X within some Y to have a unique value for Z, then your key/unique declarations need to go in the element declaration for Y; the selector needs to select X starting from Y, and the fields need to select Z starting from X.






share|improve this answer
























  • I solved for the 1) and 2). But the 3) doesn't work. I edited my message with changes and added where I put them.

    – Lala Cip
    Nov 14 '18 at 15:35











  • I'm not sure precisely what your schema now looks like but I suspect that the key and keyref are not in the same element declaration. That's allowed, but the rules are incredibly complicated and it's a situation I prefer to avoid at all costs. I know it would take me half an hour to investigate, so sorry, I don't have the time.

    – Michael Kay
    Nov 15 '18 at 9:26
















0














You haven't shown us WHERE you put these declarations, and my guess is that you put them in the wrong place.



If you want every X within some Y to have a unique value for Z, then your key/unique declarations need to go in the element declaration for Y; the selector needs to select X starting from Y, and the fields need to select Z starting from X.






share|improve this answer
























  • I solved for the 1) and 2). But the 3) doesn't work. I edited my message with changes and added where I put them.

    – Lala Cip
    Nov 14 '18 at 15:35











  • I'm not sure precisely what your schema now looks like but I suspect that the key and keyref are not in the same element declaration. That's allowed, but the rules are incredibly complicated and it's a situation I prefer to avoid at all costs. I know it would take me half an hour to investigate, so sorry, I don't have the time.

    – Michael Kay
    Nov 15 '18 at 9:26














0












0








0







You haven't shown us WHERE you put these declarations, and my guess is that you put them in the wrong place.



If you want every X within some Y to have a unique value for Z, then your key/unique declarations need to go in the element declaration for Y; the selector needs to select X starting from Y, and the fields need to select Z starting from X.






share|improve this answer













You haven't shown us WHERE you put these declarations, and my guess is that you put them in the wrong place.



If you want every X within some Y to have a unique value for Z, then your key/unique declarations need to go in the element declaration for Y; the selector needs to select X starting from Y, and the fields need to select Z starting from X.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 17:49









Michael KayMichael Kay

109k660114




109k660114













  • I solved for the 1) and 2). But the 3) doesn't work. I edited my message with changes and added where I put them.

    – Lala Cip
    Nov 14 '18 at 15:35











  • I'm not sure precisely what your schema now looks like but I suspect that the key and keyref are not in the same element declaration. That's allowed, but the rules are incredibly complicated and it's a situation I prefer to avoid at all costs. I know it would take me half an hour to investigate, so sorry, I don't have the time.

    – Michael Kay
    Nov 15 '18 at 9:26



















  • I solved for the 1) and 2). But the 3) doesn't work. I edited my message with changes and added where I put them.

    – Lala Cip
    Nov 14 '18 at 15:35











  • I'm not sure precisely what your schema now looks like but I suspect that the key and keyref are not in the same element declaration. That's allowed, but the rules are incredibly complicated and it's a situation I prefer to avoid at all costs. I know it would take me half an hour to investigate, so sorry, I don't have the time.

    – Michael Kay
    Nov 15 '18 at 9:26

















I solved for the 1) and 2). But the 3) doesn't work. I edited my message with changes and added where I put them.

– Lala Cip
Nov 14 '18 at 15:35





I solved for the 1) and 2). But the 3) doesn't work. I edited my message with changes and added where I put them.

– Lala Cip
Nov 14 '18 at 15:35













I'm not sure precisely what your schema now looks like but I suspect that the key and keyref are not in the same element declaration. That's allowed, but the rules are incredibly complicated and it's a situation I prefer to avoid at all costs. I know it would take me half an hour to investigate, so sorry, I don't have the time.

– Michael Kay
Nov 15 '18 at 9:26





I'm not sure precisely what your schema now looks like but I suspect that the key and keyref are not in the same element declaration. That's allowed, but the rules are incredibly complicated and it's a situation I prefer to avoid at all costs. I know it would take me half an hour to investigate, so sorry, I don't have the time.

– Michael Kay
Nov 15 '18 at 9:26


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280128%2fxml-schema-key-constraint-on-two-two-elements%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python