Adding new attributes to schema

C

craig.wagner

I have a situation where I need to carry additional information in an
XML Schema. What I've found to appear to work is doing something like
the following:

<xs:schema xmlns:xs=" http://www.w3.org/2001/XMLSchema"
xmlns:xx="http://www.mycompany.com">
<xs:complexType name="SomeType">
<xs:sequence>
<xs:element name="AccountNumber" type="xs:int"
xx:errorAction="fail"/>
<xs:element name="AccountName" type="xs:string"
xx:errorAction="truncate" xx:length="25"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

This appears to work just fine. One thing I'm wondering though is how,
or if it's possible, to ensure that the xx: stuff that gets put in
falls within the rules we define. For example, the only valid
attributes are xx:errorAction and xx:length, xx:errorAction must be
part of a finite set (null, fail, truncate), and xx:length must be > 0.

We could live without doing this if it's going to be an excessive
amount of work, but if it's reasonable to do so it'll make the app more
bullet-proof.

Pointers or thoughts would be appreciated.
 
G

George Bina

You can use a Schematron schema to check that your XML Schema follows
the desired constraints. Here it is a working Schematron schema based
on your example:

<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns:sch="http://www.ascc.net/xml/schematron"
xmlns:xx="http://www.mycompany.com">
<sch:ns prefix="xx" uri="http://www.mycompany.com"/>
<sch:pattern name="Check xx: attributes">
<sch:rule context="*[@xx:*]">
<sch:assert test="not(@xx:*[local-name()!='errorAction' and
local-name()!='length'])"> The
possible xx attributes are errorAction and length.
</sch:assert>
</sch:rule>
</sch:pattern>
<sch:pattern name="Check xx:errorAction">
<sch:rule context="*[@xx:errorAction]">
<sch:assert
test="@xx:errorAction='null' or @xx:errorAction='fail' or
@xx:errorAction='truncate'">
Possible values for xx:errorAction are null, fail or truncate.
</sch:assert>
</sch:rule>
</sch:pattern>
<sch:pattern name="Check xx:length">
<sch:rule context="*[@xx:length]">
<sch:assert test="@xx:length>0"> The xx:length attribute must be
greater than zero.
</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>

Best Regards,
George
 
H

Henry S. Thompson

craig.wagner said:
I have a situation where I need to carry additional information in an
XML Schema. What I've found to appear to work is doing something like
the following:

<xs:schema xmlns:xs=" http://www.w3.org/2001/XMLSchema"
xmlns:xx="http://www.mycompany.com">
. . .
<xs:element name="AccountName" type="xs:string"
xx:errorAction="truncate" xx:length="25"/>
</xs:sequence>
</xs:complexType>
</xs:schema>

This appears to work just fine. One thing I'm wondering though is how,
or if it's possible, to ensure that the xx: stuff that gets put in
falls within the rules we define. For example, the only valid
attributes are xx:errorAction and xx:length, xx:errorAction must be
part of a finite set (null, fail, truncate), and xx:length must be > 0.

A schema document is an XML document like any other, and most schema
validators check the schema documents they are handed with the schema
for schema documents. This uses lax attribute wildcards in virtually
all its type definitions, which is why your schema document (above) is
OK. This means that to get validation of your added attributes, just
define a schema for your namespace with appropriate top-level
attribute declarations, e.g.

<xs:attribute name="errorAction">
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:enumeration value="null"/>
<xs:enumeration value="fail"/>
<xs:enumeration value="truncate"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>

and add

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mycompany.com [URI for doc. defining schema]"

to the original schema document.

ht
--
Henry S. Thompson, HCRC Language Technology Group, University of Edinburgh
Half-time member of W3C Team
2 Buccleuch Place, Edinburgh EH8 9LW, SCOTLAND -- (44) 131 650-4440
Fax: (44) 131 650-4587, e-mail: (e-mail address removed)
URL: http://www.ltg.ed.ac.uk/~ht/
[mail really from me _always_ has this .sig -- mail without it is forged spam]
 
C

craig.wagner

Thank you for your suggestions, but I'm still having some trouble.

I created a separate XSD as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.xx.com">
<xs:attribute name="errorAction">
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:enumeration value="null"/>
<xs:enumeration value="fail"/>
<xs:enumeration value="trunc"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:schema>

I then changed my original schema as follows:

<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xx="http://www.xx.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.xx.com C:\xx.XSD">

So far everything seems okay. But if I declare an element as follows:

xs:element name="AccountNumber" xx:errorAction="fail" xx:foo="bar">

It still tells me the document is valid (even though xx:foo shouldn't
be).

Still kind of a neophyte to this stuff, so I can't figure out what it
is I'm doing or not doing here.
 
H

Henry S. Thompson

craig.wagner said:
It still tells me the document is valid (even though xx:foo shouldn't
be).

'It'? What validator are you using, invoked in what way?

ht
--
Henry S. Thompson, HCRC Language Technology Group, University of Edinburgh
Half-time member of W3C Team
2 Buccleuch Place, Edinburgh EH8 9LW, SCOTLAND -- (44) 131 650-4440
Fax: (44) 131 650-4587, e-mail: (e-mail address removed)
URL: http://www.ltg.ed.ac.uk/~ht/
[mail really from me _always_ has this .sig -- mail without it is forged spam]
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top