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 8904867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:14:54+00:00 2026-06-15T02:14:54+00:00

I have an XML structure similar to this: <root> <hierarchy1> <foo id=foo1 /> <!–

  • 0

I have an XML structure similar to this:

<root>
   <hierarchy1>
       <foo id="foo1" /> <!-- id is of type id -->
       <bar id="bar1" /> <!-- id is of type id -->
   </hierarchy1>
   <hierarchy2>
       <foohandler ref="foo1" /> <!-- ref is of type idref -->
       <barhandler ref="bar1" /> <!-- ref is of type idref -->
   </hierarchy2>
</root>

I.e. I have different types of Objects that have ids and I have different types of target Objects that need to reference these IDs using IDREF.

Is there a way to ensure that the IDREF is of the correct expected type, i.e.

  • <foohandler ref="foo1" /> is valid whereas
  • <foohandler ref="bar1" /> isn’t?
  • 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-15T02:14:55+00:00Added an answer on June 15, 2026 at 2:14 am

    I’ve almost posted an answer which used pattern based restrictions (foo[0-9]+, etc.) to achieve what the OP asked. However before posting I found out about keys and keyrefs, which seemed to be the solution. So I came up with the following schema; but, at first it didn’t work because of a silly namespace declaration issue. I’m posting it primarily, because I didn’t know how to do this before either and I was going crazy, because I knew I’m almost there.

    <?xml version="1.0" encoding="UTF-8" ?>
    <xs:schema xmlns="http://stackoverflow.com"
               xmlns:so="http://stackoverflow.com"
               xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               targetNamespace="http://stackoverflow.com"
               attributeFormDefault="qualified"
               elementFormDefault="qualified">
      <xs:element name="root" type="rootType">
        <xs:key name="fooKey">
          <xs:selector xpath="so:hierarchy1/so:foo"/>
          <xs:field xpath="@so:key"/>
        </xs:key>
        <xs:key name="barKey">
          <xs:selector xpath="so:hierarchy1/so:bar"/>
          <xs:field xpath="@so:key"/>
        </xs:key>
        <xs:keyref name="fooKeyref" refer="fooKey">
          <xs:selector xpath="so:hierarchy2/so:foohandler"/>
          <xs:field xpath="@so:keyref"/>
        </xs:keyref>
        <xs:keyref name="barKeyref" refer="barKey">
          <xs:selector xpath="so:hierarchy2/so:barhandler"/>
          <xs:field xpath="@so:keyref"/>
        </xs:keyref>
      </xs:element>
    
      <xs:complexType name="rootType">
        <xs:sequence>
          <xs:element name="hierarchy1" type="hierarchy1Type"/>
          <xs:element name="hierarchy2" type="hierarchy2Type"/>
        </xs:sequence>
      </xs:complexType>
    
      <xs:complexType name="hierarchy1Type">
        <xs:sequence>
          <xs:element name="foo" type="fooType"/>
          <xs:element name="bar" type="barType"/>
        </xs:sequence>
      </xs:complexType>
    
      <xs:complexType name="hierarchy2Type">
        <xs:sequence>
          <xs:element name="foohandler" type="foohandlerType"/>
          <xs:element name="barhandler" type="barhandlerType"/>
        </xs:sequence>
      </xs:complexType>
    
      <xs:complexType name="fooType">
        <xs:attribute name="key" type="xs:integer"/>
      </xs:complexType>
    
      <xs:complexType name="barType">
        <xs:attribute name="key" type="xs:integer"/>
      </xs:complexType>
    
      <xs:complexType name="foohandlerType">
        <xs:attribute name="keyref" type="xs:integer"/>
      </xs:complexType>
    
      <xs:complexType name="barhandlerType">
        <xs:attribute name="keyref" type="xs:integer"/>
      </xs:complexType>
    </xs:schema>
    

    The schema above validates the XML document below.

    <?xml version="1.0" encoding="UTF-8"?>
    <so:root xmlns:so="http://stackoverflow.com"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://stackoverflow.com a.xsd">
      <so:hierarchy1>
        <so:foo so:key="1"/>
        <so:bar so:key="2"/>
      </so:hierarchy1>
      <so:hierarchy2>
        <so:foohandler so:keyref="1"/>
        <so:barhandler so:keyref="2"/>
      </so:hierarchy2>
    </so:root>
    

    If <so:barhandler so:keyref="2"/> is changed to <so:barhandler so:keyref="1"/> or <so:barhandler so:keyref="3"/> the document becomes invalid.

    The catch here is that without specifying the namespace xmlns:so="http://stackoverflow.com" and using this same namepsace in the xpath attribute of xs:selector and xs:field some parsers won’t care about the integrity of the document.

    Eclipse’s XML parser/validator and this online tool didn’t give a crap about my key, keyref declarations until I specified which namespace should the XPath expressions refer.

    The bottom lime could be use namespaces everyhwere?

    This answer lead me to my relevation.

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

Sidebar

Related Questions

I have a source document with XML structure similar to this: <FOO> <BAR>x</BAR> <BAR>y</BAR>
I have a XML with a structure similar to this: <category> <subCategoryList> <category> </category>
Here I had very similar xml structure, but now I have this: <Fields> <Company>My
I have a piece of XML that is structured similar to this: <root> <score
I have a XML file with a structure similar to this: <egh_eval> <eval_set> <eval_id>FLOAT</eval_id>
I have an XML structure similar to this: <Header> <ElementA> <ElementB> <ElementC/> <ElementC/> </ElementB>
I have xml structure like this: <Group id=2 name=Third parentid=0 /> <Group id=6 name=Five
I have an XML structure that looks like: <Succeeded p1:type=Edm.Boolean xmlns:p1=http://schemas.microsoft.com/ado/2007/08/dataservices/metadata xmlns=http://schemas.microsoft.com/ado/2007/08/dataservices>false</Succeeded> Since I
Hi I have big xml file which has a structure similar to the one
I have three xml files of similar structure and I would like to use

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.