Archive

Archive for the ‘XSLT’ Category

XSLT 1.0 – Grouping XML Elements

January 8, 2013 1 comment

Here I have given a XSLT sample to group XML elements.

Request XML

<?xml version="1.0" encoding="UTF-8"?>
<Order>
<Bundle>
    <authors>
        <author>
            <authorID>100</authorID>
            <authorName>Kathisiera</authorName>
        </author>
        <author>
            <authorID>200</authorID>
            <authorName>Bates</authorName>
        </author>
        <author>
            <authorID>300</authorID>
            <authorName>Gavin King</authorName>
        </author>
    </authors>
    <books>
        <book>
            <orderID>1111</orderID>
            <bookName>Head First Java</bookName>
            <bookAuthorID>100</bookAuthorID>
        </book>
        <book>
            <orderID>5555</orderID>
            <bookName>Head First Servlets</bookName>
            <bookAuthorID>200</bookAuthorID>
        </book>
        <book>
            <orderID>1111</orderID>
            <bookName>Hibernate In Action</bookName>
            <bookAuthorID>300</bookAuthorID>
        </book>
    </books>
</Bundle>
</Order>

Here we have to separate the Order bundle based on the book orderID.
Have to convert the request xml like the below to fit the request structure of external system.

Expected Output
  

<?xml version="1.0" encoding="UTF-8"?>
<Order>
	<Bundle>
		<authors>
			<author>
				<authorID>100</authorID>
				<authorName>Kathisiera</authorName>
			</author>
			<author>
				<authorID>300</authorID>
				<authorName>Gavin King</authorName>
			</author>
		</authors>
		<books>
			<book>
				<orderID>1111</orderID>
				<bookName>Head First Java</bookName>
				<bookAuthorID>100</bookAuthorID>
			</book>
			<book>
				<orderID>1111</orderID>
				<bookName>Hibernate In Action</bookName>
				<bookAuthorID>300</bookAuthorID>
			</book>
		</books>
	</Bundle>
	<Bundle>
		<authors>
			<author>
				<authorID>200</authorID>
				<authorName>Bates</authorName>
			</author>
		</authors>
		<books>
			<book>
				<orderID>5555</orderID>
				<bookName>Head First Servlets</bookName>
				<bookAuthorID>200</bookAuthorID>
			</book>
		</books>
	</Bundle>
</Order>

Used key() and generate-id() function in XSL to transform the input xml into above Expected Output structure.

XSL Transformation tp group xml elements
 

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes" />

	<xsl:key name="k" match="book" use="orderID" />
	<xsl:key name="a" match="author" use="authorID" />

	<xsl:template match="/Order">
		<xsl:copy>
			<xsl:apply-templates select="//books" />
		</xsl:copy>
	</xsl:template>

	<xsl:template match="books">
		<xsl:apply-templates select="book[generate-id(.) = generate-id(key('k', orderID))]" />
	</xsl:template>

	<xsl:template match="book">
		<Bundle>
			<authors>
				<xsl:apply-templates select="key('a', key('k', orderID)/bookAuthorID)" />
			</authors>

			<books>
				<xsl:copy-of select="key('k', orderID)" />
			</books>
		</Bundle>
	</xsl:template>

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

XSLT value-of sample

December 10, 2011 Leave a comment

XSL value-of function used to retrive the value from the input xml file using XPATH expressions.

To see the result , open the XML file in browser there we have included the XSLT file to format the data from XML file based on value-of function.

XSL file “XSL_Value-of_Fn_Test.xsl” :

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>Employee Detail</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Employee Id</th>
      <th>Name</th>
      <th>Designation</th>
    </tr>
    <tr>
      <td><xsl:value-of select="company/employee/empId"/></td>
      <td><xsl:value-of select="company/employee/firstname"/></td>
      <td><xsl:value-of select="company/employee/designation"/></td>
    </tr>
  </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet> 

XML file :

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="XSL_Value-of_Fn_Test.xsl"?>
<company>
	<employee>
		<empId>1212</empId>
		<firstname>Tom</firstname>
		<lastname>Cruise</lastname>
		<designation>Project Manager</designation>
		<age>35</age>
		<gender>Male</gender>
	</employee>
</company>

XSLT choose sample

December 10, 2011 Leave a comment

In this post we will see how to use XSLT choose example.

To see the result , open the XML file in browser there we have included the XSLT file to format the data from XML file based on choose condition implemented.

XSL file “XSL_choose_Fn_Test.xsl”:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>Employee Details</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Employee Id</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Designation</th>
    </tr>
<xsl:for-each select="company/employee">
    <xsl:choose>
        <xsl:when test="gender!='Male'">
    	  <tr>
	      <td><xsl:value-of select="empId"/></td>
	      <td><xsl:value-of select="firstname"/></td>
	      <td><xsl:value-of select="lastname"/></td>
	      <td><xsl:value-of select="designation"/></td>
	 </tr>
	</xsl:when>
	<xsl:otherwise>
    	  <!--<tr>
	      <td><xsl:value-of select="empId"/></td>
	      <td><xsl:value-of select="firstname"/></td>
	      <td><xsl:value-of select="lastname"/></td>
	      <td><xsl:value-of select="designation"/></td>
	 </tr>-->
	</xsl:otherwise>
	</xsl:choose>
</xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

XML file:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="XSL_choose_Fn_Test.xsl"?>
<company>
	<employee>
		<empId>1212</empId>
		<firstname>Tom</firstname>
		<lastname>Cruise</lastname>
		<designation>Project Manager</designation>
		<age>35</age>
		<gender>Male</gender>
	</employee>
	<employee>
		<empId>2383</empId>
		<firstname>Selva</firstname>
		<lastname>Kumar</lastname>
		<designation>Systems Engineer</designation>
		<age>29</age>
		<gender>Male</gender>
	</employee>
	<employee>
		<empId>2171</empId>
		<firstname>Swarna</firstname>
		<lastname>Reddy</lastname>
		<designation>Assistant Consultant</designation>
		<age>39</age>
		<gender>Female</gender>
	</employee>
	<employee>
		<empId>1111</empId>
		<firstname>Renuga</firstname>
		<lastname>Devi</lastname>
		<designation>Analyst</designation>
		<age>25</age>
		<gender>Female</gender>
	</employee>
	<employee>
		<empId>4211</empId>
		<firstname>Krishna</firstname>
		<lastname>Kumari</lastname>
		<designation>Systems Engineer</designation>
		<age>24</age>
		<gender>Female</gender>
	</employee>
	<employee>
		<empId>3571</empId>
		<firstname>Rama</firstname>
		<lastname>Raju</lastname>
		<designation>Business Analyst</designation>
		<age>31</age>
		<gender>Male</gender>
	</employee>
</company>

XSLT for-each Sample

December 10, 2011 Leave a comment

In this post we will see how to iterate the repeating elements from input XML file using XSLT for-each.

To see the result , open the XML file in browser there we have included the XSLT file to format the data from XML file based on for-each condition implemented.

XSL file “XSL_for-each_Fn_Test.xsl”
:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>Employee Details</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Employee Id</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Designation</th>
    </tr>
    <xsl:for-each select="company/employee">
    <tr>
      <td><xsl:value-of select="empId"/></td>
      <td><xsl:value-of select="firstname"/></td>
      <td><xsl:value-of select="lastname"/></td>
      <td><xsl:value-of select="designation"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

XML file :

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="XSL_for-each_Fn_Test.xsl"?>
<company>
	<employee>
		<empId>1212</empId>
		<firstname>Tom</firstname>
		<lastname>Cruise</lastname>
		<designation>Project Manager</designation>
		<age>35</age>
		<gender>Male</gender>
	</employee>
	<employee>
		<empId>2383</empId>
		<firstname>Selva</firstname>
		<lastname>Kumar</lastname>
		<designation>Systems Engineer</designation>
		<age>29</age>
		<gender>Male</gender>
	</employee>
	<employee>
		<empId>2171</empId>
		<firstname>Swarna</firstname>
		<lastname>Reddy</lastname>
		<designation>Assistant Consultant</designation>
		<age>39</age>
		<gender>Female</gender>
	</employee>
	<employee>
		<empId>1111</empId>
		<firstname>Renuga</firstname>
		<lastname>Devi</lastname>
		<designation>Analyst</designation>
		<age>25</age>
		<gender>Female</gender>
	</employee>
	<employee>
		<empId>4211</empId>
		<firstname>Krishna</firstname>
		<lastname>Kumari</lastname>
		<designation>Systems Engineer</designation>
		<age>24</age>
		<gender>Female</gender>
	</employee>
	<employee>
		<empId>3571</empId>
		<firstname>Rama</firstname>
		<lastname>Raju</lastname>
		<designation>Business Analyst</designation>
		<age>31</age>
		<gender>Male</gender>
	</employee>
</company>

XSLT If Sample

December 10, 2011 Leave a comment

In this post we will see how to use If condition in XSLT.

Here I have coded the XSLT to display the result in browser. To see the result ,open the XML file in browser there we have included the XSLT file to format the data from XML file based on the conditions implemented.

This XSL file will print the employee details sorted based on empid whose age is less than 30.

XSL file “XSL_if_Fn_Test.xsl”:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>Employee Details</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Employee Id</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Designation</th>
    </tr>
    <xsl:for-each select="company/employee">
    <xsl:sort select="empId"/>
    <xsl:if test="age<30">
    <tr>
      <td><xsl:value-of select="empId"/></td>
      <td><xsl:value-of select="firstname"/></td>
      <td><xsl:value-of select="lastname"/></td>
      <td><xsl:value-of select="designation"/></td>
    </tr>
    </xsl:if>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Input XML file contains employee details :

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="XSL_if_Fn_Test.xsl"?>
<company>
	<employee>
		<empId>1212</empId>
		<firstname>Tom</firstname>
		<lastname>Cruise</lastname>
		<designation>Project Manager</designation>
		<age>35</age>
		<gender>Male</gender>
	</employee>
	<employee>
		<empId>2383</empId>
		<firstname>Selva</firstname>
		<lastname>Kumar</lastname>
		<designation>Systems Engineer</designation>
		<age>29</age>
		<gender>Male</gender>
	</employee>
	<employee>
		<empId>2171</empId>
		<firstname>Swarna</firstname>
		<lastname>Reddy</lastname>
		<designation>Assistant Consultant</designation>
		<age>26</age>
		<gender>Female</gender>
	</employee>
</company>