如何在采用逗号分隔的字符串值的 XSD 架构中添加元素 [英] How to add element in XSD Schema that takes comma separated String values

查看:29
本文介绍了如何在采用逗号分隔的字符串值的 XSD 架构中添加元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 XML 模式的新手...我有一个 xsd 架构文件:-

I am new in XML schema ... I have a xsd schema file :-

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.anirban.com/v1"
    xmlns:tns="http://www.anirban.com/v1" elementFormDefault="qualified">

    <complexType name="InputRequest">
        <sequence>
            <element name="Name" maxOccurs="unbounded" type="string">
            </element>
            <element name="Url" type="anyURI" maxOccurs="1" minOccurs="0">
            </element>

            <element name="Employee">
                <complexType>
                    <sequence>
                        <element name="VehicleList" maxOccurs="unbounded" type="string" />
                        <element name="EmployeeName" type="string" default="Anirban" />

                    </sequence>
                </complexType>
            </element>
        </sequence>
    </complexType>


    <complexType name="OutputResponse">
        <sequence>
            <element name="ResponseCode" type="string"></element>
        </sequence>
    </complexType>

    <element name="getInputRequest" type="tns:InputRequest"></element>

    <element name="getOutputResponse" type="tns:OutputResponse"></element>
</schema>

用于创建 SOAP 请求,例如:-

Which is used to create a SOAP Request like :-

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.anirban.com/v1">
   <soapenv:Header/>
   <soapenv:Body>
      <v1:getInputRequest>
         <!--1 or more repetitions:-->
         <v1:Name>Abc</v1:Name>
         <v1:Name>Efg</v1:Name>
         <!--Optional:-->
         <v1:Url>http://localhost:8084/test?wsdl</v1:Url>
         <v1:Employee>
            <!--1 or more repetitions:-->
            <v1:VehicleList>ee</v1:VehicleList>
            <v1:VehicleList>ff</v1:VehicleList>
            <v1:EmployeeName>Anirban</v1:EmployeeName>
         </v1:Employee>
      </v1:getInputRequest>
   </soapenv:Body>
</soapenv:Envelope>

现在我的问题是,我想在中间添加一个元素,例如:- <v1:Orders>Ord1,Ord2,Ord3</v1:Orders> 它将采用带有 逗号分隔 .. 那么我如何在它们之间添加 element Orders 呢?是否可以在 complexType 的序列中添加一个将采用 逗号分隔的字符串值 的元素?请帮忙...

Now my question is, I want to add an element in between like :- <v1:Orders>Ord1,Ord2,Ord3</v1:Orders> which will take String value with comma separated .. So how can I add the the element Orders in between ? Is it possible to add an element which will take comma separated String values in the sequence of complexType ??? Please help ...

推荐答案

正如helderdarocha 已经指出的那样,您几乎可以肯定将元素指定为元素序列会更好:

As helderdarocha has already pointed out, you would almost certainly do better to specify your element as a sequence of elements:

<v1:Order>Ord1</v1:Order>
<v1:Order>Ord2</v1:Order>
<v1:Order>Ord3</v1:Order>

如果愿意,您可以将它们包装在 v1:Orders 元素中;不同的人对这样的事情有不同的品味.

You can wrap them in a v1:Orders element if you like; different people have different tastes when it comes to things like that.

如果出于某种原因,使用一系列原子值来制作单个元素确实更好(我实际上暂时不相信它真的会更好,但许多人需要从他们的痛苦经历中学习)自己的而不是学习别人的惨痛经验),那么在XSD和相关工具中做列表空格分隔会更好:

If for some reason it really is better to make a single element with a sequence of atomic values (I don't actually believe for a moment that it really will be better, but many people need to learn by painful experience of their own instead of learning from other people's painful experience), then it will work better in XSD and related tools to make the list white-space separated:

<v1:Orders>Ord1 Ord2 Ord3</v1:Orders>

这样,Orders 元素可以声明为简单值列表,并且可以针对类型(整数、日期、IDREF、NCName、您选择的正则表达式定义的标记,...)测试各个值.XSD 验证器和下游的模式感知软件将看到一系列简单的值,而不是一个.

That way, the Orders element can be declared as a list of simple values, and the individual values can be tested against a type (integer, date, IDREF, NCName, regex-defined token of your choice, ...). The XSD validator, and schema-aware software downstream, will see a sequence of simple values, not one.

从 XSD 的角度来看,使其成为逗号分隔的列表是所有可能解决方案中最糟糕的.XSD 列表类型是空格分隔的,而不是逗号分隔的(并且不是任意的正则表达式分隔,无论世界上的 Perl 爱好者有多喜欢它),因此 XSD 验证器和下游模式感知应用程序都不会看到一系列值;他们只会看到一个值,词法空间受一个很长的复杂(并且可能是错误的,除非你比普通程序员更好)正则表达式的约束.如果单个标记应该表示整数、日期或其他一些 XSD 简单类型,则不会对它们进行类型验证.如果要限制列表的长度,只能通过用于验证字符串的正则表达式;另一个出错的机会.

Making it a comma-delimited list is, from an XSD point of view, the worst of all possible solutions. XSD list types are whitespace-delimited, not comma-delimited (and not arbitrary-regex-delimited, no matter how much the Perl aficionados of the world might have liked that), so neither the XSD validator nor downstream schema-aware applications will see a sequence of values; they'll see just one value, with a lexical space constrained by a long complicated (and possibly faulty, unless you're better than the average programmer) regular expression. If the individual tokens are supposed to represent integers, dates, or some other XSD simple type, you get no type validation for them. If you want to restrict the length of the list, it's possible only by means of the regex used to validate the string; another opportunity for error.

另一方面,如果单个标记没有词法约束,并且您确实坚持使用逗号分隔的列表而不是空格分隔的列表,那么您根本无需做任何工作:只需声明 Orders 元素作为 xsd:string.因为 XSD 不会对逗号做任何特别的事情,所以不需要一个可以用逗号做任何聪明事情的模式;由于标记没有词法约束,因此模式在任何情况下都不需要做任何工作.

If on the other hand the individual tokens have no lexical constraints, and you really really insist on having a comma-delimited list and not a whitespace-delimited list, you have no work to do at all: just declare the Orders element as xsd:string. Since XSD won't do anything special with the commas, there is no need for a pattern that does anything clever with commas; since the tokens have no lexical constraints, there is no work for a pattern to do in any case.

这篇关于如何在采用逗号分隔的字符串值的 XSD 架构中添加元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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