How to convert a XML file based on attributes to an XML file based on child nodes by using XSLT.

I wanted to convert a XML based on attributes to a XML based on child nodes, e.g.:



<?xml version="1.0" encoding="utf-8" ?> <people> <person firstname="John" lastname="Smith"/> <person firstname="Michael" lastname="Johnson"/> <person firstname="Jacob" lastname="Williams"/> </people>




The forum post [] contains a XSLT script that will just do that.

<xsl:stylesheet version="1.0"
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:variable name="vNamespace" select="namespace-uri(/*)"/>

  <xsl:template match="node()|@*">
      <xsl:apply-templates select="node()|@*"/>

  <xsl:template match="*/*/@*">
    <xsl:element name="{name()}" namespace="{$vNamespace}">
      <xsl:value-of select="."/>

  <xsl:template match="x:Value">
    <xsl:apply-templates select="@*"/>

Transforming an input file to an output file in C#, by using XSLT.

Just a code snippet for transforming an input file to an output file in C# by using XSLT:

/// <summary>
/// Transform an input file to an output file, by using XSLT.
/// </summary>
public void ExecuteTransform()
    string xsltFilePath = @"C:\BDATA\Transform.xsl";
    string sourceFilePath = @"C:\BDATA\input.xml";
    string destinationFilePath = @"C:\BDATA\output.xml";

    var transform = new XslCompiledTransform();
    transform.Transform(sourceFilePath, destinationFilePath);

Generating a HTML link <A>, with href attribute

There are 2 ways to generate a HTML <A> link with XSLT:


Using the <xsl:element>

        <xsl:element name="a">
            <xsl:attribute name="href">
                <xsl:value-of select="url"/>
            <xsl:value-of select="url"/>

Using only text

<a href="{url}"><xsl:value-of select="url"/></a>


HTML link, that fires a JavaScript function with parameter

<xsl:element name="a">
   <xsl:attribute name="href">javascript:myFunction(<xsl:value-of select="@parameter1"/>);</xsl:attribute>
      My link text