default value of attribute depends on attribute of parent

This should cover W3C XML Schema, Relax NG and DTD related problems.
Menno Pruijssers
Posts: 1
Joined: Thu Mar 16, 2006 3:25 pm

default value of attribute depends on attribute of parent

Post by Menno Pruijssers »

Ey Guys,

I a creating a schema for a xml file like this:

Code: Select all


<root package="com.test">
<class name="Class">
....
</class>

<class package="com.test2">
....
</class>
</root>
My schema looks like this:

Code: Select all


  <xsd:element name="root" type="RootType">
<xsd:key name="className">
<xsd:selector xpath=".//class"/>
<xsd:field xpath="@name"/>
<xsd:field xpath="@package"/>
</xsd:key>
</xsd:element>


<xsd:complexType name="RootType">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="1" type="ClassType" name="class"/>
</xsd:sequence>
<xsd:attribute name="package" use="optional" type="PackageNameType"/>
</xsd:complexType>
<xsd:complexType name="ClassType">
....
</xsd:complexType>

<xsd:simpleType name="PackageNameType">
<xsd:restriction base="strippedString">
<xsd:pattern value="([a-z][a-zA-Z0-9]*.)*"/>
</xsd:restriction>
</xsd:simpleType>
The 'package' attribute of the 'class'-element is optional. If not specified: the package attribute of the 'root'-element must be used. Is it possible to model this. It is needed foor the key.
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Post by sorin_ristache »

Hello,

XML Schema does not allow to specify that if an optional attribute is not specified for an element in an XML document that attribute takes the value of some attribute of other element. The possible values of an attribute, optional or not must be specified statically in the schema as string constants, not as values of an attribute from other element.

Regards,
Sorin
alk
Posts: 32
Joined: Wed Jan 25, 2006 5:43 pm

Post by alk »

Hi,

in addition to this I'm having another question:

I have an element that shall express a value either as a certain date or a date interval, e.g <Arrival Date="" From="" Until=""/>, so either Date or From and Until have to be set, but not all of them. Is there a possiblity to create a schema definition for this matter?

Kind Regards
- Alex
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi Alex,

No, you cannot do this in XML Schema. You can do that however either with XML Schema with embedded Schematron rules or with a Relax NG schema, all being supported by oXygen.
Here it is a sample XML Schema with embedded Schematron rules:

Code: Select all


<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="Arrival">
<xs:annotation>
<xs:appinfo>
<pattern xmlns="http://www.ascc.net/xml/schematron" name="Check attributes">
<rule context="Arrival">
<assert test="(@Date and not(@From or @Until))or (not(@Date) and @From and @Until)">
Either specify Date or both From and Until attributes.</assert>
</rule>
</pattern>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:attribute name="Date"/>
<xs:attribute name="From"/>
<xs:attribute name="Until"/>
</xs:complexType>
</xs:element>
</xs:schema>
Here it is a sample Relax NG Schema
in compact syntax

Code: Select all


default namespace = ""
start =
element Arrival {
attribute Date { text }
| (attribute From { text },
attribute Until { text })
}
in XML syntax

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<grammar ns="" xmlns="http://relaxng.org/ns/structure/1.0">
<start>
<element name="Arrival">
<choice>
<attribute name="Date"/>
<group>
<attribute name="From"/>
<attribute name="Until"/>
</group>
</choice>
</element>
</start>
</grammar>
Best Regards,
George
alk
Posts: 32
Joined: Wed Jan 25, 2006 5:43 pm

Post by alk »

Hi George,

thanks again for putting quite some effort into your answer.

Best Regards
- Alex
muewi
Posts: 26
Joined: Wed Aug 24, 2005 10:47 am

but how can I activate it?

Post by muewi »

Hi,

I tried to apply this, but nothing happened. This is how I tried to build it in:

Code: Select all


<xs:element name="sp">
<xs:annotation>
<xs:documentation>Syntactic particle. This includes rnam dbye (case-marking particles), phrad rang dbang can
(independent particles) and phrad gzhan dbang can (dependent particles)</xs:documentation>
<xs:appinfo>
<pattern xmlns="http://www.ascc.net/xml/schematron" name="Check attributes">
<rule context="sp">
<assert test="@type">type nicht da </assert>
<report test="@type">type ist da</report>"
</rule>
</pattern>
</xs:appinfo>
</xs:annotation>
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="type">
....
...
By using assert and report on the same test, I wanted to make sure that I get a reaction, but nothing happens when validating my document. Do I have to turn something on before?

Regards,
Frank
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi Frank,

To have the Schematron validation and the XML Schema validation in a single action then you need to associate the schema both as an XML Schema and as a Schematron Schema in the document. For instance let's suppose you have the schema test.xsd associated with the xsi:noNamepsaceSchemaLocation attribute then you should add also a PI like

<?oxygen SCHSchema="test.xsd"?>

before the root element. This is added automatically by the Associate schema action if you select the Schematron page and select the same XML Schema. oXygen will automatically detect that it is an XML Schema and will try to extract the Schematron rules from that and use them for the Schematron validation.

Note however that if your element sp belongs to a namespace then you need to use a prefix to match that in the rule and declare that prefix like:

<sch:ns prefix="p" uri="yourNamespace"/>

and use p:sp instead of sp.

Best Regards,
George
muewi
Posts: 26
Joined: Wed Aug 24, 2005 10:47 am

Post by muewi »

Hi George,

thanks again - it is working now!

Regards,
Frank
Post Reply