Re: Can an attribute have an alias name?

Mukul Gandhi <[email protected]>
Newsgroups gmane.text.xml.schema.devel
Message-ID <CABuuzNM0bQYRkQsgktBSpScFzh0oCN3Trcaz4ZdRZPN-0fyqEA@mail.gmail.com>
I was actually playing earlier with writing a schema, as follows (only
schema fragments shown, describing properties of a "book"):

<xs:element name="pubyear" type="PUB_YEAR" minOccurs="0"/>

<xs:attribute name="pubyear" type="PUB_YEAR"/>

<xs:simpleType name="PUB_YEAR">
    <xs:restriction base="xs:gYear">
         <xs:explicitTimezone value="prohibited"/>
    </xs:restriction>
</xs:simpleType>


I wanted that, both of following instances should be allowed:

<book pubyear="2005">
  ...
</book>


<book>
  ...
  <pubyear>2005</pubyear>
  ...
</book>


i.e allowing XML instance writers to have 'pubyear' either as an
attribute, or as a child element of "book". One of these is necessary,
but both of these are not allowed.

To enforce such a constraint, I found XML Schema 1.1 assertions to be
useful (as follows):

<xs:assert test="count(@pubyear | pubyear) = 1"/>

PS: xs:explicitTimezone is also XSD 1.1 specific.

And I found the use-case mentioned in this thread, similar to that
mentioned above, and thought of sharing the thoughts here.

I hope this is useful.

On Fri, Jul 1, 2011 at 2:04 PM, Mukul Gandhi <[email protected]> wrote:
> The suggestion of using assertions in this case, was not to say that
> this method is best by design. Assertions seem to solve the "alias"
> creation problem been mentioned.
>
> The best design may be (as suggested by Jozef & Mike) to keep your
> schema same (it doesn't look good design to define two attributes in
> schema, for one problem domain concept), and write a transformation
> (XSLT is good for this need) pre-process step to convert one attribute
> to another (as required by your original schema).





-- 
Regards,
Mukul Gandhi
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.