XSLT - Add / Change element name

Change element name

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
  Change element name:
    -While copying everything:
      -Catch the element you want to change.
      -Reconstruct the element you catched under another name.
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
 
    <!-- Copy everything -->
    <!-- ############# -->
    <xsl:template match="@*|node()">
      <xsl:copy>

XSLT - Sample Template Code

Sample XSLT Template Code

XSLT - Removing leading zeros

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" encoding="UTF-8" />
  <!-- 
    Usage example: Removing leading zeros 
  -->
  <xsl:template match="*">
    <xsl:param name="RemoveLeadingZerosResult">
      <xsl:call-template name="RemoveLeadingZeros">
        <xsl:with-param name="sTmp" select="00001"/>
      </xsl:call-template>   
    </xsl:param>
    <xsl:value-of select="$RemoveLeadingZerosResult"/><!-- Display the result -->
  </xsl:template>
 
  <!-- 

XSLT - Raise to power

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" encoding="UTF-8" />
 
  <!-- Usage example: Calculate 10^5-->
  <xsl:template match="*">
    <xsl:param name="PowResult">
      <xsl:call-template name="Pow">
        <xsl:with-param name="Base" select="10"/>
        <xsl:with-param name="Exponent" select="5"/>
        <xsl:with-param name="Result" select="1"/><!-- Always set to 1 when Pow() function is used. -->
      </xsl:call-template>
    </xsl:param>

XSLT - Change letters to upper or lower cases

<?xml version="1.0" encoding="UTF-8"?>
<!--

XSLT - Custom Functions

Custom XSL functions

XSLT

XSLT reference

Useful identity templates

String funciton string-length()

Output xml entities as is

<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>

Handling CDATA XSL can't copy CDATA section as CDATA section. http://sources.redhat.com/ml/xsl-list/2001-05/msg00980.html

XPath's axes http://www.w3schools.com/XPath/xpath_axes.asp

Useful XPath samples  

TestNG - Testing private methods

Sometimes you need to test the private method of a class. I use reflection to access the private method. Here are the codes:

/**
 * Class with a private method that you want to test.
 * @author Xuan Ngo
 *
 */
public class ClassExample
{
  private String privateFoo(int a, String b)
  {
    return a+b;
  }
}
/**
 * Here is the code showing how to access the private method
 *  using reflection.
 * @author Xuan Ngo
 */
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

SQL - Hibernate

In Eclipse, include the following JARs:

  • hibernate-distribution-3.3.2.GA/hibernate3.jar
  • hibernate-distribution-3.3.2.GA/required/antlr-2.7.6.jar
  • hibernate-distribution-3.3.2.GA/required/commons-collections-3.1.jar
  • hibernate-distribution-3.3.2.GA/required/dom4j-1.6.1.jar
  • hibernate-distribution-3.3.2.GA/required/javassist-3.9.0.GA.jar
  • hibernate-distribution-3.3.2.GA/required/jta-1.1.jar
  • hibernate-distribution-3.3.2.GA/required/slf4j-api-1.5.8.jar

SQL

Basic

Create a table
--Create a table called t1 which has 2 columns, col1 and col2. 
--col1 is defined to allow only 1 characters and col2 is defined to allow integer.
CREATE TABLE t1( col1 CHAR(1), col2 INT);
Insert data in table
INSERT INTO t1 VALUES('A', 123); -- Insert 'A' and 123 into table t1.
INSERT INTO t1 VALUES('Z', 23 ); -- Insert 'Z' and 23 into table t1.
INSERT INTO t1 VALUES('B', 23 ); -- Insert 'Z' and 23 into table t1.

Pages

Subscribe to OpenWritings.net RSS