Using XSLT to copy all nodes in XML, with support for special cases

<?xml version=”1.0″?> <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <!–Identity template, provides default behavior that copies all content into the output –> <xsl:template match=”@*|node()”> <xsl:copy> <xsl:apply-templates select=”@*|node()”/> </xsl:copy> </xsl:template> <!–More specific template for Node766 that provides custom behavior –> <xsl:template match=”Node766″> <xsl:copy> <xsl:apply-templates select=”@*|node()”/> <!–Do something special for Node766, like add a certain string–> <xsl:text> add some text </xsl:text> … Read more

How to check if an attribute exists in a XML file using XSL

Here is a very simple way to do conditional processing using the full power of XSLT pattern matching and exclusively “push” style, and this even avoids the need to use conditional instructions such as <xsl:if> or <xsl:choose>: <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=”/root/diagram[graph[1]/@color]”> Graph[1] has color </xsl:template> <xsl:template match=”/root/diagram[not(graph[1]/@color)]”> Graph[1] has not color … Read more

Passing parameters to XSLT Stylesheet via .NET

You need to define the parameter within your XSLT and you also need to pass the XsltArgumentList as an argument to the Transform call: private static void CreateHierarchy(string manID) { string man_ID = manID; XsltArgumentList argsList = new XsltArgumentList(); argsList.AddParam(“Boss_ID”, “”, man_ID); XslCompiledTransform transform = new XslCompiledTransform(true); transform.Load(“htransform.xslt”); using (StreamWriter sw = new StreamWriter(“output.xml”)) { … Read more

Which version of XPATH and XSLT am I using..?

In XSLT, call system-property(‘xsl:version’). It will return 1.0 or 2.0 depending on whether you are using a 1.0 or 2.0 processor. In XPath, there’s no direct equivalent. But a quick test is to call current-date() with no arguments. If this succeeds, you have a 2.0 processor, if it fails, you have a 1.0 processor. Unless … Read more

Performance: call-template vs apply-template

As with all performance questions, the answer will depend on your particular configuration (in particular the XSLT processor you’re using) and the kind of processing that you’re doing. <xsl:apply-templates> takes a sequence of nodes and goes through them one by one. For each, it locates the template with the highest priority that matches the node, … Read more

How can I select the first element using XSLT?

If you wish to output XHTML 1.1, here’s one way: <?xml version=”1.0″?> <xsl:transform version=”2.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:xs=”http://www.w3.org/2001/XMLSchema” exclude-result-prefixes=”xsl xs”> <xsl:output mode=”xhtml” version=”1.1″ omit-xml-declaration=”yes” encoding=”utf-8″ media-type=”application/xhtml+xml” indent=”no” doctype-public=”-//W3C//DTD XHTML 1.1//EN” doctype-system=”http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd” /> <xsl:template match=”//newsItem[1]”> <div><xsl:value-of select=”dateCreated”/></div> <div><xsl:value-of select=”summary”/></div> </xsl:template> </xsl:transform>

How to display   in XML output

Unless your XML has a DTD which says what &nbsp; means, you cannot use &nbsp;. The only reason this works in HTML is because the XHTML DTD defines what it means (for XHTML) or it’s just baked in to the parser (HTML). In general you should not use named character entities in XML because such … Read more

Increment a value in XSLT

XSLT is a functional language and among other things this means that variables in XSLT are immutable and once they have been defined their value cannot be changed. Here is how the same effect can be achieved in XSLT: <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=”https://stackoverflow.com/”> <posts> <xsl:for-each select=”data/posts/entry”> <xsl:variable name=”i” select=”position()” … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)