Validating XML where element names contain sequence numbers

独自空忆成欢 提交于 2019-12-14 03:26:11

问题


I'm trying to create an XSD for validating XML we receive from our customer. The XML looks something like this:

<someElement>
   <items>
      <item1 name=”abc” />
      <item2 name =”def” />
      <item3 name =”ghi” />
   </items>
</someElement>

Note that for whatever reason the item names also contain a number. The number of items isn't specified and will vary by file.

Because of the line numbers in the element names something like this doesn't work:

<xs:sequence>
   <xs:element name="items" type="item" maxOccurs="unbounded" minOccurs="0" />
</xs:sequence>

What would be proper XSD to validate this?


回答1:


In previous cases where I've had to mechanically process badly-design XML, be it for schema validation or binding to a class model, I've found that pre-processing the XML with an XSL transform is often a good start. This pre-processing can in many cases turn badly-design XML into something nicer.

In your case, you could write a transform that turns

<item1 name="abc"/>

into

<item num="1" name="abc"/>

This is then much easier to design a schema for. If a given XML input doesn't conform to that pattern, then the XSLT should leave it alone, and it will then fail validation.




回答2:


Stupid question: have you tried type="xs:string" instead of type="item"? I think you could get some inspiration using this.



来源:https://stackoverflow.com/questions/10548550/validating-xml-where-element-names-contain-sequence-numbers

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