XSD keyref problem

Posted on 2006-05-19
Last Modified: 2013-11-19
I want to write an XSD, which accepts the following XML File:

<type name="type1"/>
<type name="type2"/>
<item name="n1" type="type1"/>
<item name="n2"  type="standardtype1"/>
<item name="n3"  type="standardtype1"/>
<item name="n4"  type="type1"/>
<item name="n5"  type="type2"/>
<item name="n6"  type="standardtype2"/>

The problem that I have is the attribute "type" of the item element. Acceptable values for this attribute are the names of the defined types. Additionally, it should accept a list of standard types (lets them call standardtype1 and standardtype2) which do not occure in the XML File.

How can I define such an attribute in an XSD?
Question by:maverick65
    LVL 60

    Expert Comment

    by:Geert Bormans
    Hi maverick65,
    > <type name="type2"/>

    The following schema does the key/keyref bit

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="">
        <xs:element name="root">
                    <xs:element ref="types"/>
                    <xs:element ref="items"/>
            <xs:key name="TypeName">
                <xs:selector xpath="types/type"/>
                <xs:field xpath="@name"/>
            <xs:keyref refer="TypeName" name="ItemName">
                <xs:selector xpath="items/item"/>
                <xs:field xpath="@type"/>
        <xs:element name="types">
                <xs:sequence maxOccurs="unbounded">
                    <xs:element ref="type"/>
        <xs:element name="type">
                <xs:attribute name="name" use="required"/>

        <xs:element name="items">
                <xs:sequence maxOccurs="unbounded">
                    <xs:element ref="item"/>

        <xs:element name="item">
                <xs:attribute name="name" use="required"/>
                <xs:attribute name="type" use="required"/>

    >Additionally, it should accept a list of standard types (lets them call standardtype1 and standardtype2) which do not occure in the XML File
    I am afraid this cannot be done using the key/keyref mechanisms in W3C schema
    you either force the attribute to be a referer to the attribute values in your typelist
    or you leave it open,
    a combination sounds kinda weird

    Can you not force these two extra type dfinitions in the document before validation
    (in some sort of transformation preprocess)
    that would be handier I guess

    You can also try to use schematron instead...
    you would be able to express this requirement in schematron (


    LVL 60

    Accepted Solution


    this refernce is better :
    LVL 11

    Expert Comment

    I don't think it can be done. We can capture the user-defined types with ID, as in

    <!ELEMENT type EMPTY>
    <!ATTLIST type name ID #REQUIRED>

    And we could enumerate the built-in types, but this gives us two different kinds of information - and I can't find a way for one attribute to have two different types.

    I'm hoping to be corrected...

    LVL 60

    Expert Comment

    by:Geert Bormans

    > I don't think it can be done
    nope, not with schema, certainly not with DTD
    likely with schematron,
    but in stead of schematron you could build a validation layer using XSLT, that does what is needed

    > We can capture the user-defined types with ID

    well, ID/IDREF is the DTD way of doing KEY/KEYREF
    the later is more powerfull because it can eg. capture element content as the ID
    or can combine values as the "ID"
    none of this is neede though, so the ID/IDREF mechanism of W3C schema could be used

    I don't recommend using ID/IDREF in schema though,
    because its only purpose of existense is pleasing the DTD people (so some sort of backwards compatibility if you like)
    since this seems a brand new project and W3C schema was somehow mentioned as a requirement, forget about ID/IDREF

    > and I can't find a way for one attribute to have two different types

    you can't do what you are trying here



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    The viewer will learn how to dynamically set the form action using jQuery.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now