XSD keyref problem

Posted on 2006-05-19
Medium Priority
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
  • 3
LVL 60

Expert Comment

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

The following schema does the key/keyref bit

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <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 (www.schematron.org)


LVL 60

Accepted Solution

Geert Bormans earned 1000 total points
ID: 16721790

this refernce is better : http://www.schematron.com/
LVL 11

Expert Comment

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


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
ID: 16782219

> 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



Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

839 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