XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (225 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
11.94Mb size Format: txt, pdf, ePub

It's worth noting that many XSLT processors do not physically remove whitespace text nodes from the tree; they only behave as if they did. Whether the nodes are physically removed or whether the processor creates a view of the tree in which these nodes are invisible, whitespace stripping can incur a significant cost. However, if whitespace is stripped while the tree is being built from serial XML input, the performance arguments are reversed: it then becomes cheaper to remove the whitespace nodes than to preserve them. Generally, if whitespace is insignificant, then it's best to get rid of it as early as possible.

Examples

To strip whitespace nodes from all elements of the source tree:


To strip whitespace nodes from selected elements:


To strip whitespace nodes from all elements except the

element:



To strip whitespace nodes from all elements except those in the namespace with URI
http://mednet.org/text
:


                               xmlns:mednet=“http://mednet.org/text” />

See Also


on page 465

xsl:processing-instruction

The

instruction is used to write a processing instruction node to the result sequence.

Changes in 2.0

A
select
attribute has been added. Any leading spaces in the value of the processing instruction are now discarded.

Format

  name = { NCName }

  select? = expression>

  


Position


is an instruction. It is always used as part of a sequence constructor.

Attributes

Name
Value
Meaning
name
mandatory
Attribute value template returning an NCName
The name (target) of the generated processing instruction
select
optional
XPath Expression
Used to compute the string value (the data part) of the generated processing instruction

Content

If the
select
attribute is present, the element must be empty. If the
select
attribute is absent, the element may contain a sequence constructor.

Effect

The name of the generated processing instruction (in XML terms, the
PITarget
) is determined by the
name
attribute. This may be expressed as an attribute value template. The name must be valid as a
PITarget
as defined in the XML specification, and XSLT imposes the additional rule that it must be a valid
NCName
, as defined in the XML Namespaces Recommendation. This means it must be an XML
Name
that doesn't contain a colon (to make it an
NCName
) and that isn't the name
xml
in any mixture of upper and lower case (to make it a
PITarget
).

The specification is quite explicit that

cannot be used to generate an XML declaration at the start of the output file. The XML declaration looks like a processing instruction, but technically it isn't one, and the ban on using the name
xml
makes this quite explicit. The XML declaration in the output file is generated automatically by the serializer and can be controlled using the

element.

The string value of the processing instruction (which corresponds to the
data
part of a processing instruction in XML terms) is generated using either the
select
attribute or the contained sequence constructor. If neither is present, the string value of the processing instruction node will be a zero-length string.

The space that separates the
PITarget
from the data is produced automatically when a processing instruction node is serialized. It is not present in the tree model.

The sequence produced by evaluating the
select
attribute or the contained sequence constructor is converted to a string in the same way as for

(see page 258), except that (a) the separator between adjacent values is always a single space, and (b) any leading whitespace in the value is discarded. The resulting string forms the string value of the new processing instruction.

If the resulting string contains the characters
?>
, then a space is inserted between the characters to prevent it terminating the processing instruction.

The data part of a processing instruction cannot contain character references such as

, so an error occurs at serialization time if there are characters that can't be represented directly in the chosen character encoding of the output file. Some processing instructions may accept data that looks like a character reference, but this is an application-level convention, not something defined in the XML standard, so the XSLT processor will never generate such a reference automatically.

Usage

Use this instruction when you want to output a processing instruction.

Processing instructions are not widely used in most XML applications, so you will probably not need to use this instruction very often. They are used even less in HTML, though HTML 4.0 does recommend that any vendor-specific extensions should be implemented this way. In HTML the terminator for a processing instruction is
>
rather than
?>
, and this difference is handled automatically during serialization by the HTML output method; see Chapter 15.

Other books

Instant Family by Elisabeth Rose
Friendly Fire by Bryan, C. D. B.;
Bad Tidings by Nick Oldham
Rising by J Bennett
Taste: A Love Story by Tracy Ewens