473,395 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Adding new attributes to schema

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.

Feb 7 '06 #1
4 1909
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
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Feb 7 '06 #2
craig.wagner writes:
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: ht@inf.ed.ac.uk
URL: http://www.ltg.ed.ac.uk/~ht/
[mail really from me _always_ has this .sig -- mail without it is forged spam]
Feb 7 '06 #3
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.

Feb 8 '06 #4
craig.wagner writes:
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: ht@inf.ed.ac.uk
URL: http://www.ltg.ed.ac.uk/~ht/
[mail really from me _always_ has this .sig -- mail without it is forged spam]
Feb 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John L. Clark | last post by:
I am curious as to the rationale, and effect, of having default namespaces not applying (directly) to attributes (see http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute without a...
5
by: Ralf Wahner | last post by:
Dear Masters of XML As I'm new to XML Schema I dare to ask a possibly recurring question: Given an element <elem> with two attributes @a and @b. The attributes are bound by the condition, that...
2
by: Stewart Johnson | last post by:
Hey All - I'm stuck while trying to write a schema, hoping someone can provide me with a flash of inspiration. One of the elements I'm writing has two attributes, both of which are optional....
8
by: nicolas.sanguinetti | last post by:
Hi, I want to add custom attributes to my xhtml documents to use with my DOM scripts. For example, I want to have some tags -say, the <h1>- have an attribute and a . The thing is that I also...
3
by: Rohit Sharma | last post by:
Hi all.. ..NET + MSXML platform....VB Need to build a list of all the entities and attributes to allow the user to do search...how can i do that from the schema ?? Cheers Rohit
2
by: Shailendra Batham | last post by:
Hello Gurus, I want to put some restrictions on my attribute tag in my XML Schema, anyone out there have any idea how to do that. here is my XML and the XML Schema <?xml version="1.0"...
6
by: Martin | last post by:
Hi, I have a xml file like the one below <?xml version="1.0" encoding="utf-8"?><e1 xmlns:e1="http://tempuri.org/Source1.xsd" e1:att1="1" e1:att2="2" e1:rest="345"/> If I try to create a...
4
by: Mike Jansen | last post by:
Does anyone know why if I create a complexType based off another complexType using xsd:extension the attributes don't seem to be inherited? Is this a bug/non-implementation in the .NET Schema...
3
by: J'son | last post by:
Guys, I have created a custom class that derives from DataList so that I can add some custom client side functionality into each new item row (<td>). Heres the class in its simplest form: ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.