An improbable transformation example
to illustrate the DOM-like XSLT construct <xsl element
name="... ">
-
Basic
- Source document
(RDF list of names)
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
xmlns:vcard='http://www.w3.org/2001/vcard-rdf/3.0#'
xml:base='http://somewhere/'
>
<rdf:Description rdf:about='#JohnSmith'>
<vcard:FN>John Smith</vcard:FN>
</rdf:Description>
<rdf:Description rdf:about='#SarahJones'>
<vcard:FN>Sarah Jones</vcard:FN>
</rdf:Description>
.....
</rdf:RDF>
- Target (list of
names-as tags...)
<?xml version="1.0"?>
<list>
<JohnSmith xml:id="1">
<fullName>John Smith</fullName>
</JohnSmith>
<SarahJones xml:id="2">
<fullName>Sarah Jones</fullName>
</SarahJones>
....
</list>
- Stylesheet
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version='1.0'
xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
xmlns:vcard='http://www.w3.org/2001/vcard-rdf/3.0#'
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<list>
<xsl:apply-templates
select="rdf:RDF/rdf:Description" />
</list>
</xsl:template>
<xsl:template match="rdf:Description">
<xsl:element
name="{substring-after(@rdf:about, '#')}">
<xsl:attribute
name="xml:id">
<xsl:value-of select="position()" />
</xsl:attribute>
<fullName>
<xsl:value-of select="vcard:FN" />
</fullName>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
-
Advanced
- Source (a more
detailed RDF list of names)
<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
xmlns:vcard='http://www.w3.org/2001/vcard-rdf/3.0#'
xml:base='http://somewhere/'
>
<rdf:Description rdf:about='#JohnSmith'>
<vcard:FN>John Smith</vcard:FN>
<vcard:N rdf:nodeID="A0"/>
</rdf:Description>
<rdf:Description rdf:nodeID="A0">
<vcard:Family>Smith</vcard:Family>
<vcard:Given>John</vcard:Given>
</rdf:Description>
....
</rdf:RDF>
- Target (names-as-tags
with additional detail)
<?xml version="1.0"?>
<list>
<JohnSmith xml:id="1">
<fullName>John Smith</fullName>
<familyName>Smith</familyName>
<personalName>John</personalName>
</JohnSmith>
...
</list>
- Stylesheet
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version='1.0'
xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
xmlns:vcard='http://www.w3.org/2001/vcard-rdf/3.0#'
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<list>
<xsl:apply-templates
select="rdf:RDF/rdf:Description[@rdf:about]" />
</list>
</xsl:template>
<xsl:template match="rdf:Description[@rdf:about]">
<xsl:variable name="VN"
select="vcard:N/@rdf:nodeID" />
<xsl:element name="{substring-after(@rdf:about,
'#')}">
<xsl:attribute
name="xml:id">
<xsl:value-of select="position()" />
</xsl:attribute>
<fullName>
<xsl:value-of select="vcard:FN" />
</fullName>
<xsl:apply-templates
select="//rdf:Description[@rdf:nodeID=$VN]" />
</xsl:element>
</xsl:template>
<xsl:template match="rdf:Description[@rdf:nodeID]">
<familyName>
<xsl:value-of
select="vcard:Family" />
</familyName>
<personalName>
<xsl:value-of
select="vcard:Given" />
</personalName>
</xsl:template>
</xsl:stylesheet>
Tech. Notes
"rdf:Description[@rdf:about]"
and "rdf:Description[@rdf:nodeID]"
simply naming the attribute in the predicate
(instead of comparing its value) means "selecting those Desc. that have such an attr.
-
Use
of an
<xsl:variable>
This is not a variable in the ususal sense of (imperative)
programming : its value cannot change !
It is more like the "let var=expr in
{ sequence }
" construct of functional languages.
In our example, it is necessary to remember the value of the XPath
expr. "vcard:N/@rdf:nodeID"
in the orignal Description
node
in order to test the value of "@rdf:nodeID"
in other Description
nodes.