Skip to content

Instantly share code, notes, and snippets.

@netsensei
Created August 27, 2015 20:57
Show Gist options
  • Save netsensei/0fe7f98b70ff01eae853 to your computer and use it in GitHub Desktop.
Save netsensei/0fe7f98b70ff01eae853 to your computer and use it in GitHub Desktop.
Adlib XML to CSV XSLT.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:variable name="delimiter" select="';'" />
<!-- define an array containing the fields we are interested in -->
<xsl:variable name="fieldArray">
<field>Production</field>
<field>object_number</field>
<field>Title</field>
</xsl:variable>
<xsl:param name="fields" select="document('')/*/xsl:variable[@name='fieldArray']/*" />
<xsl:template match="/">
<!-- output the header row -->
<xsl:for-each select="$fields">
<xsl:if test="position() != 1">
<xsl:value-of select="$delimiter"/>
</xsl:if>
<xsl:value-of select="." />
</xsl:for-each>
<!-- output newline -->
<xsl:text>&#xa;</xsl:text>
<xsl:apply-templates select="adlibXML/recordList/record"/>
</xsl:template>
<xsl:template match="record">
<xsl:variable name="currNode" select="." />
<!-- output the data row -->
<!-- loop over the field names and find the value of each one in the xml -->
<xsl:for-each select="$fields">
<xsl:if test="position() != 1">
<xsl:value-of select="$delimiter"/>
</xsl:if>
<xsl:text>"</xsl:text>
<xsl:variable name="child" select="$currNode/*[name() = current()]/*[1]" />
<xsl:choose>
<xsl:when test="count($child) > 0">
<xsl:value-of select="$child"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$currNode/*[name() = current()]" />
</xsl:otherwise>
</xsl:choose>
<xsl:text>"</xsl:text>
</xsl:for-each>
<!-- output newline -->
<xsl:text>&#xa;</xsl:text>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment