ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 9 / 9 << | Prev | Next |
Contents > Simple tasks > Element contains text only and one attribute

Element contains text only and one attribute

  1. XML Schema
  2. Relax NG
XML Schema keys: attribute
Relax NG keys: attribute, text

1. XML Schema

This should be a simple task - we want the element "root" to contain text only and have just one attribute, named "xx".

Valid document

<f:root xsi:schemaLocation="http://foo correct_0.xsd" xx="1" xmlns:f="http://foo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" > qwerty uiop </f:root>

Correct XML Schema (correct_0.xsd)

<xsd:schema targetNamespace="http://foo" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:foo="http://foo" >

  <xsd:element name="root">
        <xsd:extension base="xsd:string">
          <xsd:attribute name="xx" type="xsd:string" use="required"/>

2. Relax NG

Valid document

<f:root xx="1" xmlns:f="http://foo" > qwerty uiop </f:root>

Correct Relax NG schema (correctRelax_0.rng)

<grammar ns="http://foo" xmlns="http://relaxng.org/ns/structure/1.0" >

        <name ns="">xx</name>