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

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

XML Envelope/Payload Applications

It is not uncommon to find structures in which one XML document is wrapped in a CDATA section inside another. For example:


  

… 

  

     …]]>

  


I don't normally recommend this as a good way of designing nested structures. In general, it is usually better to nest the structure directly, without using CDATA. That is, to use:


  

… 

  

     

  


But sometimes you don't get to design the documents yourself; and there are some advantages for the CDATA approach, such as the ability for the payload document to include a DOCTYPE declaration.

Handling such structures in XSLT is not easy: the payload document is presented as a single text node, not as a tree of element nodes. However, the
unparsed-text()
function makes it much easier to output such structures. All you need to do is:




  

… 

  

     

  


HTML Boilerplate Generation

Generally, it is best to think of HTML in terms of a tree of element and text nodes, and to manipulate it as such in the stylesheet. Occasionally, you may need to process HTML that is not well formed, and cannot easily be converted into a well-formed structure. For example, you may be dealing with a syndicated news feed that arrives in HTML, whose format is sufficiently unpredictable that you don't want to rely on tools that automatically turn the HTML into structured XHTML. You might want to output the HTML news stories embedded in your own XSLT-generated pages.

An option in such cases is to treat the HTML as unparsed text rather than as a tree of nodes. You can read the HTML news feed using the
unparsed-text()
function, and you can output it to the serialized result, using the
disable-output-escaping
option, provided your processor supports this.

               disable-output-escaping=“yes”/>

Remember when you use
disable-output-escaping
that not all processors support the feature and that it works only if the output of the stylesheet is serialized. You can't always tell whether the output is going to be serialized or not; for example, if you run a transformation in Internet Explorer, the output HTML is serialized and then reparsed before being displayed, but if you run the same transformation in the Firefox browser, the result tree is passed directly to the rendering engine, bypassing the serialization stage. This means that
disable-output-escaping
doesn't work with a client-side transformation in Firefox.

See Also


on page 230 in Chapter 6

upper-case

The
upper-case()
function converts lower-case characters in a string to upper case.

Signature

Argument
Type
Meaning
value
xs:string?
The string to be converted
Result
xs:string
The string with lower-case letters converted to upper case

Effect

See the entry for
lower-case()
on page 827 for a description of how this function is defined in terms of Unicode case mappings.

The effect of the function is as follows:

  • If the input is an empty sequence, the result is the zero-length string.
  • Otherwise, every character in the input string is replaced by its corresponding upper-case character (or sequence of characters) if there is one, or is included unchanged in the result string if not.

The function does not implement case mappings that Unicode defines as being locale-sensitive (such as the Turkish dotless I).

Examples

Expression
Result
upper-case(“Sunday”)
“SUNDAY”
upper-case(“2+2”)
“2+2”
upper-case(“césar”)
“CÉSAR”
upper-case(”
ϵλλα
“)
“E
A

Usage

See
lower-case()
on page 827

See Also

lower-case()
on page 827

translate()
on page 897

year-from-date, year-from-dateTime

These two functions extract the year component from an
xs:date
or
xs:dateTime
value. For example,
year-from-date(current-date())
might return 2008.

Signature

Argument
Type
Meaning
input
xs:date?
or
xs:dateTime?
The value from which the year component is to be extracted. The type of the supplied argument must correspond to the type implied by the function name. If an empty sequence is supplied, an empty sequence is returned.
Result
xs:integer
The year. The range of values is implementation-defined; negative years (representing BC dates) are allowed
.

Effect

The function returns the year component of the supplied
xs:date
or
xs:dateTime
. The value is used in its local timezone (not normalized to UTC).

Examples

Expression
Result
year-from-date(xs:date(“2008-02-28”))
2008
year-from-dateTime(xs:dateTime(“1969-07-20T16:17:00-04:00”))
1969

See Also

current-date(), -dateTime(),-time()
on page 738

format-date()
,
-dateTime()
,
-time()
on page 781

day-from-date()
,
-dateTime()
on page 744

month-from-date()
,
-dateTime()
on page 833

years-from-duration

This function extracts the value of the years component from a normalized
xs:duration
value.

Signature

Argument
Type
Meaning
input
xs:duration
The value from which the component is to be extracted
Result
xs:integer
The years component

Effect

The function returns the years component of the supplied
xs:duration
. The duration value is first normalized so that the number of months is less than 12. The result will be negative if the duration is negative.

Examples

Expression
Result
years-from-duration(xs:yearMonthDuration(“P1200Y”))
1200
years-from-duration(xs:duration(“P18 M”))
1
years-from-duration(xs:duration(“-P3Y6 M”))
-3
years-from-duration(xs:dayTimeDuration(‘P8000D’))
0

Other books

Eve Silver by Dark Desires
Demon's Kiss by Devereaux, V. J.
The Book of the Dead by Elizabeth Daly
Violated by Jamie Fessenden
Rough Country by John Sandford
A Disgraceful Miss by Elaine Golden
Containment by Sean Schubert