【问题标题】:XML Schema: elements with this name pattern have this attributeXML Schema:具有此名称模式的元素具有此属性
【发布时间】:2018-11-03 18:01:17
【问题描述】:

我有这个 XML 示例。

<root>
  <Ilhas_da_Madeira_e_Porto_Santo update-date="2016-04-01" update-time="14:14:46">
    <Santana update-author="sourceData" update-date="2016-04-01" update-time="14:14:48">
      <year1995 ncrimes="33"/>
      <year1994 ncrimes="58"/>
      <year1993 ncrimes="33"/>
    </Santana>
  </Ilhas_da_Madeira_e_Porto_Santo>
  <NE update-date="2016-04-01" update-time="14:14:48">
    <NE update-author="sourceData" update-date="2016-04-01" update-time="14:14:48">
      <year1995 ncrimes="163"/>
      <year1994 ncrimes="125"/>
      <year1993 ncrimes="126"/>
    </NE>
  </NE>
</root>

(XML 架构 - xsd) 我已经用xs:groupxs:attributeGroup 对元素和属性进行分组。

问题: 是否可以这样做:elements with name_pattern="year(\d){4}" have attribute=ncrimes

【问题讨论】:

    标签: xml xsd xsd-validation


    【解决方案1】:

    XML Schema 的设计不鼓励这样的 XML 词汇设计。通常认为元素或属性名称具有这样的内部结构是不好的形式。而不是

    <year1995 ncrimes="163"/>
    

    你应该使用类似的东西

    <year y="1995" ncrimes="163"/>
    

    通过这种设计,您会发现 XML 堆栈中的所有内容(不仅是 XML Schema,还包括 XSLT、XPath 和 DOM 之类的东西)的工作更加顺畅。

    但是,如果您不能或不会更改词汇表设计,那么您的下一个最佳选择是使用abstract="true"name="year" 定义元素声明,然后定义元素year1995year1994 等作为year的替换组中的元素。

    【讨论】:

    • 谢谢。这是我得到的结论。我重新设计了我的 XML。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 2010-09-27
    • 2019-04-23
    相关资源
    最近更新 更多