Adding new attributes to schema

Discussion in 'XML' started by craig.wagner@gmail.com, Feb 7, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. George Bina Guest

    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
    George Bina, Feb 7, 2006
    #2
    1. Advertising

  3. 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:
    URL: http://www.ltg.ed.ac.uk/~ht/
    [mail really from me _always_ has this .sig -- mail without it is forged spam]
    Henry S. Thompson, Feb 7, 2006
    #3
  4. Guest

    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, 2006
    #4
  5. 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:
    URL: http://www.ltg.ed.ac.uk/~ht/
    [mail really from me _always_ has this .sig -- mail without it is forged spam]
    Henry S. Thompson, Feb 10, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus
    Replies:
    1
    Views:
    1,518
    Markus
    Nov 23, 2005
  2. Philippe Poulard
    Replies:
    0
    Views:
    435
    Philippe Poulard
    Mar 21, 2005
  3. Stanimir Stamenkov
    Replies:
    3
    Views:
    1,243
    Stanimir Stamenkov
    Apr 25, 2005
  4. Replies:
    3
    Views:
    2,824
  5. pramodr
    Replies:
    3
    Views:
    838
    Peter Flynn
    Apr 5, 2009
Loading...

Share This Page