Page 1 of 1

default value of attribute depends on attribute of parent

Posted: Thu Mar 16, 2006 3:30 pm
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.

Posted: Thu Mar 16, 2006 5:38 pm
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

Posted: Fri Mar 17, 2006 3:28 pm
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

Posted: Fri Mar 17, 2006 3:46 pm
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

Posted: Fri Mar 17, 2006 4:05 pm
by alk
Hi George,

thanks again for putting quite some effort into your answer.

Best Regards
- Alex

but how can I activate it?

Posted: Tue May 16, 2006 7:26 pm
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

Posted: Tue May 16, 2006 9:49 pm
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

Posted: Thu May 18, 2006 9:02 am
by muewi
Hi George,

thanks again - it is working now!

Regards,
Frank