How do I defined an XSD attribute name as a regular expression?

半腔热情 提交于 2020-01-24 21:37:05

问题


The XML allows a variable number of attributes of Q followed by a number, like this

<recognitionPattern Q0="12.5" Q1="12.5" Q2="12.5" Q3="12.5" Q4="12.5" Q5="12.5" Q6="12.5" Q7="12.5"/>

My current solution looks something like this:

<xs:complexType name="PerQuarterDoubleHack"> <!-- YUCK! /> -->
<xs:attribute name="Q0"                 type="xs:double"       />
<xs:attribute name="Q1"                 type="xs:double"       />
<xs:attribute name="Q2"                 type="xs:double"       />
<xs:attribute name="Q3"                 type="xs:double"       />
<xs:attribute name="Q4"                 type="xs:double"       />
<xs:attribute name="Q5"                 type="xs:double"       />
<xs:attribute name="Q6"                 type="xs:double"       />
<xs:attribute name="Q7"                 type="xs:double"       />
</xs:complexType>

I've seen lots of regular expressions applied to the value, but never an attribute name. Do I have any choice than changing the XML?


回答1:


XML Schema doesn't allow you to apply any validation to an attribute name, only its value. Your best bet is to change the XML, if possible, to something along these lines:

<recognitionPattern>
    <quarter id="0">12.55</quarter> <!-- equivalent to Q0 -->
    <quarter id="1">12.55</quarter> <!-- equivalent to Q1 -->
    <quarter id="2">12.55</quarter> <!-- equivalent to Q2 -->
    <quarter id="3">12.55</quarter> <!-- equivalent to Q3 -->
</recognitionPattern>



回答2:


It's a very poor XML design. Generally, XML processing tools assume that the application knows all the element and attribute names statically, and if this isn't the case, writing applications will be difficult. Change the design if you have the chance.



来源:https://stackoverflow.com/questions/11434098/how-do-i-defined-an-xsd-attribute-name-as-a-regular-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!