define static content in XML-Schema / Relax NG

M

mhuhn.de

I am using XML Schema for quite a while but haven't written a single
line of Relax NG yet.

For what I know, I cannot define static content within an XML Schema.
For example, I have the following structure:

<colors>
<color>green</color>
<color>red</color>
</colors>

With XML-Schema, I can say colors should have to elements of type
color, and color can contain "green" or "red". But I cannot say that
the content has to be exactly as posted above. Is this correct?

Is there a way to do this in Relax NG?

I need it for more levels than just one. I want to be able to define a
whole XML-Document within the "Schema", so only a document with exactly
that content will be valid.
 
S

Stan Kitsis [MSFT]

If you don't care about the order in which colors appear, you can do this in
xml schema. Depending on how complex your schema is, you might be able to
to something like this for more than just one level.

<?xml version="1.0" encoding="utf-8" ?>

<xs:schema targetNamespace="foo"

elementFormDefault="qualified"

xmlns="foo"

xmlns:foo="foo"

xmlns:xs="http://www.w3.org/2001/XMLSchema">



<xs:simpleType name="colorType">

<xs:restriction base="xs:string">

<xs:enumeration value="green"/>

<xs:enumeration value="red"/>

</xs:restriction>

</xs:simpleType>

<xs:element name="colors">

<xs:complexType>

<xs:sequence>

<xs:element name="color" type="colorType" minOccurs="2"
maxOccurs="2"/>

</xs:sequence>

</xs:complexType>

<xs:unique name="uniqueColors">

<xs:selector xpath="foo:color"/>

<xs:field xpath="."/>

</xs:unique>

</xs:element>

</xs:schema>


--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
M

mhuhn.de

Thanks for your answer but the order does play an important role in
this project. I already found out that with Relax NG it is possible to
define the order and therefore static content. Even though it is also
quite "talkative" so you cannot just write how it has to look.
 
S

Steve Jorgensen

I don't get it. If the exact content is predetermined, what information does
the document convey?

If a large section of a document must be boilerplate for content from a
specific source, why not make a schema and documents such that the boilerplate
content is excluded, then use XSLT to insert the boilerplate content after the
initial validation is done?

Finally, if inserting the content fixed later is not an option, you could use
a combination of XML Schema and Schematron since Schematron makes it easy to
validate data content. In the case below, you just first assert that, in the
context of "/colors", "color[1]='green' and color[2]='red'"
 
S

Steve Jorgensen

I don't get it. If the exact content is predetermined, what information does
the document convey?

If a large section of a document must be boilerplate for content from a
specific source, why not make a schema and documents such that the boilerplate
content is excluded, then use XSLT to insert the boilerplate content after the
initial validation is done?

Finally, if inserting the content fixed later is not an option, you could use
a combination of XML Schema and Schematron since Schematron makes it easy to
validate data content. In the case below, you just first assert that, in the
context of "/colors", "color[1]='green' and color[2]='red'"
 
M

mhuhn.de

It is (normally) not the whole document that is predetermined but I
want the possibility to predetermine large parts of a document.
Furthermore, I want to use the same mechanism (i.e. W3C XML Schema) to
validate. In addition to validation I want to use this schema (or
better call it template) to generate a valid XML file without further
information.
I use a modified version of W3C Schema now, i have an element called
"template" where I can predetermine fixed parts and also include
dynamic elements again. Looks like that:
....
<xs:complexType name="colorsType">
<xc:template>
<color>red</color>
<color>blue</color>
<color xs:type="colorType" xs:minOccurs="0" xs:maxOccurs="4" />
</xc:template>
</xs:complexType>
....

This is ok for my purpose and it works as an "XML constructor" as I
provide initial values for regular pattern values for example. But I
have to write my own validator which is quite a lot of work. You
schematron suggestion is nice for validation but doesn't work for
creation. I need it all in one file.
 
S

Steve Jorgensen

It is (normally) not the whole document that is predetermined but I
want the possibility to predetermine large parts of a document.
Furthermore, I want to use the same mechanism (i.e. W3C XML Schema) to
validate. In addition to validation I want to use this schema (or
better call it template) to generate a valid XML file without further
information.
I use a modified version of W3C Schema now, i have an element called
"template" where I can predetermine fixed parts and also include
dynamic elements again. Looks like that:
...
<xs:complexType name="colorsType">
<xc:template>
<color>red</color>
<color>blue</color>
<color xs:type="colorType" xs:minOccurs="0" xs:maxOccurs="4" />
</xc:template>
</xs:complexType>
...

This is ok for my purpose and it works as an "XML constructor" as I
provide initial values for regular pattern values for example. But I
have to write my own validator which is quite a lot of work. You
schematron suggestion is nice for validation but doesn't work for
creation. I need it all in one file.

No XML tools I know of were really built to do what you're describing in
regard to performing both generation and validation. If you can make it work
at all, it's going to be a terrible kludge.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top