如何在Relax NG中定义自己的类型?
考虑以下正则表达式
pattern = "(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d"
。此模式表示 MM/DD/YYYY 格式的日期字符串。现在,如果我想创建几个这种类型的属性,我显然可以这样写:
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" }
}
}
但是每次我想以这种格式定义日期时都编写模式行非常不方便。我想定义自己的类型,例如 MyDateFormat
,并在必要时将其用作 attribute someDate { MyDateFormat }
,而不是多次重写相同的代码,但我不能'找不到使用 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 use 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了一种解决方案:可以将新类型定义为
然后在必要时使用它:
Found out one solution: new type can be defined as
and then use it where necessary: