简体   繁体   English

XML模式:具有此名称模式的元素具有此属性

[英]XML Schema: elements with this name pattern have this attribute

I have this XML sample. 我有这个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 Schema - xsd) I am already grouping elements and attributes with xs:group and xs:attributeGroup . (XML架构-xsd)我已经使用xs:groupxs:attributeGroup对元素和属性进行了分组。

Question: Is it possible to do this: elements with name_pattern="year(\\d){4}" have attribute=ncrimes ? 问题:是否可以这样做: elements with name_pattern="year(\\d){4}" have attribute=ncrimes吗?

The design of XML Schema does not encourage an XML vocabulary design like this. XML Schema的设计不鼓励这样的XML词汇设计。 It's generally considered bad form to have element or attribute names with an internal structure like this. 通常,具有这样的内部结构的元素或属性名称是不好的形式。 Rather than 而不是

<year1995 ncrimes="163"/>

you should be using something like 你应该使用类似

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

With this design, you'll find that everything in the XML stack (not just XML Schema but things like XSLT, XPath, and DOM) works a little bit more smoothly. 通过这种设计,您会发现XML堆栈中的所有内容(不仅是XML Schema,而且还包括XSLT,XPath和DOM之类的东西)都可以更加顺畅地工作。

But if you can't or won't change the vocabulary design, then your next best option is to define an element declaration for name="year" with abstract="true" , and then define elements year1995 , year1994 etc as elements in the substitution group of year . 但是,如果您不能或不会更改词汇表设计,那么您的下一个最佳选择是使用abstract="true"name="year"定义元素声明,然后将元素year1995year1994等定义为置换组的year

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

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