简体   繁体   English

xsi:nil in xslt-如何使用它

[英]xsi:nil in xslt - how to use it

I'm trying to set xsi:nil to element but got this: 我正在尝试将xsi:nil设置为element,但是得到了这个:

Attribute 'http://www.w3.org/2001/XMLSchema-instance, nil' must not appear on element 'xsl:element', because the {nillable} property of 'xsl:element' is false.




<xsl:element name="elem" namespace="namespace" xsi:nil="true">
    <xsl:text></xsl:text>
</xsl:element>

From §11.1.2 of the XSLT 3.0 specification (equally applicable to earlier versions): 从XSLT 3.0规范的§11.1.2开始(同样适用于早期版本):

The xml:base, xml:lang, xml:space, and xml:id attributes have two effects in XSLT. xml:base,xml:lang,xml:space和xml:id属性在XSLT中具有两个作用。 They behave as standard XSLT attributes, which means for example that if they appear on a literal result element, they will be copied to the result tree in the same way as any other attribute. 它们的行为类似于标准XSLT属性,这意味着,例如,如果它们出现在文字结果元素上,则它们将以与其他任何属性相同的方式复制到结果树中。 In addition, they have their standard meaning as defined in the core XML specifications. 此外,它们具有核心XML规范中定义的标准含义。 Thus, an xml:base attribute in the stylesheet affects the base URI of the element on which it appears, and an xml:space attribute affects the interpretation of whitespace text nodes within that element. 因此,样式表中的xml:base属性会影响出现该元素的元素的基本URI,而xml:space属性会影响该元素内空白文本节点的解释。 One consequence of this is that it is inadvisable to write these attributes as attribute value templates: although an XSLT processor will understand this notation, the XML parser will not. 这样的结果是不建议将这些属性写为属性值模板:尽管XSLT处理器可以理解这种表示法,但XML解析器不能。 See also 11.1.4 Namespace Aliasing which describes how to use xsl:namespace-alias with these attributes. 另请参见11.1.4命名空间别名,其中描述了如何将xsl:namespace-alias与这些属性一起使用。

The same is true of the schema-defined attributes xsi:type, xsi:nil, xsi:noNamespaceSchemaLocation, and xsi:schemaLocation. 模式定义的属性xsi:type,xsi:nil,xsi:noNamespaceSchemaLocation和xsi:schemaLocation也是如此。 If the stylesheet is processed by a schema processor, these attributes will be recognized and interpreted by the schema processor, but in addition the XSLT processor treats them like any other attribute on a literal result element: that is, their effective value (after expanding attribute value templates) is copied to the result tree in the same way as any other attribute. 如果样式表由模式处理器处理,则这些属性将由模式处理器识别和解释,但是XSLT处理器将它们与文字结果元素上的任何其他属性一样对待:即,它们的有效值(在扩展属性之后)值模板)以与其他任何属性相同的方式复制到结果树。 If the result tree is validated, the copied attributes will again be recognized and interpreted by the schema processor. 如果结果树得到验证,则复制的属性将再次被模式处理器识别和解释。

In practice what this means is that it's best to avoid including these special attributes literally in a stylesheet. 实际上,这意味着最好避免在样式表中原样包含这些特殊属性。 If you want to generate them in the result tree, use xsl:attribute to achieve it. 如果要在结果树中生成它们,请使用xsl:attribute来实现。 You could also use xsl:namespace-alias to use a different namespace URI aliased to the "special" URI. 您还可以使用xsl:namespace-alias来使用别名为“特殊” URI的另一个名称空间URI。

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

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