How to define own types in Relax NG?
Consider the following regular expression
pattern = "(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d"
This pattertn represents date strings in MM/DD/YYYY format. Now, if I want to create several attribues of this type, I can apparently write them like:
element holiday
{
attribute beginDate
{
xs:string { pattern = "(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d" }
}
attribute endDate
{
xs:string { pattern = "(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d" }
}
}
But this is very inconvenient to write the pattern line every time when I want to define a date in such format. I would like to define my own type, e.g. MyDateFormat
, and us开发者_如何转开发e it where necessary as attribute someDate { MyDateFormat }
instead of rewriting the same code multiple times, but I couldn't find a way to define own types using Relax NG compact syntax. My question is how is it possible to implement such behaviour and avoid code twice rewriting.
Any help will be appreciated. Thanks in advance.
Found out one solution: new type can be defined as
MMDDYYYY = xs:string { pattern = "(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d" }
and then use it where necessary:
element holiday
{
attribute beginDate { MMDDYYYY }
attribute endDate { MMDDYYYY }
}
精彩评论