Skip to content

Commit

Permalink
2024.46.2
Browse files Browse the repository at this point in the history
- Added TSV XSLT
- Combined table separation into XSL
  • Loading branch information
simon-techkid committed May 25, 2024
1 parent 7291127 commit 490212f
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 108 deletions.
112 changes: 4 additions & 108 deletions SpotifyGPX/Xslt/csv.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -2,115 +2,11 @@
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:include href="extensions.xsl"/>
<xsl:include href="tableextensions.xsl"/>
<xsl:include href="separatedvaluestable.xsl"/>
<xsl:output method="html" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes" media-type="text/html"/>

<!-- Template for the root element -->
<xsl:template match="/">
<xsl:variable name="type" select="'Pairs Table'"/>
<xsl:variable name="header" select="$type"/>

<xsl:call-template name="doctype"/>
<html>
<xsl:call-template name="html_head_template">
<xsl:with-param name="title" select="$header"/>
</xsl:call-template>
<xsl:call-template name="html_body_template">
<xsl:with-param name="header" select="$header"/>
<xsl:with-param name="lines" select="//Line"/>
</xsl:call-template>
</html>
</xsl:template>

<xsl:template name="html_body_template">
<xsl:param name="header" />
<xsl:param name="lines" />
<body>
<h1><xsl:value-of select="$header"/></h1>
<hr/>
<xsl:call-template name="table">
<xsl:with-param name="lines" select="$lines"/>
</xsl:call-template>
<hr/>
</body>
</xsl:template>

<xsl:template name="table">
<xsl:param name="lines" />
<table>
<!-- Apply template to process header line -->
<tr>
<xsl:call-template name="processLine">
<xsl:with-param name="line" select="$lines[1]"/>
<xsl:with-param name="isHeader" select="true()"/>
</xsl:call-template>
</tr>
<!-- Apply templates to process non-header lines -->
<xsl:apply-templates select="//Line[position() > 1]"/>
</table>
</xsl:template>

<!-- Template to process each line -->
<xsl:template match="Line">
<tr>
<!-- Call template to process the line -->
<xsl:call-template name="processLine">
<xsl:with-param name="line" select="."/>
<xsl:with-param name="isHeader" select="false()"/>
</xsl:call-template>
</tr>
</xsl:template>

<!-- Template to split the contents of a line -->
<xsl:template name="processLine">
<xsl:param name="line"/>
<xsl:param name="isHeader"/>
<!-- Split the line by the delimiter -->
<xsl:call-template name="split">
<xsl:with-param name="string" select="$line"/>
<xsl:with-param name="isHeader" select="$isHeader"/>
</xsl:call-template>
</xsl:template>

<!-- Template to split a string by a delimiter -->
<xsl:template name="split">
<xsl:param name="string"/>
<xsl:param name="isHeader"/>
<xsl:choose>
<!-- If the string contains quotes -->
<xsl:when test="contains($string, '&quot;')">
<xsl:variable name="value" select="substring-before(substring-after($string, '&quot;'), '&quot;')"/>
<!-- If it's a header, create a th element, else create a td element -->
<xsl:choose>
<xsl:when test="$isHeader">
<xsl:call-template name="header">
<xsl:with-param name="value" select="$value"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="row">
<xsl:with-param name="value" select="$value"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
<!-- Recursively call split template with the remaining string -->
<xsl:call-template name="split">
<xsl:with-param name="string" select="substring-after(substring-after($string, '&quot;'), '&quot;')"/>
<xsl:with-param name="isHeader" select="$isHeader"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>

<!-- Template to process split values as table headers -->
<xsl:template name="header">
<xsl:param name="value"/>
<th><xsl:value-of select="$value"/></th>
</xsl:template>

<!-- Template to process split values as table rows -->
<xsl:template name="row">
<xsl:param name="value"/>
<td><xsl:value-of select="$value"/></td>
</xsl:template>
<!-- TSV column delimiter -->
<xsl:variable name="delimiter" select="'&quot;&#44;&quot;'"/>

</xsl:stylesheet>
106 changes: 106 additions & 0 deletions SpotifyGPX/Xslt/separatedvaluestable.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Template for the root element -->
<xsl:template match="/">
<xsl:variable name="type" select="'Pairs Table'"/>
<xsl:variable name="header" select="$type"/>

<xsl:call-template name="doctype"/>
<html>
<xsl:call-template name="html_head_template">
<xsl:with-param name="title" select="$header"/>
</xsl:call-template>
<xsl:call-template name="html_body_template">
<xsl:with-param name="header" select="$header"/>
<xsl:with-param name="lines" select="//Line"/>
</xsl:call-template>
</html>
</xsl:template>

<!-- Template for creating the HTML body -->
<xsl:template name="html_body_template">
<xsl:param name="header"/>
<xsl:param name="lines"/>
<body>
<h1><xsl:value-of select="$header"/></h1>
<hr/>
<xsl:call-template name="table">
<xsl:with-param name="lines" select="$lines"/>
</xsl:call-template>
<hr/>
</body>
</xsl:template>

