XSLT Bitwise Logic

XSLT is Turing-complete, see for example here or here, hence it can be done. But I have used XSLT only one or two times and can give no solution. UPDATE I just read a tutorial again and found a solution using the following fact. bitset(x, n) returns true, if the n-th bit of x is … Read more

Update the text of an element with XSLT based on param

There are a number of problems with the provided code which lead to compile-time errors: <xsl:template match=”/foo/bar/”> <xsl:param name=”baz” value=”something different”/> <xsl:value-of select=”$baz”/> </xsl:template> The match pattern specified on this template is syntactically illegal — an XPath expression cannot end with the / character. xsl:param cannot have an unknown attribute such as value Solution: <xsl:stylesheet … Read more

adding attribute to the node

Part 1. So let’s say if the country id is equal to 32 then it should add attribute country=32 to Employee node. 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:strip-space elements=”*”/> <xsl:template match=”node()|@*”> <xsl:copy> <xsl:apply-templates select=”node()|@*”/> </xsl:copy> </xsl:template> <xsl:template match=”Employee[countryid=32]”> <Employee countryid=”{countryid}”> <xsl:apply-templates select=”@*|node()”/> </Employee> </xsl:template> </xsl:stylesheet> when applied on the provided XML document: … Read more

Removing empty tags from XML via XSLT

This transformation doesn’t need any conditional XSLT instructions at all and uses no explicit priorities: <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:output omit-xml-declaration=”yes” indent=”yes”/> <xsl:strip-space elements=”*”/> <xsl:template match=”node()|@*”> <xsl:copy> <xsl:apply-templates select=”node()|@*”/> </xsl:copy> </xsl:template> <xsl:template match= “*[not(@*|*|comment()|processing-instruction()) and normalize-space()=” ]”/> </xsl:stylesheet> When applied on the provided XML document: <Person> <FirstName>Ahmed</FirstName> <MiddleName/> <LastName>Aboulnaga</LastName> <CompanyInfo> <CompanyName>IPN Web</CompanyName> <Title/> <Role></Role> <Department> </Department> … Read more

How-to break a for-each loop in XSLT?

XSLT is written in a very functional style, and in this style there is no equivalent of a break statement. What you can do is something like this: <xsl:for-each select=”…nodes…”> <xsl:if test=”…some condition…”> …body of loop… </xsl:if> </xsl:for-each> That way the for-each will still iterate through all the nodes, but the body of the loop … Read more

Inserting a line break in a PDF generated from XSL FO using

You could also replace <br/> with &#xA; and add a linefeed-treatment=”preserve” attribute to your <fo:block>. Something like: <fo:block linefeed-treatment=”preserve”>This is an example Description.&#xA;List item 1&#xA;List item 2&#xA;List item 3&#xA;List item 4</fo:block> Edit Some users may need to use \n instead of &#xA; depending on how they are creating the XML. See Retain the &#xA; during … Read more

How to concat a string to xsl:value-of select=”…?

You can use the rather sensibly named xpath function called concat here <a> <xsl:attribute name=”href”> <xsl:value-of select=”concat(‘myText:’, /*/properties/property[@name=”report”]/@value)” /> </xsl:attribute> </a> Of course, it doesn’t have to be text here, it can be another xpath expression to select an element or attribute. And you can have any number of arguments in the concat expression. Do … Read more

How to use starts-with() , contains() and ends-with() in XPath to find the xml node innertext? in XPATH 1.0

One possible way: //Heading[starts-with(., ‘Ethical’) and ends-with(., ‘consent’)] The ends-with() function is XPath 2.0. In XPath 1.0, it can be replaced using substring() and string-length(). Here is the equivalent XPath 1.0 (wrapped for readability): //Heading[ starts-with(., ‘Ethical’) and ‘consent’ = substring(., string-length(.) – string-length(‘consent’) +1) ]

Create node set and pass as a parameter

There is a way (non-standard) in XSLT 1.0 to create temporary trees dynamically and evaluate XPath expressions on them, however this requires using the xxx:node-set() function. Whenever nodes are dynamically created inside the body of an xsl:variable or an xsl:param, the type of that xsl:variable / xsl:param is RTF (Result Tree Fragment) and the W3 … Read more

Adding element in middle of xml using xslt

Here is an XSLT 1.0 stylesheet that will do what you asked: <?xml version=”1.0″ encoding=”UTF-8″?> <xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0″> <!– Identity transform –> <xsl:template match=”@* | node()”> <xsl:copy> <xsl:apply-templates select=”@* | node()”/> </xsl:copy> </xsl:template> <xsl:template match=”Name”> <xsl:copy-of select=”.”/> <Age>34</Age> </xsl:template> <xsl:template match=”Dept”> <xsl:copy-of select=”.”/> <Domain>Insurance</Domain> </xsl:template> </xsl:stylesheet> Obviously the logic will vary depending on where you … Read more