itsb/xslt/opml.xsl
2021-03-18 21:47:19 +01:00

112 lines
3.7 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:itsb="http://tilde.town/~lucidiot/itsb/">
<xsl:output method="xml" />
<xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<!-- Caution: Final slash is required. -->
<xsl:variable name="itsbRoot" select="'https://tilde.town/~lucidiot/itsb/'" />
<!--
Capitalize the first letter of a string.
Usage:
<xsl:call-template name="capitalize">
<xsl:with-param name="text" select="." />
</xsl:call-template>
-->
<xsl:template name="capitalize">
<xsl:param name="text" />
<xsl:value-of select="concat(translate(substring($text, 1, 1), $lower, $upper), substring($text, 2))" />
</xsl:template>
<xsl:template match="/">
<opml version="2.0">
<head>
<title>International Transport Safety Bureau feeds</title>
<ownerName>Lucidiot</ownerName>
<ownerEmail>lucidiot@brainshit.fr</ownerEmail>
</head>
<body>
<xsl:apply-templates select="//itsb:section[itsb:source]" />
</body>
</opml>
</xsl:template>
<xsl:template match="itsb:section">
<outline type="text" text="{@name}">
<xsl:apply-templates select=".//itsb:feed" />
</outline>
</xsl:template>
<xsl:template match="itsb:feed">
<xsl:element name="outline">
<xsl:attribute name="text">
<xsl:value-of select="@id" />
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="@id" />
</xsl:attribute>
<xsl:attribute name="description">
<xsl:value-of select="ancestor::itsb:source/itsb:name/text()" />
<xsl:if test="ancestor::itsb:source/itsb:region">
<xsl:text> (</xsl:text>
<xsl:value-of select="ancestor::itsb:source/itsb:region/text()" />
<xsl:text>)</xsl:text>
</xsl:if>
<xsl:text> - </xsl:text>
<xsl:choose>
<xsl:when test="@type">
<xsl:call-template name="capitalize">
<xsl:with-param name="text" select="@type" />
</xsl:call-template>
<xsl:text> - </xsl:text>
</xsl:when>
<xsl:when test="ancestor::itsb:source/itsb:type">
<xsl:value-of select="ancestor::itsb:source/itsb:type/text()" />
<xsl:text> - </xsl:text>
</xsl:when>
</xsl:choose>
<xsl:call-template name="capitalize">
<xsl:with-param name="text" select="@lang" />
</xsl:call-template>
</xsl:attribute>
<xsl:attribute name="language">
<xsl:value-of select="translate(@lang, $upper, $lower)" />
</xsl:attribute>
<xsl:attribute name="type">rss</xsl:attribute>
<xsl:attribute name="version">
<xsl:choose>
<xsl:when test="@format = 'rss'">
<xsl:text>RSS</xsl:text>
</xsl:when>
<xsl:when test="@format = 'atom'">
<xsl:text>ATOM</xsl:text>
</xsl:when>
<xsl:when test="@format = 'rdf'">
<xsl:text>RSS1</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="translate(@format, $lower, $upper)" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:attribute name="xmlUrl">
<xsl:choose>
<xsl:when test="./itsb:output">
<xsl:value-of select="$itsbRoot" />
<xsl:text>feeds/</xsl:text>
<xsl:value-of select="./itsb:output/text()" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="./itsb:link/text()" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>