Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 391127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:01:53+00:00 2026-05-12T16:01:53+00:00

I’m writing an XSD schema for a project I’m working on. The schema below

  • 0

I’m writing an XSD schema for a project I’m working on. The schema below is one I took from a microsoft example and modified slightly.

I am trying to use the key and keyref to declare a unique key for one set of items and then refer to that key in another section.

I couldn’t get it to work for a long time. I would write the schema and setup a test doc which should fail validation because of (1) duplicate keys and (2) refkeys that refer to non-existant keys but it kept passing.

After a bunch of tinkering and working off of an example, I got it to work. So I tried to narrow it down to what worked in the example but was causing it to not work in my original attempt.

I am validating using the .NET XmlDocument and XmlSchema. I’ll paste my test validation code at the bottom.

My question is, why does the key work if declared as it is below but not work if declared as it is in the comment?

XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      targetNamespace="namespace1"
      xmlns="namespace1"
      xmlns:r="namespace1"
      elementFormDefault="qualified">  

<xs:element name="root">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="A" type="r:AType" maxOccurs="unbounded">
        <xs:keyref name="dummy" refer="r:pNumKey">
          <xs:selector xpath="part"/>
          <xs:field xpath="@ref-number"/>
        </xs:keyref>
      </xs:element>
      <xs:element name="B" type="r:BType"/>
    </xs:sequence>
  </xs:complexType>

  <!-- This works. -->
  <xs:key name="pNumKey">
    <xs:selector xpath="r:B/r:part"/>
    <xs:field xpath="@key-number"/>
  </xs:key>
  <!--
  This doesn't work.

  <xs:key name="pNumKey">
    <xs:selector xpath="B/part"/>
    <xs:field xpath="@key-number"/>
  </xs:key>
  -->
</xs:element>

<xs:complexType name="AType">
  <xs:sequence>
    <xs:element name="part" maxOccurs="unbounded">
      <xs:complexType>
        <xs:simpleContent>
          <xs:extension base="xs:string">
            <xs:attribute name="ref-number" type="xs:integer"/>
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="BType">
  <xs:sequence>
    <xs:element name="part" maxOccurs="unbounded">
      <xs:complexType>
        <xs:attribute name="key-number" type="xs:integer"/>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>
</xs:schema>

Vaidation Code:

    private static void ValidateXml(string root, string xsdFileName, string xmlFileName)
    {
        ValidationEventHandler veh = new ValidationEventHandler(Program_ValidationEventHandler);
        XmlSchema schema = XmlSchema.Read(new XmlTextReader(root + xsdFileName), veh);

        XmlDocument xdoc = new XmlDocument();
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.Schemas.Add(schema);

        settings.ValidationType = ValidationType.Schema;

        settings.ValidationEventHandler +=
            new ValidationEventHandler(Program_ValidationEventHandler);

        XmlReader reader = XmlReader.Create(root + xmlFileName, settings);

        xdoc.Load(reader);

        Console.WriteLine(xdoc.SchemaInfo.Validity);
    }

    private static void Program_ValidationEventHandler(object sender, ValidationEventArgs e)
    {
        Console.WriteLine(string.Format("-Message:{0}", e.Message));
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:01:53+00:00Added an answer on May 12, 2026 at 4:01 pm

    I’m not 100 % sure, but my pretty confident guess is that the XPath expression in the selector element does not care about the target namespace, just the namespaces you’ve declared with xmlns attributes. So, since you haven’t declared a default namespace, it tries to ensure elements part in no namespace are unique. And since the part elements in your document seem to be in the namespace1 namespace, you don’t have any part elements in no namespace, so this uniqueness constraint succeeds.

    You can verify my guess by adding to your schema element the attribute xmlns="namespace1". (Since all your elements in the schema document have prefixes, this is the only thing you need to do.) If your commented-out key element works then, this would seem to be the correct explanation.

    EDIT: Okay, I found the answer. Turns out that, in XPath 1.0, when you have a non-prefixed name, it is automatically interpreted as being in no namespace. There is no way to set the default namespace in XPath 1.0, so you always need to prefix all names in XPath expressions when they are in some namespace. So no matter whether you’ve declared a default namespace in your schema document, the commented out key definition is trying to match names in no namespace, which doesn’t match your document.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 248k
  • Answers 248k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the past I've used local domain sockets for that… May 13, 2026 at 8:58 am
  • Editorial Team
    Editorial Team added an answer def b(): ID = [0, 1, 2] ID2 = ('ID=%d.%d.%d'… May 13, 2026 at 8:58 am
  • Editorial Team
    Editorial Team added an answer Hmm, it works well for me: HTML code: <textarea id="reply_form"… May 13, 2026 at 8:58 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.