用于不同元素名称的 XSD [英] XSD for varying element names

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

问题描述

我想为元素范围从 z1-zx 的 XMl 形成一个 xsd 模式.是否可以在 xml 模式中定义它而不必写出声明每个元素.

请看下面:

<区域><Z1>资产00</Z1><Z2>资产00</Z2></区域>

我希望区域能够到达 Zxxx 而不必在 XSD 中声明每个区域,是否可以这样做?

请注意,我无法更改 xml 的结构,因为我将其用于另一个只能采用此格式的软件.

解决方案

XSD 1.0

您可以在 XSD 1.0 中做的最好的事情是允许 Zones 下的任何元素:

XSD 1.1

在 XSD 1.1 中,您可以进一步限制 Zones 的子代名称以 Z 开头:

<xs:element name="区域"><xs:complexType><xs:序列><xs:any processContents="skip" maxOccurs="unbounded"/></xs:sequence><xs:assert test="* 中的每一个 $e 都满足 starts-with(local-name($e), 'Z')"/></xs:complexType></xs:element></xs:schema>

您可以更进一步,要求 Z 前缀之后的字符串是一个数字,甚至可能还施加一个排序约束,但这应该会让您指向正确的方向.

I want to form an xsd schema for an XMl whose elements will range from z1-zx. Is it possible to define this in the xml schema without having to write out declare each of the elements.

Please see below:

<?xml version="1.0"?>
<Zones>
    <Z1>Asset00</Z1>
    <Z2>Asset00</Z2>
</Zones>

I want the zones to be able to go upto Zxxx without having to declare each and every one in the XSD, is it possible to do this?

Please note that I wouldn't be able to change the structure of the xml, as I am using this for another software which can only take this format.

解决方案

XSD 1.0

The best you can do in XSD 1.0 is allow any elements under Zones:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Zones">
    <xs:complexType>
      <xs:sequence>
        <xs:any processContents="skip" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
   </xs:element>
</xs:schema>

XSD 1.1

In XSD 1.1 you can go further and constrain the names of the children of Zones to start with Z:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
  vc:minVersion="1.1">
  <xs:element name="Zones">
    <xs:complexType>
      <xs:sequence>
        <xs:any processContents="skip" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:assert test="every $e in * satisfies starts-with(local-name($e), 'Z')"/>
    </xs:complexType>
   </xs:element>
</xs:schema>

You could go even further and require that the string after the Z prefix be a number and probably even impose a sorted constraint as well, but this should get you pointed in the right direction.

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

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