<!-- Template for creating the table and underlying rows -->
<xsl:template name="table">
<xsl:param name="lines"/>
<table>
<!-- Apply template to process header line -->
<tr>
<xsl:call-template name="processLine">
<xsl:with-param name="line" select="$lines[1]"/>
<xsl:with-param name="isHeader" select="true()"/>
</xsl:call-template>
</tr>
<!-- Apply templates to process non-header lines -->
<xsl:apply-templates select="$lines[position() > 1]"/>
</table>
</xsl:template>

<!-- Template to process each line -->
<xsl:template match="Line">
<tr>
<!-- Call template to process the line -->
<xsl:call-template name="processLine">
<xsl:with-param name="line" select="."/>
<xsl:with-param name="isHeader" select="false()"/>
</xsl:call-template>
</tr>
</xsl:template>

<!-- Template to split the contents of a line -->
<xsl:template name="processLine">
<xsl:param name="line"/>
<xsl:param name="isHeader"/>
<!-- Split the line by the delimiter -->
<xsl:call-template name="split">
<xsl:with-param name="string" select="$line"/>
<xsl:with-param name="isHeader" select="$isHeader"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="split">
<xsl:param name="string" />
<xsl:param name="isHeader"/>
<xsl:choose>
<!-- If the string contains a delimiter character -->
<xsl:when test="contains($string, $delimiter)">
<!-- Extract the value before the delimiter character -->
<xsl:variable name="value" select="substring-before($string, $delimiter)" />

<!-- Process the extracted value -->
<xsl:call-template name="process-value">
<xsl:with-param name="isHeader" select="$isHeader"/>
<xsl:with-param name="value" select="$value" />
</xsl:call-template>

<!-- Recursively call split template with the remaining string -->
<xsl:variable name="remaining" select="substring-after($string, $delimiter)" />
<xsl:call-template name="split">
<xsl:with-param name="string" select="$remaining"/>
<xsl:with-param name="isHeader" select="$isHeader"/>
</xsl:call-template>
</xsl:when>
<!-- If no delimiter character is found -->
<xsl:otherwise>
<!-- Process the remaining part of the string -->
<xsl:call-template name="process-value">
<xsl:with-param name="isHeader" select="$isHeader"/>
<xsl:with-param name="value" select="$string" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>
66 changes: 66 additions & 0 deletions SpotifyGPX/Xslt/tableextensions.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Should encapsulating quotes be removed from table cells? -->
<xsl:variable name="removeQuotes" select="true()"/>

<!-- What does an encapsulating quote look like? -->
<xsl:variable name="quotes" select="'&quot;'"/>

<!-- Template to create table headers or table rows and remove encapsulating quotes -->
<xsl:template name="process-value">
<xsl:param name="value"/>
<xsl:param name="isHeader"/>

<xsl:variable name="string">
<xsl:choose>
<xsl:when test="$removeQuotes = true()">
<xsl:call-template name="removeQuotes">
<xsl:with-param name="string" select="$value"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="$removeQuotes = false()">
<xsl:value-of select="$value"/>
</xsl:when>
</xsl:choose>
</xsl:variable>

<xsl:choose>
<xsl:when test="$isHeader = true()">
<th><xsl:value-of select="$string"/></th>
</xsl:when>
<xsl:when test="$isHeader = false()">
<td><xsl:value-of select="$string"/></td>
</xsl:when>
</xsl:choose>
</xsl:template>

<!-- Template to remove encapsulating quotes from a given string -->
<xsl:template name="removeQuotes">
<xsl:param name="string"/>
<xsl:variable name="startQuote" select="substring($string, 1, 1) = $quotes"/>
<xsl:variable name="endQuote" select="substring($string, string-length($string), 1) = $quotes"/>
<xsl:variable name="startRemoved">
<xsl:choose>
<xsl:when test="$startQuote">
<xsl:value-of select="substring($string, 2)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="bothRemoved">
<xsl:choose>
<xsl:when test="$endQuote">
<xsl:value-of select="substring($startRemoved, 1, string-length($startRemoved) - 1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$startRemoved"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$bothRemoved"/>
</xsl:template>

</xsl:stylesheet>
12 changes: 12 additions & 0 deletions SpotifyGPX/Xslt/tsv.xslt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:include href="extensions.xsl"/>
<xsl:include href="tableextensions.xsl"/>
<xsl:include href="separatedvaluestable.xsl"/>
<xsl:output method="html" version="1.0" encoding="UTF-8" omit-xml-declaration="no" indent="yes" media-type="text/html"/>

<!-- TSV column delimiter -->
<xsl:variable name="delimiter" select="'&#9;'"/>

</xsl:stylesheet>

0 comments on commit 490212f

Please sign in to comment.