question about attributes

R

Russ

Suppose I want to specify a position on the earth. I could define an
element such as

<position coords="WGS84", lat="34.56223", lon="-121.8277"/>

where lat and lon are latitude and longitude in the WGS84 coordinate
system. Now suppose I also want to have the option of specifying the
position in another coordinate system, a locally level cartesian
system. I could define an element such as

<position coords="DFW", x="236.764", y="129.667"/>

where "DFW" is a predefined coordinate system.

I'm new to XML, and I'd like to know if it is possible to define a
schema that would enforce consistency for both options and perhaps
others as well. The trick here is that for the WGS84 coordinate
system, the coordinate attributes are lat and lon, whereas for the DFW
system they are x and y. Is it possible to write a schema to require
this? Thanks.
 
M

Martin Honnen

Russ said:
Suppose I want to specify a position on the earth. I could define an
element such as

<position coords="WGS84", lat="34.56223", lon="-121.8277"/>
^^^ ^^^
I am not sure why you have those commas, that is not well-formed.
where lat and lon are latitude and longitude in the WGS84 coordinate
system. Now suppose I also want to have the option of specifying the
position in another coordinate system, a locally level cartesian
system. I could define an element such as

<position coords="DFW", x="236.764", y="129.667"/>

where "DFW" is a predefined coordinate system.

I'm new to XML, and I'd like to know if it is possible to define a
schema that would enforce consistency for both options and perhaps
others as well. The trick here is that for the WGS84 coordinate
system, the coordinate attributes are lat and lon, whereas for the DFW
system they are x and y. Is it possible to write a schema to require
this?

Well, it is easy to define an element where the coords attribute is
mandatory and the other optional, e.g. a document alike

<?xml version="1.0" encoding="UTF-8"?>
<positions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test20040208Xsd.xml">
<position coords="WGS84" lat="34.56223" lon="-121.8277" />
<position coords="DFW" x="236.764" y="129.667" />
</positions>

then validates against

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="positions">
<xs:complexType>
<xs:sequence>
<xs:element ref="position" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:element name="position">
<xs:complexType>
<xs:attribute name="coords" type="coordsType" use="required" />
<xs:attribute name="lat" type="xs:double" />
<xs:attribute name="lon" type="xs:double" />
<xs:attribute name="x" type="xs:double" />
<xs:attribute name="y" type="xs:double" />
</xs:complexType>
</xs:element>

<xs:simpleType name="coordsType">
<xs:restriction base="xs:string">
<xs:enumeration value="WGS84" />
<xs:enumeration value="DFW" />
</xs:restriction>
</xs:simpleType>

</xs:schema>

That is not very restrictive however as you could also have an element
<position coords="DFW" lat="42" lon="42" />

You might want to choose a different structure than putting all
information in attributes.
 
P

Patrick TJ McPhee

% others as well. The trick here is that for the WGS84 coordinate
% system, the coordinate attributes are lat and lon, whereas for the DFW
% system they are x and y. Is it possible to write a schema to require
% this? Thanks.

Not with w3c schemas. You'd need to move the coordinate system to the
element level, something like

<position><wgs84 lat='34.56223' lon='-121.8277'/></position>
<position><dfw x='236.764' y='129.667'/></position>

I believe you can do it the way you want with schematron.
 

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

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top