Created
August 30, 2010 22:36
-
-
Save mcarneiro/558159 to your computer and use it in GitHub Desktop.
useful functions for xslt 1.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > | |
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes" /> | |
<!-- usage | |
<xsl:variable name="value"> | |
<xsl:call-template name="replace"> | |
<xsl:with-param name="value" select="{root}teste/index.html" /> | |
<xsl:with-param name="from" select="'{root}'" /> | |
<xsl:with-param name="to" select="http://www.carneiro.com/" /> | |
</xsl:call-template> | |
</xsl:variable> | |
variable will be "http://www.carneiro.com/teste/index.html" | |
--> | |
<xsl:template name="replace"> | |
<xsl:param name="value" /> | |
<xsl:param name="from" /> | |
<xsl:param name="to" /> | |
<xsl:variable name="before"> | |
<xsl:value-of select="substring-before($value, $from)" /> | |
</xsl:variable> | |
<xsl:variable name="after"> | |
<xsl:value-of select="substring-after($value, $from)" /> | |
</xsl:variable> | |
<xsl:variable name="replaced"> | |
<xsl:choose> | |
<xsl:when test="contains($value, $from)"> | |
<xsl:value-of disable-output-escaping="yes" select="concat(concat($before, $to), $after)" /> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of disable-output-escaping="yes" select="$value" /> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:variable> | |
<xsl:choose> | |
<xsl:when test="contains($replaced, $from)"> | |
<xsl:call-template name="replace"> | |
<xsl:with-param name="value" select="$replaced" /> | |
<xsl:with-param name="from" select="$from" /> | |
<xsl:with-param name="to" select="$to" /> | |
</xsl:call-template> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of disable-output-escaping="yes" select="$replaced" /> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:template> | |
</xsl:stylesheet> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > | |
<xsl:import href="split.xsl" /> | |
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes" /> | |
<!-- | |
needs split.xsl | |
usage | |
<xsl:variable name="var"> | |
<xsl:call-template name="sliceArray"> | |
<xsl:with-param name="value" select="teste.1.2.3" /> | |
<xsl:with-param name="by" select="'.'" /> | |
<xsl:with-param name="position" select="2" /> | |
</xsl:call-template> | |
</xsl:variable> | |
variable will be "teste.1" | |
<xsl:variable name="area"> | |
<xsl:call-template name="sliceArray"> | |
<xsl:with-param name="value" select="teste.1.2.3" /> | |
<xsl:with-param name="by" select="'.'" /> | |
<xsl:with-param name="position" select="-2" /> | |
</xsl:call-template> | |
</xsl:variable> | |
variable will be "2.3" | |
--> | |
<xsl:template name="sliceArray"> | |
<xsl:param name="value" /> | |
<xsl:param name="by" /> | |
<xsl:param name="position" /> | |
<xsl:choose> | |
<xsl:when test="$position = 0 or contains($value, $by) = False"> | |
<xsl:value-of disable-output-escaping="yes" select="$value" /> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:variable name="splitted"> | |
<xsl:call-template name="split"> | |
<xsl:with-param name="value" select="$value" /> | |
<xsl:with-param name="by" select="$by" /> | |
</xsl:call-template> | |
</xsl:variable> | |
<xsl:choose> | |
<xsl:when test="count(msxsl:node-set($splitted)/item) < 0"> | |
<xsl:value-of disable-output-escaping="yes" select="$value" /> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:for-each select="msxsl:node-set($splitted)/item"> | |
<xsl:choose> | |
<xsl:when test="($position < 0)"> | |
<xsl:choose> | |
<xsl:when test="position() = (last() + $position + 1)"> | |
<xsl:value-of disable-output-escaping="yes" select="." /> | |
</xsl:when> | |
<xsl:when test="position() > (last() + $position + 1)"> | |
<xsl:if test="position() != 1"> | |
<xsl:value-of select="$by" /> | |
</xsl:if> | |
<xsl:value-of disable-output-escaping="yes" select="." /> | |
</xsl:when> | |
</xsl:choose> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:choose> | |
<xsl:when test="position() = 1 and position() <= $position"> | |
<xsl:value-of disable-output-escaping="yes" select="." /> | |
</xsl:when> | |
<xsl:when test="position() <= $position"> | |
<xsl:value-of select="$by" /><xsl:value-of disable-output-escaping="yes" select="." /> | |
</xsl:when> | |
</xsl:choose> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:for-each> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:template> | |
</xsl:stylesheet> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes" /> | |
<!-- Usage | |
<xsl:variable name="splitted"> | |
<xsl:call-template name="split"> | |
<xsl:with-param name="value" select="$value" /> | |
<xsl:with-param name="by" select="$by" /> | |
</xsl:call-template> | |
</xsl:variable> | |
<xsl:for-each select="node-set($splitted)/item"> // remember that "node-set" is proprietary by parser | |
... | |
</xsl:for-each> | |
--> | |
<xsl:template name="split"> | |
<xsl:param name="value" /> | |
<xsl:param name="by" /> | |
<xsl:variable name="remains"> | |
<xsl:value-of select="substring-after($value, $by)" /> | |
</xsl:variable> | |
<item> | |
<xsl:choose> | |
<xsl:when test="contains($value, $by)"> | |
<xsl:value-of select="substring-before($value, $by)" /> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select="$value" /> | |
</xsl:otherwise> | |
</xsl:choose> | |
</item> | |
<xsl:if test="$remains != ''"> | |
<xsl:call-template name="split"> | |
<xsl:with-param name="value" select="$remains" /> | |
<xsl:with-param name="by" select="$by" /> | |
</xsl:call-template> | |
</xsl:if> | |
</xsl:template> | |
</xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment