How to convert json to xml using xslt

My work on JSON parsing doesn’t cover the full JSON grammar. And the task of “translating” any JSON document to an XML document doesn’t have a solution. There are JSON constructs, which cannot be translated to XML without defining additional conventions and introducing additional elements — so the final XML structure isn’t a true and … Read more

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

How to iterate over IDREFS values in XSLT 1.0?

You need to tokenize the value of the idrefsField attribute. XSLT 1.0 has no native tokenize() function, so you need to call a recursive named template to do this for you: <xsl:template match=”node_With_IDREFS_field”> <xsl:copy> <xsl:call-template name=”tokenize”> <xsl:with-param name=”text” select=”@idrefsField”/> </xsl:call-template> </xsl:copy> </xsl:template> <xsl:template name=”tokenize”> <xsl:param name=”text”/> <xsl:param name=”delimiter” select=”‘ ‘”/> <xsl:variable name=”token” select=”substring-before(concat($text, $delimiter), $delimiter)” … 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

xslt 1.0 string replace function

A very simple solution (that will work as long as your string value doesn’t have spaces): translate(normalize-space(translate(‘aa::bb::cc’,’:’,’ ‘)),’ ‘,’,’) translate “:’ into ” “ normalize-space() to collapse multiple whitespace characters into a single space ” “ translate single spaces ” ” into “,” A more robust solution would be to use a recursive template: <xsl:template … 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

XSLT string replace

replace isn’t available for XSLT 1.0. Codesling has a template for string-replace you can use as a substitute for the function: <xsl:template name=”string-replace-all”> <xsl:param name=”text” /> <xsl:param name=”replace” /> <xsl:param name=”by” /> <xsl:choose> <xsl:when test=”$text=”” or $replace=””or not($replace)” > <!– Prevent this routine from hanging –> <xsl:value-of select=”$text” /> </xsl:when> <xsl:when test=”contains($text, $replace)”> <xsl:value-of select=”substring-before($text,$replace)” … Read more