简体   繁体   中英

xsi:nil in xslt - how to use it

I'm trying to set xsi:nil to element but got this:

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):

The xml:base, xml:lang, xml:space, and xml:id attributes have two effects in 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. In addition, they have their standard meaning as defined in the core XML specifications. 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. 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. See also 11.1.4 Namespace Aliasing which describes how to use xsl:namespace-alias with these attributes.

The same is true of the schema-defined attributes xsi:type, xsi:nil, xsi:noNamespaceSchemaLocation, and 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. 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. You could also use xsl:namespace-alias to use a different namespace URI aliased to the "special" URI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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