验证元素名称包含序列号的 XML [英] Validating XML where element names contain sequence numbers

查看:29
本文介绍了验证元素名称包含序列号的 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 XSD 来验证我们从客户那里收到的 XML.XML 看起来像这样:

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>

什么是合适的 XSD 来验证这一点?

What would be proper XSD to validate this?

推荐答案

在以前我不得不机械地处理设计糟糕的 XML 的情况下,无论是为了模式验证还是绑定到类模型,我发现使用 XSL 转换预处理 XML 通常是一个好的开始.在许多情况下,这种预处理可以将设计不佳的 XML 变成更好的东西.

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"/>

进入

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

这样设计架构就容易多了.如果给定的 XML 输入不符合该模式,则 XSLT 应不理会它,然后验证将失败.

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.

这篇关于验证元素名称包含序列号的 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