Datasheet
Williams c01.tex V3 - 07/31/2009 2:53pm Page 18
Chapter 1: First Steps with XSLT
Next, declare the output method as
"xml"
and the encoding as
"UTF-8"
. In the main template, create
the literal result elements
<rdf:RDF>
and
<channel>
, in that order, as the container for your output. The
namespaces must be declared again on the
<rdf:RDF>
element:
<?xml version
=
"1.0" encoding
=
"UTF-8"?>
<xsl:stylesheet version
=
"2.0"
xmlns:xsl
=
"http://www.w3.org/1999/XSL/Transform"
xmlns:rdf
=
"http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc
=
"http://purl.org/dc/elements/1.1/"
xpath-default-namespace
=
"http://www.w3.org/2005/Atom">
<xsl:output method
=
"xml" encoding
=
"UTF-8"/>
<xsl:template match
=
"/">
<rdf:RDF
xmlns:rdf
=
"http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc
=
"http://purl.org/dc/elements/1.1/"
<channel>
...
</channel>
</rdf:RDF>
</xsl:template>
...
</xsl:styleheet>
Specifying Attributes
Both the
<channel>
and
<item>
elements require the
rdf:about
attribute.
An attribute can be set directly on a literal result element if you know its value ahead of time, but in
this case you need to use another approach, with the <
xsl:attribute>
instruction. This element should
always come first in any set of sequence constructor instructions. You can use either the element content
or the
select
attribute, but note that these approaches are mutually exclusive.
The XSLT 2.0 schema definition looks like this:
<xs:element name
=
"attribute" substitutionGroup
=
"xsl:instruction">
<xs:complexType>
<xs:complexContent mixed
=
"true">
<xs:extension base
=
"xsl:sequence-constructor">
<xs:attribute name
=
"name" type
=
"xsl:avt" use
=
"required"/>
<xs:attribute name
=
"namespace" type
=
"xsl:avt"/>
<xs:attribute name
=
"select" type
=
"xsl:expression"/>
<xs:attribute name
=
"separator" type
=
"xsl:avt"/>
<xs:attribute name
=
"type" type
=
"xsl:QName"/>
<xs:attribute name
=
"validation" type
=
"xsl:validation-type"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
To add the
rdf:about
attribute to the
<channel>
element, enter the
<xsl:attribute>
element
right after the
<channel>
element, and use the
<xsl:value-of>
instruction to obtain the link URL
http://news.oreilly.com/
from the
href
attribute on the source feed’s
<link>
element:
18