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

  • SEARCH
  • Home
  • 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 7864971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:56:55+00:00 2026-06-02T23:56:55+00:00

I have an XML document structured as Q&A which follows the following format (edited

  • 0

I have an XML document structured as Q&A which follows the following format (edited for clarity):

<question>
    <answer id="1">
        <question>
            <answer id="1"/>
            <answer id="2"/>
            <answer id="3"/>
        </question>
    </answer>
    <answer id="2">
        <question>
            <answer id="1"/>
            <answer id="2"/>
        </question>
    </answer>
</question>

My XSD looks like this:

<xs:element name="question">
     <xs:complexType>
        <xs:sequence>
            <xs:element name="answer" type="answerType" minOccurs="2" maxOccurs="unbounded">
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:unique name="AnswerIdUnique">
        <xs:selector xpath="./*" />
        <xs:field xpath="@id" />
    </xs:unique>
</xs:element>

<xs:complexType name="answerType">
    <xs:sequence>
        <xs:element ref="question" minOccurs="0" maxOccurs="1" />
    </xs:sequence>
    <xs:attribute name="id" type="xs:token" use="required" />
</xs:complexType>

There is, of course, more to it than what you see above but this illustrates my problem.
I need for the id attribute on answer elements to be unique among siblings. The XSD defined above enforces uniqueness of id attributes among sibling elements, but it does not discriminate on element type. I’ve tried a variety of selectors and fields in the unique constraint, but have not found a combination that works.
Any suggestions?

  • 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-06-02T23:56:56+00:00Added an answer on June 2, 2026 at 11:56 pm

    Just change the selector to <xs:selector xpath="answer"/> and you’ll be fine. In general it is good to avoid XPaths like .//*, if only for performance reasons.

    This is the XML Schema for the XML sample you’ve provided that I think is working the way you want:

    <?xml version="1.0" encoding="utf-8" ?>
    <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="question" type="questionType">
            <xs:unique name="AnswerIdUnique">
                <xs:selector xpath="answer"/>
                <xs:field xpath="@id"/>
            </xs:unique>
        </xs:element>
        <xs:complexType name="questionType">
            <xs:sequence>
                <xs:element name="answer" type="answerType" minOccurs="2" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
        <xs:complexType name="answerType">
            <xs:sequence>
                <xs:element ref="question" minOccurs="0" maxOccurs="1"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:token" use="required"/>
        </xs:complexType>
    </xs:schema>
    

    Your posted XML validates fine with the above; duplicating any sibling answer’s id yields a validation error. For example, the following XML:

    <question> 
        <answer id="1"> 
            <question> 
                <answer id="1"/> 
                <answer id="2"/> 
                <answer id="1"/> 
            </question> 
        </answer> 
        <answer id="1"> 
            <question> 
                <answer id="1"/> 
                <answer id="2"/> 
            </question> 
        </answer> 
    </question> 
    

    When validated (in QTAssistant, should be similar to the message in Visual Studio since it is based on the same technology), these are the errors:

    Error occurred while loading [], line 6 position 5
    There is a duplicate key sequence '1' for the 'AnswerIdUnique' key or unique identity constraint.
    Error occurred while loading [], line 9 position 3
    There is a duplicate key sequence '1' for the 'AnswerIdUnique' key or unique identity constraint.
    Document1.xml is invalid.
    

    Below is a screenshot from Visual Studio 2010 showing the above XML validation against the XSD I’ve posted; while the problems are inadvertently reported as warnings, they are, nonetheless, reported.

    VS2010 showing unique constraint errors

    I’ve randomly picked an online validator (http://xsdvalidation.utilities-online.info/) and validated the same XML and XSD I’ve posted; the error is reported as:

    org.xml.sax.SAXParseException: Duplicate unique value [1] declared for identity constraint of element "question".org.xml.sax.SAXParseException: Duplicate unique value [1] declared for identity constraint of element "question".

    One thing you have to pay attention to is when you have a target namespace for your XSD; in that case, it is needed to define an alias for all of the involved namespaces, and use them in your selectors.

    UPDATE: And the XSD with namespaces:

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://localhost" xmlns="http://localhost" targetNamespace="http://localhost" elementFormDefault="qualified" attributeFormDefault="unqualified">
        <xs:element name="question" type="questionType">
            <xs:unique name="AnswerIdUnique">
                <xs:selector xpath="tns:answer"/>
                <xs:field xpath="@id"/>
            </xs:unique>
        </xs:element>
        <xs:complexType name="questionType">
            <xs:sequence>
                <xs:element name="answer" type="answerType" minOccurs="2" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
        <xs:complexType name="answerType">
            <xs:sequence>
                <xs:element ref="question" minOccurs="0" maxOccurs="1"/>
            </xs:sequence>
            <xs:attribute name="id" type="xs:token" use="required"/>
        </xs:complexType>
    </xs:schema>
    

    Please notice the introduction of the tns prefix and the use of it in the selector.

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

Sidebar

Related Questions

I have an XML file which is structured as follows: <row> <store>place1</store> <location>location1</location> </row>
I have an XML document structured as follows <items> <item> <name>item1</name> <attributes>a,b,c,d</attributes> </item> <item>
I have an xml sitemap structured like a document tree, such that it looks
I have to following XML document structure: <option_set id=1> <option>Yes</option> <option>No</option> <option>Maybe</option> </option_set> <question
I have an xml document that looks like this <?xml version=1.0?> <XML> <VIDEO> <WIDTH>800</WIDTH>
I have a reasonably complex XML document which I want to flatten down to
When designing a specialized structured-data document format (perhaps upon XML): part of the requirements
i have an xml document structured like this: <NewDataSet> <videos> <video> <name>name of video</name>
I have an XML document with following structure: root element DataModel can have children
I have an XML document with (basically) looks like this: ... <param> <key>age</key> <value>10</value>

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.