xslt-2.0saxon

I don't understand how to expand attributes with default values with Saxon xslt transformation


I am using Saxon-J EE 12.5 and I don't understand how to expand attributes with default values, and also use a schema-aware transform (I understand that I need to use a schema-aware transform if I want to expand default values for attributes, with is logical).

Basically I understand that this expansion will consider that the attribute value has the default value if the value is not present in the input. Am I right?

In Saxon configuration properties, the EXPAND_ATTRIBUTE_DEFAULTS property is explained as: http://saxon.sf.net/feature/expandAttributeDefaults

EXPAND_ATTRIBUTE_DEFAULTS determines whether fixed and default values defined in a schema or DTD will be expanded (both on input and on output documents, if validation is requested). By default (and for conformance with the specification) validation against a DTD or schema will cause default values defined in the schema or DTD to be inserted into the document. Setting this feature to false suppresses this behavior. In the case of DTD-defined defaults this only works if the XML parser reports whether each attribute was specified in the source or generated by expanding a default value. Not all XML parsers report this information.

I have these questions:


Solution

  • The critical thing is not that the transformation is schema-aware, but that the source document is validated against a schema.

    If you're using the s9api interface, the first thing is that you need to start with new Processor(true) to force loading of a Saxon-EE configuration.

    You can control validation of the source document either from the s9api API, or from the transformation. For example you can load a schema into the Processor using the SchemaManager, get a SchemaValidator from this, and supply the SchemaValidator to the DocumentBuilder used to build the source document of the transformation.

    Another way is for the Source object that you supply as input to the transformation to be an instance of AugmentedSource on which you have called setSchemaValidationMode(...).

    You could also invoke the validation using XSLT logic by writing, for example <xsl:copy-of select="..." validation="strict"/>. In this case the schema to be used for validation can be supplied either using the SchemaManager API as above, or using <xsl:import-schema> in the stylesheet. This approach may be less efficient because it is best to perform schema validation while the document is being parsed and built into a tree, rather than while copying the tree subsequently.

    It's possible to force validation of all documents (including those read by doc() or collection()) with a configuration setting, Processor.setConfigurationProperty(Feature.SCHEMA_VALIDATION, ...); but I find this can often be a nuisance because there are often documents that you don't want to validate. Setting the value to "lax" (which roughly speaking means validate if there's a schema, don't validate if not) can be a useful choice.