简体   繁体   English

XML Schema的xs:ID类型对XML元素有效,还是仅对于attribuutes?

[英]Is XML Schema's xs:ID type valid for an XML element, or only for attribuutes?

In the W3C Reco ( here ), I find that 在W3C Reco( 这里 ),我发现了

[...] ID is a type of attribute and so it cannot be applied to attributes, elements or their content [...] ID是一种属性,因此无法应用于属性,元素或其内容

As I can find some in the file I must deal with, I wonder if xsi:ID is really a valid xlm element type 我可以在文件中找到一些我必须处理的内容,我想知道xsi:ID是否真的是一个有效的xlm元素类型

My existing xsd mention : 我现有的xsd提到:

<xsd:complexType name="customTypeName">
 <xsd:sequence>
  <xsd:element name="ID" type="xsd:ID"/>
  <xsd:element name="myElement" type="string"/>
 </xsd:sequence>
</xsd:complexType>

But according to my reading, I rather think that the valid declaration should rather be 但根据我的阅读,我宁愿认为有效的声明应该是

<xsd:complexType name="customTypeName">
<xsd:element name="myElement" type="string"/>
<attribute name="ID" type="xsd:ID" use="required"/>  
</xsd:complexType>

But unfortunatelly, I can't change the already used xsd... I'm willing to apply xslt search using the id() function. 但不幸的是,我无法改变已经使用的xsd ...我愿意使用id()函数应用xslt搜索。 Should I give up or is there a way ? 我应该放弃还是有办法?

Thanks by advance for any help to my first question over a forum. 感谢您对我在论坛上提出的第一个问题的任何帮助。

( Good question, pity it didn't receive enough attention at the time.) 好问题,可惜它当时没有得到足够的重视。)

ID is a type of attribute and so it cannot be applied to attributes, elements or their content ID是一种属性,因此无法应用于属性,元素或其内容

This was a bug in the spec. 这是规范中的一个错误。 It should have said "cannot be applied to elements or their content" . 应该说“不能适用于元素或其内容” In XML Schema 1.1 it is quite clear , though: 在XML Schema 1.1中,很明显

For compatibility, ID should be used only on attributes. 为了兼容性,ID应仅用于属性。

This means, you can apply the type to attributes, but not to elements, but it typically won't raise an error when you do. 这意味着,您可以将类型应用于属性,但不应用于元素,但通常不会引发错误。

However, using xs:ID does not always have the effect that people expect. 但是,使用xs:ID并不总是具有人们期望的效果。 For instance, it expects an NCName , which means it cannot start with a digit. 例如,它需要一个NCName ,这意味着它不能以数字开头。 Also, the uniqueness constraint is not enforced. 此外,不强制执行唯一性约束。

It is usually a lot better to use xml:id . 使用xml:id通常要好得多。 It is well-defined by the spec , and it is well-supported by the fn:id() function you referred to. 由规范定义得很好 ,并且你所引用的fn:id()函数很好地支持它。 Also, using xml:id , people don't need to learn a new name and it doesn't require an extra namespace definition. 此外,使用xml:id ,人们不需要学习新名称,也不需要额外的命名空间定义。

Even if your XSD validator accepts xs:ID on an element, the XPath specification does not accept it and will not return it when using fn:id() . 即使您的XSD验证器在元素上接受xs:ID ,XPath规范也不接受它,并且在使用fn:id()时不会返回它。 However, both this function and the element-with-id() function return the element the ID is applied on anyway, so that does not need to be too much of a problem. 但是,这个函数和element-with-id()函数都会返回无论如何应用ID的元素,因此不需要太多问题。

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

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