XML 架构:不同的元素名称(序列) [英] XML Schema: Different Element Names (sequence)

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

问题描述

我认为解决我的问题很简单,但我找不到所以,这里是:

I think the solution to my problem is very easy, but i couldn't fint it So, here is:

我有一个 XML,其中包含具有不同名称但按顺序排列的元素列表.一个例子:

I have an XML which have a list of elements with different names, but in sequence. An example:

<DOC>
 <DOC_OBL_1>
  <TIP_DOC_OBL>1</TIP_DOC_OBL> 
 </DOC_OBL_1>
 <DOC_OBL_2>
  <TIP_DOC_OBL>2</TIP_DOC_OBL> 
 </DOC_OBL_2>
 <DOC_OBL_3>
  <TIP_DOC_OBL>3</TIP_DOC_OBL>  
 </DOC_OBL_3>
</DOC>

所以,我有 3 个元素:DOC_OBL_1、DOC_OBL_2 和 DOC_OBL_3.是的,可能有数字 4、5、6 等.正如您所看到的,所有 3 个内部都有相同的元素(实际上,它们有很多,但现在并不重要),我想我可以声明一个可以验证此类文档的通用类型.

So, i have 3 elements: DOC_OBL_1, DOC_OBL_2 and DOC_OBL_3. And yes, there could be number 4, 5, 6, etc. As you can se, all 3 have the same elements inside(actually, they have a lot of them, but arent important righ now), and I thinked i could declare a general type which could validate this kind of documents.

我如何使用架构验证这一点???

How can i validate this with an Schema???

我知道它是一个非常丑陋的 XML(也许它不是标准的,请告诉我,我不知道),但是我不关心构建这个文档.我只需要解析它、验证它并转换它.

I know its a very ugly XML (maybe it isnt standard, please tell me, i dont know), but It's not my concern to build this document. I just have to parse it, validate it and transform it.

推荐答案

好吧,当然可以!实际上非常简单:如果每个元素的结构都相同,您可以定义一个 来验证它,然后使用:

Well, sure you can! Pretty simple actually: if the structure is the same for each element, you can define a single <xs:complexType> to validate that, and then use:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="DOC" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="DOC">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="DOC_OBL_1" type="DocType" />
        <xs:element name="DOC_OBL_2" type="DocType" />
        <xs:element name="DOC_OBL_3" type="DocType" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:complexType name="DocType">
    <xs:sequence>
      <xs:element name="TIP_DOC_OBL" type="xs:string" minOccurs="0" />
    </xs:sequence>
  </xs:complexType>
</xs:schema>

这对你有用吗?它能满足您的所有需求吗?

Does that work for you? Does it handle all your needs?

正如 Zach 指出的那样——这个解决方案"显然是相当有限的,因为它不能处理任意数量的标签 DOC_OBL_1、DOC_OBL_2、....、DOC_OBL_x——名称以及标签的数量必须提前知道.

As Zach points out quite correctly - this "solution" obviously is rather limited, since it can't deal with an arbitrary number of tag DOC_OBL_1, DOC_OBL_2, ...., DOC_OBL_x - the name and thus the number of tags must be known ahead of time.

这很不幸,但鉴于这个残缺的 XML,这是唯一的解决方案.真正的解决方案是:

This is unfortunate, but it's the only solution, given this crippled XML. The REAL solution would be to have something like:

<DOC>
  <DOC_OBL id="1">
  </DOC_OBL>
  <DOC_OBL id="2">
  </DOC_OBL>
  .....
  <DOC_OBL id="x">
  </DOC_OBL>
</DOC>

然后 XML 模式会变得更加简单,并且可以处理任意数量的 标签.

and then the XML schema would become even easier and could deal with any number of <DOC_OBL> tags.

但是 GIGO 原则适用:Garbage In, Garbage Out ==> 糟糕的 XML 结构进来了,只有糟糕的、不完整的验证是可能的.

But the GIGO principle applies: Garbage In, Garbage Out ==> crappy XML structure comes in, only a crappy, incomplete validation is possible.

马克

这篇关于XML 架构:不同的元素名称(序列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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