使用序列创建多个元素名称. [英] Creating multiple element name with sequence.

查看:42
本文介绍了使用序列创建多个元素名称.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是示例 xml,它有多个 ,从序列 1 开始,它可以结束很多规则,如 ;、<rule2>、<rule3> 等等......

Below is the sample xml which has multiple <rulex> that starts with sequence 1 and it can end up to many rule like <rule1> , <rule2>, <rule3> etc....

<?xml version="1.0" encoding="UTF-8"?>
<AddressChange_181>
    <rules>
        <rule1>
            <conditions>xya</conditions>
            <response_path>abc</response_path>
        </rule1>
        <rule2>
            <conditions>xxxx</conditions>
            <response_path>aaaa</response_path>
        </rule2>
        <rule3>
            <conditions>yyyyy</conditions>
            <response_path>ffff</response_path>
        </rule3>
        <rule4>
            <conditions>zzzz</conditions>
            <response_path>yyyy</response_path>
        </rule4>
        <default>
            <response_path>uuuuu</response_path>
        </default>
    </rules>
</AddressChange_181>

下面是我尝试为上述 xml 创建动态 元素名称的模式.当我从这个模式生成 xml 时,我没有得到与上面 xml 相同的 xml 格式.您能否让我知道如何创建具有以序列号开头的多个元素名称的架构.我的要求是在 xml 文件中添加多个规则( 等...),并且该 xml 文件应根据架构进行验证.

Below is the schema where i tried creating dynamic <rulex> element name for the above xml. when i generate xml from this schema i do not get the same xml format as above xml. can you please let me know how to create schema with multiple element name that starts with a sequence number. My requirement is to add more than one rule (<rule1>,<rule2>,<rule3> etc...) in xml file and this xml file should be validated against schema.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="mock_rule_list"> 
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="rules">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="rule" minOccurs="0" maxOccurs="unbounded"/>        
        <xs:element ref="default" maxOccurs="1"/>
      </xs:sequence>
    </xs:complexType>   
  </xs:element>

  <xs:element name="rule">    
    <xs:complexType>  
    <xs:simpleContent>
      <xs:restriction base="xs:anyType">
        <xs:pattern value="rule/d{1,3}"></xs:pattern>
      </xs:restriction>
    </xs:simpleContent>      
      <xs:sequence>
        <xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
        <xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
      </xs:sequence>
    </xs:complexType>   

  </xs:element>

  <xs:element name="default">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="response_path"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="conditions" type="xs:string">   
  </xs:element>
  <xs:element name="response_path" type="xs:string"/>
</xs:schema>

谢谢,马杜

推荐答案

如果 ruleX 标签的数量是任意定义的,则无法使用 XSD 定义这样的结构.如果您可以将上限限制为最大值,并且您确实必须遵守 ruleX 命名约定,那么您可以定义一个复杂类型,例如 ruleType,然后是该类型的一堆 rule1、rule2、...、ruleN 元素- 我会称之为凌乱......我不推荐这个.

There is no way to define such a structure using XSD if the number of ruleX tags is arbitrarily defined. If you can constrain the upper bound to a maximum, and you really have to stick with ruleX naming convention, than you can define a complex type such as ruleType, then a bunch of rule1, rule2,... , ruleN elements of that type - I would call this messy... I wouldn't recommend this.

XSD(最多 6 个):

XSD (with a maximum of 6):

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="mock_rule_list">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="rules">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="rule1" minOccurs="0"/>
                <xs:element ref="rule2" minOccurs="0"/>
                <xs:element ref="rule3" minOccurs="0"/>
                <xs:element ref="rule4" minOccurs="0"/>
                <xs:element ref="rule5" minOccurs="0"/>
                <xs:element ref="rule6" minOccurs="0"/>
                <xs:element ref="default"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>


    <xs:complexType name="ruleType">
        <xs:sequence>
            <xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
            <xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="default">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="response_path"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="conditions" type="xs:string">
    </xs:element>
    <xs:element name="response_path" type="xs:string"/>

    <xs:element name="rule1" type="ruleType"/>
    <xs:element name="rule2" type="ruleType"/>
    <xs:element name="rule3" type="ruleType"/>
    <xs:element name="rule4" type="ruleType"/>
    <xs:element name="rule5" type="ruleType"/>
    <xs:element name="rule6" type="ruleType"/>
</xs:schema>

或者,您可以使用带有 @sequence 属性的名为rule"的标签.

Alternatively, you could have a tag named "rule" with a @sequence attribute.

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="mock_rule_list">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="rules">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="rule" minOccurs="0" maxOccurs="unbounded"/>
                <xs:element ref="default"/>
            </xs:sequence>
        </xs:complexType>
        <xs:unique name="SequenceKey">
            <xs:selector xpath="rule"/>
            <xs:field xpath="@sequence"/>
        </xs:unique>
    </xs:element>

    <xs:complexType name="ruleType">
        <xs:sequence>
            <xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
            <xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
        <xs:attribute name="sequence" type="xs:int" use="required"/>
    </xs:complexType>

    <xs:element name="default">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="response_path"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="conditions" type="xs:string">
    </xs:element>
    <xs:element name="response_path" type="xs:string"/>

    <xs:element name="rule" type="ruleType"/>
</xs:schema>

示例 XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<rules xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <rule sequence="1">
        <conditions>conditions1</conditions>
        <response_path>response_path1</response_path>
    </rule>
    <rule sequence="2">
        <conditions>conditions2</conditions>
        <response_path>response_path2</response_path>
    </rule>
    <default>
        <response_path>response_path1</response_path>
    </default>
</rules>

或者也可以简单地使用父集合中 的索引.

Or it could also be that one could simply use the index of the <rule/> within the parent collection .

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

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