A List Apart: for 
people who make websites

Using XML.

Sample Relax NG File

If you want to try this file to validate the sample XML document, copy and paste this code into a file called nutrition.rng in the nutrition directory. (See Linux and Windows setup instructions.)


<grammar datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
  xmlns="http://relaxng.org/ns/structure/1.0">

<start>
<element name="nutrition">
	<ref name="daily-values-defn"https://alistapart.com/>
	<oneOrMore>
		<ref name="food-defn"https://alistapart.com/>
	</oneOrMore>
</element>
</start>

<define name="daily-values-defn">
	<element name="daily-values">
		<element name="total-fat">
			<ref name="decimal-units-defn"https://alistapart.com/>
		</element>
		<element name="saturated-fat">
			<ref name="decimal-units-defn"https://alistapart.com/>
		</element>
		<element name="cholesterol">
			<ref name="decimal-units-defn"https://alistapart.com/>
		</element>
		<element name="sodium">
			<ref name="decimal-units-defn"https://alistapart.com/>
		</element>
		<element name="carb">
			<ref name="decimal-units-defn"https://alistapart.com/>
		</element>
		<element name="fiber">
			<ref name="decimal-units-defn"https://alistapart.com/>
		</element>
		<element name="protein">
			<ref name="decimal-units-defn"https://alistapart.com/>
		</element>
	</element>
</define>

<define name="decimal-units-defn">
	<data type="decimal"https://alistapart.com/>
	<attribute name="units"><text/></attribute>
</define>

<define name="food-defn">
	<element name="food">
		<element name="name"><text/></element>
		<element name="mfr"><text/></element>
		<element name="serving">
			<ref name="decimal-units-defn"https://alistapart.com/>
		</element>
		<element name="calories">
			<empty/>
			<attribute name="total"><data type="decimal"https://alistapart.com/></attribute>
			<attribute name="fat"><data type="decimal"https://alistapart.com/></attribute>
		</element>
		<element name="total-fat"><data type="decimal"https://alistapart.com/></element>
		<element name="saturated-fat"><data type="decimal"https://alistapart.com/></element>
		<element name="cholesterol"><data type="decimal"https://alistapart.com/></element>
		<element name="sodium"><data type="decimal"https://alistapart.com/></element>
		<element name="carb"><data type="decimal"https://alistapart.com/></element>
		<element name="fiber"><data type="decimal"https://alistapart.com/></element>
		<element name="protein"><data type="decimal"https://alistapart.com/></element>
		<element name="vitamins">
			<element name="a"><ref name="percent-defn"https://alistapart.com/></element>
			<element name="c"><ref name="percent-defn"https://alistapart.com/></element>
		</element>
		<element name="minerals">
			<element name="ca"><ref name="percent-defn"https://alistapart.com/></element>
			<element name="fe"><ref name="percent-defn"https://alistapart.com/></element>
		</element>
	</element>
</define>

<define name="percent-defn">
	<data type="decimal">
		<param name="minInclusive">0</param>
		<param name="maxInclusive">100</param>
	</data>
</define>
</grammar>