-->

Validating XML where element names contain sequenc

2019-08-24 12:26发布

问题:

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.