Skip to content

Instantly share code, notes, and snippets.

@xpathr
Created May 10, 2012 14:40
Show Gist options
  • Select an option

  • Save xpathr/2653487 to your computer and use it in GitHub Desktop.

Select an option

Save xpathr/2653487 to your computer and use it in GitHub Desktop.
Alternate Text Attribute Splitter by tonyarnold
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:template mode="html-filter" match="img/@alt | a/@title">
<xsl:variable name="keys-and-values" select="str:split(., ';; ')"/>
<xsl:variable name="attribute-local-name" select="local-name()"/>
<xsl:for-each select="$keys-and-values">
<xsl:choose>
<xsl:when test="contains(., ':')">
<xsl:attribute name="{substring-before(., ':')}">
<xsl:value-of select="substring-after(., ':')"/>
</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="{$attribute-local-name}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template mode="html-filter" match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template mode="html-filter" match="text()">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template mode="html-filter" match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates mode="html-filter" select="@*" />
<xsl:apply-templates mode="html-filter" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment