Вы находитесь на странице: 1из 20

XSLT (eXtensible Stylesheet Language Transformations) is the recommended style

sheet language for XML.

XSLT is far more sophisticated than CSS. With XSLT you can add/remove elements and
attributes to or from the output file. You can also rearrange and sort elements,
perform tests and make decisions about which elements to hide and display, and a
lot more.

XSLT uses XPath to find information in an XML document.

XSLT Example
We will use the following XML document:

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


<breakfast_menu>

<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple
syrup</description>
<calories>650</calories>
</food>

<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped
cream</description>
<calories>900</calories>
</food>

<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and
whipped cream</description>
<calories>900</calories>
</food>

<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>

<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash
browns</description>
<calories>950</calories>
</food>

</breakfast_menu>
Use XSLT to transform XML into HTML, before it is displayed in a browser:

Example XSLT Stylesheet:


<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="breakfast_menu/food">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
<xsl:value-of select="price"/>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>
<xsl:value-of select="description"/>
<span style="font-style:italic"> (<xsl:value-of select="calories"/> calories
per serving)</span>
</p>
</div>
</xsl:for-each>
</body>
</html>

///rmi

WHAT WE DO
CORPORATE
EXPLORE
STORE
BLOG
CONTACT
SIGN UP
LOG IN

Library

FACTORIAL OF A NUMBER USING RMI IN JAVA

FACTORIAL OF A NUMBER USING RMI IN JAVA

AIM:

To write the Remote Invocation Method (RMI) program to find a factorial


of a number .

PROCEDURE:

1. Start the process.

2. To perform the RMI operation create Interface file, Implementation


file, Server file and

Client file .

3. Perform the JAVA compilation for all files.


4. Perform the RMI compilation for the implementation file to create stub and
skeleton by

using rmic command.

5. Start the RMI registry.

6. Run the server and client file separately.

7. Display the result.

8. Terminate the process.

PROGRAM:

CLIENT PROGRAM:

import java.io.*;

import java.rmi.*;

public class client

public static void main(String args[])throws Exception

try

String s="rmi://"+args[0]+"/abc";

serverint f=(serverint)Naming.lookup(s);

DataInputStream m=new DataInputStream(System.in);

int n1=Integer.parseInt(m.readLine());

System.out.println("the factorial is"+f.fact(n1));

catch(Exception e)

System.out.println(e);

}
INTERFACE PROGRAM:

import java.rmi.*;

public interface serverint extends Remote

int fact(int n)throws Exception;

IMPLEMENTATION PROGRAM:

import java.rmi.*;

import java.rmi.server.*;

public class serverimpl extends UnicastRemoteObject implements serverint

public serverimpl()throws Exception

public int fact(int n)

int i,c=1;

for(i=1;i<=n;i++)

c=i*c;

return c;

}
SERVER PROGRAM:

import java.net.*;

import java.rmi.*;

public class server

public static void main(String args[])

try

serverimpl m=new serverimpl();

Naming.rebind("abc",m);

catch(Exception e)

System.out.println("Exception"+e);

OUTPUT:

SERVER WINDOW:

C:\vino20>javac serverint.java

C:\vino20>javac serverimpl.java

C:\vino20>javac server.java

C:\vino20>rmic serverimpl
C:\vino20>start rmiregistry

C:\vino20>java server

CLIENT WINDOW:

C:\vino20>javac client.java

Note: client.java uses or overrides a deprecated API.

Note: Recompile with -Xlint:deprecation for details.

C:\vino20>java client localhost

the factorial is 6

C:\vino20>

English,RMI IN JAVA

Average Rating:
0 ratings
Share
0

inShare
0
kannan
Sun, 10/11/2009 - 18:42
life is short so make it sweet

Comments (0)

Want to see more?

Sign Up

or Login

ABOUT
Our Mission
Why Classle?
Corporate
Features
Services
MEDIA
Media Releases
Case Studies
Press
CONTACT
Contact Us
Careers
HELP
FAQ
Report a Problem
SOCIAL
Facebook
Twitter
LinkedIn
Google+
Blog

Desktop Version available for


Windows
Ubuntu

�Copyright Classle. All Rights Reserved.Privacy | Terms | Disclaimer

RMI Program to calculate Factorial of a Number


AddServerIntf.java

import java.rmi.*;

public interface AddServerIntf extends Remote{


int fact(int d1)throws RemoteException;
}

AddServerImpl.java

import java.rmi.*;
import java.rmi.server.*;

public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf{


public AddServerImpl()throws RemoteException{}
public int fact(int d1)throws RemoteException{
int i,d=1;
for(i=d1;i>1;i--){
d=d*i;
}
return d;
}
}

AddServer.java
import java.net.*;
import java.rmi.*;

public class AddServer{


public static void main(String args[]){
try{
AddServerImpl addServerImpl=new AddServerImpl();
Naming.rebind("AddServer",addServerImpl);
}
catch(Exception e){
System.out.println("Exception: "+e);
}
}
}

AddClient.java

import java.rmi.*;

public class AddClient{


public static void main(String args[]){
try{
String addServerURL="rmi://"+args[0]+"/AddServer";
AddServerIntf addServerIntf=(AddServerIntf)Naming.lookup(addServerURL);
System.out.println("Number is : "+args[1]);
int d1=Integer.valueOf(args[1]).intValue();
System.out.println("The Factorial is : "+addServerIntf.fact(d1));
}
catch(Exception e){
System.out.println("Exception : "+e);
}
}
}

Data Transformation using XSLT Stylesheets


Now, let's actually use an XSLT stylesheet to transform XML data into HTML data.
Here, we will take a look at an example that uses an XSLT stylesheet (LIST2) to
transform an XML Document (LIST1) representing user information. LIST3 shows the
actual data transformed into an HTML format. The second line of LIST1 is where the
designated XSLT stylesheet (list2.xsl) is applied.
LIST1: Source XML Document (list1.xml)

01 <?xml version="1.0" ?>


02 <?xml-stylesheet type="text/xsl" href="list2.xsl"?>
03 <UserList>
04 <User>
05 <Name>John Smith</Name>
06 <Account>John</Account>
07 </User>
08 </UserList>
LIST2: XSLT Stylesheet (list2.xsl)

01 <?xml version="1.0" ?>


02 <xsl:stylesheet version="1.0"
03 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
04 <xsl:template match="/">
05 <html>
06 <body>
07 <h1>Welcome</h1>
08 Mr. <xsl:value-of select="UserList/User/Name" /><br/>
09 </body>
10 </html>
11 </xsl:template>
12 </xsl:stylesheet>
LIST3: Transformation Result (list3.html)

<html>
<body>
<h1>Welcome</h1>
Mr. John Smith<br>
</body>
</html>
Opening the XML document using Internet Explorer 5.x or later results in the
following display:

Looking at LIST2, you can see that the HTML tags and XSLT commands are both used.
In order to decipher what parts of the code are XSLT commands, we designate the
following namespace:

http://www.w3.org/1999/XSL/Transform

This description indicates an XSLT command. Designating this for all XSLT commands
creates redundancy, so we associate the description with a prefix. Any prefix can
be used, but the prefix "xsl" is used most frequently with XSLT. The prefix and the
element name are separated by a colon (:). In other words, "xsl:�" is notated to
separate transformation commands and output data. Since the XSLT stylesheet is an
XML document, a root element is required. The root element of an XSLT stylesheet is
"xsl:stylesheet element."

The various commands for document transformation are described in the child
element(s) of the xsl:stylesheet element. The element described directly below the
xsl:stylesheet element is called the "top level element." The following are
elements that can be coded as a top level element:
attribute-set
key
param
template
decimal-format
namespace-alias
preserve-space
variable
include
output
strip-space
Specific transformation content is called "Templates" (lines 5 through 10 in
LIST2); templates are described in "Template Rules." The "xsl:template element" is
what represents the template rules (lines 4 through 11 in LIST2).

Because it is not an XML document, an HTML document can be coded in different ways
that would not be accepted as a well-formed XML document (attributes not surrounded
by quotes; open tag with no closing tag, etc.). However, an XSLT stylesheet is an
XML document, and as such, must be a well-formed document. Accordingly, the HTML br
element <br> must always be notated as an empty element (<br/>) (line 8 in LIST2).

"XPath" specification for designating the transformation source XML


In order to transform a document from XML data, you need to do more than simply
apply an XSLT stylesheet. You must clearly designate what is to be transformed
(e.g. "transform A into B") and how. In other words, you must designate the source
location. Accordingly, the W3C recommended "XPath (XML Path Language)" in November
1999 as a specification for designating a certain location in an XML document.

With XPath, the component elements of an XML document are treated as nodes. For
example, following Figure represents the result of expressing the XML document
LIST4 via XPath in nodes.

LIST4: Catalog XML Document (list4.xml)

01 <?xml version="1.0" ?>


02 <Catalog>
03 <Title>XML Series Commemorative Goods</Title>
04 <Product ProductCode="p001">
05 <ProductName>XML Strap</ProductName>
06 <UnitPrice>700</UnitPrice>
07 </Product>
08 </Catalog>
Under XPath, the root node "/" is expressed at the top of a tree structure. But we
must be careful to note the difference between "root node" and "root element." A
root element is the root of the element tree structure. In an XML document,
comments and processing instructions may also be coded outside the root element;
the root node is what summarizes all of these components.

To designate a node, nodes are separated by the "/" character in order from the
root node down through each hierarchy level. For example, taking the ProductName
element from LIST4, the XPath method would be "/Catalog/Product/ProductName". Under
XSLT, the node to be processed with respect to the template rule is designated.
This is the match attribute (line 4 of LIST5) of the xsl:template element.

LIST5:XSLT Stylesheet (list5.xsl)

01 <?xml version="1.0" ?>


02 <xsl:stylesheet version="1.0"
03 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
04 <xsl:template match="/">
05 <html>
06 <body>
07 <h1><xsl:value-of select="Catalog/Title" /></h1>
08 Here is the featured product for today<br/>
09 <table border="1" width="200">
10 <tr><th>ProductName</th><th>Price</th></tr>
11 <xsl:apply-templates select="Catalog/Product"/>
12 </table>
13 </body>
14 </html>
15 </xsl:template>
16 <xsl:template match="Product">
17 <tr>
18 <td><xsl:value-of select="ProductName" /></td>
19 <td><xsl:value-of select="UnitPrice" /></td>
20 </tr>
21 </xsl:template>
22 </xsl:stylesheet>
Lines 4 through 15 in LIST5 represent the template rules regarding root node
processing. The node that is subject to the template rule is called the "current
node." In XPath, there is the "absolute path" where descriptions are made from the
root node, and the "relative path" where descriptions are made relative to the
current node.

If the current node in LIST4 is the Catalog element node, then the XPath notation
for the ProductName element would be "Product/ProductName".

When designating an attribute, use "@attributename". The XPath notation


representing the ProductCode attribute in LIST4 would be "Product/@ProductCode".

"xsl:value-of" command for extracting data


Using the xsl:value-of command allows you to extract data from the transformation
source XML document, and then output the transformation result. Under the
xsl:value-of command, you designate the data you want to extract for the select
attribute in XPath notation:

On line 8 of LIST2, the "John Smith" (from the designated "UserList/User/Name" text
content) is extracted, and then output as the transformation result.

"xsl:template" element for describing transformation rules


The specific transformation process content is described as a template inside the
xsl:template element. Designate in the match attribute the location within the
source XML document that is the target for processing.

The XSLT processor first applies the template rule that processes the root node,
executing the template within the applied template rules in order from top to
bottom.

"xsl:apply-templates" command for applying other template rules


A multiple number of template rules can be created, similar to the subroutines of a
traditional program. Use the xsl:apply-templates command to apply a different
template rule from within a template rule, and use XPath notation in the select
attribute to designate the location in the transformation source XML documen.

XPath notation for selecting a location

In LIST5, a separate template is applied in order to display the Product element


data.

Review Questions
Question 1
Select which of the following is incorrect as a method for using XSLT.

Transform XML data into HTML for display in a web browser


Output a portion of data extracted from source XML data in order to send XML data
to another system
Output data in CSV format (text format) in order to send XML data to a system that
does not support XML
Transform XML data into a zip format (compressed format) in order to send the data
more efficiently
Comments
Under XSLT, data can be output in XML, HTML or text formats. A certain portion of
source data can be copied in XML format. Data can also be output in CSV format by
separating data using a comma (,) when you need to output it in text format.
However, binary format files cannot be output. Accordingly the correct answer to
this question is D.

Question 2
Select which of the following is correct as an XSLT stylesheet notation.

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


<stylesheet version="1.0">

<template match="/">
<html>
<body>
<h1>Welcome</h1>
Hello<value-of select="UserList/User/Name" /> <br/>
</body>
</html>
</template>
</stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<html>
<body>
<h1>Welcome</h1>
Hello<xsl:value-of select="UserList/User/Name" /><br/>
</body>
</html>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>Welcome</h1>
Hello<xsl:value-of select="UserList/User/Name" /><br>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>Welcome</h1>
Hello<xsl:value-of select="UserList/User/Name" /><br/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Comments
With XSLT stylesheets, the namespace "http://www.w3.org/1999/XSL/Transform" must be
declared using the element associated with the namespace. If the namespace is not
designated, the XSLT command will not be recognized, even if it is the same element
name as the XSLT. The template is described as the child element of the
xsl:template element, but it cannot be written directly beneath the xsl:stylesheet
element. In addition, since the XSLT stylesheet is an XML document, it must be a
well-formed document.

The HTML <br> tag must be written as <br/>. Accordingly, the correct answer is D.

Question 3
Select which of the following is the correct XPath notation corresponding to (1),
when you want to transform the following XML document into HTML using an XSLT
stylesheet. Select all that apply.

[XML Document]
<?xml version="1.0" encoding="UTF-8"?>
<Snack>
<Yesterday>
<Fruit>Banana</Fruit>
</Yesterday>
<Today>
<Fruit>Watermelon</Fruit>
</Today>
<Tomorrow>
<Fruit>Melon</Fruit>
</Tomorrow>
</Snack>

[XSL Stylesheet]
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>Snack</h1>
<xsl:apply-templates select="Snack/Tomorrow"/>
</body>
</html>
</xsl:template>
<xsl:template match="Tomorrow">
Snack will be<xsl:value-of select=" (1) " /><br/>
</xsl:template>
</xsl:stylesheet>

[HTML Document]
<html>
<body>
<h1>Snack</h1>
Snack will be Melon<br>
</body>
</html>

/Snack/Tomorrow/Fruit
Snack/Tomorrow/Fruit
Tomorrow/Fruit
Fruit
Comments
With XPath, the absolute path is written beginning with the "/" character, while a
path not beginning with the "/" character is a relative path. In this question, the
fruit element node below the Tomorrow element node is designated. Expressing the
absolute path results in "/Snack/Tomorrow/Fruit", and in the case of a relative
path, the node designated in the match attribute of the xsl:template element is the
current node. Here, the current node is the Tomorrow element node, so expressing
the relative path results in "Fruit". Accordingly, the correct answers are A and D.

Tomoya Suzuki
Toshiba OA Consultant, Ltd. Training Solutions Engineering Department. Mr. Suzuki
works mainly as an instructor in XML and programming language seminars. He laments
that, for some reason, all of the rainy days during rainy season were on the
weekends this year, preventing him from playing tennis, and causing him to suffer
from a lack of exercise. He does predict, however, that he will have a deep, dark
tan by the time this article is published. Don't forget the sun block, Tomoya.

The content presented here is an HTML version of an article that originally


appeared in the September 2006 issue of DB Magazine published by Shoeisya.

XML Master Tutorial Indexs

best RMI
RMI application for counting factorial of a given number.

Code for RMI application for counting factorial of a given number. in Java
// FactRMI.java Interfacepublicinterface FactRMI extends java.rmi.Remote
{
long countfact(int num) throws java.rmi.RemoteException;

// FactRMIImpl.java, FactRMI implementation

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

publicclass FactRMIImpl extends UnicastRemoteObject implements FactRMI


{
privateint fact;

public FactRMIImpl(String name) throws RemoteException


{
super();
try
{
Naming.rebind(name, this);
fact = 1;
}
catch (Exception e)
{
System.out.println("Exception: " + e.getMessage());
e.printStackTrace();
}
}

publiclong countfact(int num) throws RemoteException


{
for(int i=1;i<=num;i++)
{
fact = fact * i;
}
return fact;
}

// FactRMIServer.java

import java.rmi.*;
import java.rmi.server.*;

publicclass FactRMIServer
{

publicstaticvoid main(String args[])


{

// Create and install the security manager


System.setSecurityManager(new RMISecurityManager());

try
{
// Create CountRMIImpl
FactRMIImpl myFact = new FactRMIImpl("//Binita/myFactRMI");
System.out.println("FactRMI Server ready.");
}
catch (Exception e)
{
System.out.println("Exception: " + e.getMessage());
e.printStackTrace();
}
}
}

// FactRMIClient.java RMI Count client

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.io.DataInputStream;

publicclass FactRMIClient
{ publicstaticvoid main(String args[])
{
// Create and install the security manager
System.setSecurityManager(new RMISecurityManager());
DataInputStream in = new DataInputStream(System.in);
int num;

try
{
FactRMI myFact = (FactRMI)Naming.lookup("//"
+ args[0] + "/" + "myFactRMI");

System.out.println("\nEnter the number :");


num = Integer.parseInt(in.readLine());
// Calculate Factoriallong FactVal = myFact.countfact(num);

System.out.println("Factorial = " + FactVal);


}
catch(Exception e)
{
System.err.println("System Exception" + e);
}
System.exit(0);
}
}

// OUTPUT

Enter the number :


10
Factorial = 3628800
FACTORIAL OF A NUMBER USING RMI IN JAVA
August 22, 2013

CLIENT PROGRAM:

import java.io.*;

import java.rmi.*;

public class client

public static void main(String args[])throws Exception

try

String s="rmi://"+args[0]+"/abc";

serverint f=(serverint)Naming.lookup(s);

DataInputStream m=new DataInputStream(System.in);

int n1=Integer.parseInt(m.readLine());

System.out.println("the factorial is"+f.fact(n1));

catch(Exception e)

System.out.println(e);

INTERFACE PROGRAM:

import java.rmi.*;
public interface serverint extends Remote

int fact(int n)throws Exception;

IMPLEMENTATION PROGRAM:

import java.rmi.*;

import java.rmi.server.*;

public class serverimpl extends UnicastRemoteObject implements serverint

public serverimpl()throws Exception

public int fact(int n)

int i,c=1;

for(i=1;i<=n;i++)

c=i*c;

return c;

SERVER PROGRAM:

import java.net.*;

import java.rmi.*;

public class server

public static void main(String args[])


{

try

serverimpl m=new serverimpl();

Naming.rebind("abc",m);

catch(Exception e)

System.out.println("Exception"+e);

OUTPUT:

SERVER WINDOW:

C:\>javac serverint.java

C:\>javac serverimpl.java

C:\>javac server.java

C:\>rmic serverimpl

C:\>start rmiregistry

C:\>java server

CLIENT WINDOW:

C:\>javac client.java

Note: client.java uses or overrides a deprecated API.


Note: Recompile with -Xlint:deprecation for details.

C:\>java client localhost

the factorial is 6

SHARE
SHARE
Comments

Popular posts from this blog


CREATING RESPONSIVE LAYOUT
November 15, 2014
HTML CODE:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>::Responsive Layout::</title>
<link rel="stylesheet" href="css/style.css"/>
</head>

<body class="body">
<header class="mainheader"/>
<img src="img/logo.png" alt="logo"/>
<nav>
<ul>
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Portfoliow</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<div class="maincontent">
<div class="content">
<article class="t�
SHARE
POST A COMMENT
READ MORE
simple question with answers
August 15, 2013
Five friends have their gardens next to one another, where they grow three kinds of
crops: fruits (apple, pear, nut, cherry), vegetables (carrot, parsley, gourd,
onion) and flowers (aster, rose, tulip, lily).
1. They grow 12 different varieties.