简体   繁体   English

xml-重复元素-这有效吗?

[英]xml - repeating elements - is this valid?

This XML seems to be valid according to on-line validation servces, but I suspect that each step should be wrapped in a tag to make it unique. 根据在线验证规则,该XML似乎是有效的,但是我怀疑每个步骤都应该包装在标签中以使其唯一。 What rule is this violating? 这违反什么规则?

<tasks>                                               
       <step>fix fan</step>
       <NoInc>RT260454</NoInc>             

       <step>fix power supply</step>
       <NoInc>RT260456</NoInc>                 
</tasks> 

Is it better to express like this? 这样表达更好吗?

<tasks>                                               
   <task>
       <step>fix fan</step>
       <NoInc>RT260454</NoInc>             
   </task>
   <task>        
       <step>fix power supply</step>
       <NoInc>RT260456</NoInc>                 
   </task>
</tasks> 

When mapped this to an array, would I risk overriding the first step with the second? 将其映射到数组时,我会冒着将第一步覆盖第二步的风险吗?

It is valid xml, here is an XSD that supports that xml: 这是有效的xml,这是支持该xml的XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
      xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="tasks" type="tasksType"/>
  <xs:complexType name="tasksType">
    <xs:choice maxOccurs="unbounded" minOccurs="0">
      <xs:element type="xs:string" name="step"/>
      <xs:element type="xs:string" name="NoInc"/>
    </xs:choice>
  </xs:complexType>
</xs:schema>

If a 1 to 1 mapping is required between step and noinc, then it would make sense to wrap them in another tag. 如果在step和noinc之间需要一对一的映射,则将它们包装在另一个标签中是有意义的。

The XML is valid and well-formed. XML有效且格式正确。 However, if the order of the elements is important, you should think carefully about the design your document. 但是,如果元素的顺序很重要,则应仔细考虑文档的设计。 An equally valid document would show fix power supply first and fix fan second. 同样有效的文档将首先显示固定电源,然后显示固定风扇 Or even both <NoInc> elements first and then both <Step> elements. 甚至首先是两个<NoInc>元素,然后是两个<Step>元素。

In addition, if the <NoInc> element depends on the <Step> element in some way, you should show this relationship in your design either by making the NoInc text an attribute of the <Step> element or creating a parent element for each pair of <Step> and <NoInc> elements. 此外,如果<NoInc>元素以某种方式依赖于<Step>元素,则应在设计中显示此关系,方法是将NoInc文本设置为<Step>元素的属性,或者为每对创建一个父元素<Step><NoInc>元素的集合。

For example: 例如:

  <Task>
      <SubTask>
          <step>fix fan</step>
          <NoInc>RT260454</NoInc>
      </SubTask>
      <SubTask>
          <step>fix power supply</step>
          <NoInc>RT260456</NoInc>
      </SubTask>
  </Task>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM