XSLT: How to change an attribute value during ?

This problem has a classical solution: Using and overriding the identity template is one of the most fundamental and powerful XSLT design patterns: <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:output omit-xml-declaration=”yes” indent=”yes”/> <xsl:param name=”pNewType” select=”‘myNewType'”/> <xsl:template match=”node()|@*”> <xsl:copy> <xsl:apply-templates select=”node()|@*”/> </xsl:copy> </xsl:template> <xsl:template match=”property/@type”> <xsl:attribute name=”type”> <xsl:value-of select=”$pNewType”/> </xsl:attribute> </xsl:template> </xsl:stylesheet> When applied on this XML document: <t> … Read more

Check if a string is null or empty in XSLT

test=”categoryName != ”” Edit: This covers the most likely interpretation, in my opinion, of “[not] null or empty” as inferred from the question, including it’s pseudo-code and my own early experience with XSLT. I.e., “What is the equivalent of the following Java?”: // Equivalent Java, NOT XSLT !(categoryName == null || categoryName.equals(“”)) For more details … Read more

xsl: how to split strings?

I. Plain XSLT 1.0 solution: This transformation: <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:output omit-xml-declaration=”yes” indent=”yes”/> <xsl:template match=”text()” name=”split”> <xsl:param name=”pText” select=”.”/> <xsl:if test=”string-length($pText)”> <xsl:if test=”not($pText=.)”> <br /> </xsl:if> <xsl:value-of select= “substring-before(concat($pText,’;’),’;’)”/> <xsl:call-template name=”split”> <xsl:with-param name=”pText” select= “substring-after($pText, ‘;’)”/> </xsl:call-template> </xsl:if> </xsl:template> </xsl:stylesheet> when applied on this XML document: <t>123 Elm Street;PO Box 222;c/o James Jones</t> produces the … Read more

How to remove elements from xml using xslt with stylesheet and xsltproc?

Using one of the most fundamental XSLT design patterns: “Overriding the identity transformation” one will just write the following: <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:output omit-xml-declaration=”yes”/> <xsl:template match=”node()|@*”> <xsl:copy> <xsl:apply-templates select=”node()|@*”/> </xsl:copy> </xsl:template> <xsl:template match=”Element[@fruit=”apple” and @animal=”cat”]”/> </xsl:stylesheet> Do note how the second template overrides the identity (1st) template only for elements named “Element” that have an … Read more