Sunday, April 29, 2012

WEB SERVICE & XML


 Web Service & XML

Idea about Web Services Architecture component :-
1. Service provider: The service provider provide the service and  implements the service. It make the service available on the internet
2. Service requestor: This is any consumer of the web service. The requestor utilizes an existing web service by opening a network connection and sending an XML request.
3. Service registry: Centralized directory of services. It is logically  registry provides a central place. At that place developers published  new services  and find existing services.
4. Service transport: Service transport is responsible for transporting messages between applications. Its includes hypertext transfer protocol (HTTP), Simple Mail Transfer Protocol (SMTP), file transfer protocol (FTP).
5. XML messaging: Responsible for encoding messages in a common XML format because that messages must be understandable at the second  end. Currently, this layer includes XML-RPC and SOAP.
6. Service description: Responsible for describing the public interface to a specific web service. Currently, service description is handled via the Web Service Description Language (WSDL).
7. Service discovery: Responsible for centralizing services into a common registry. Its provide easy publish and find  functionality for the developer (UDDI).
Characteristic of web services :-
1. XML-based: XML based is totally platform independent and run on any operating system and also language independent
2. Loosely Coupled: Loosely coupled means consumer of a web service does not tied web services directly.
3. Ability to be synchronous or asynchronous: Synchronized provide the facility  to binding the client when client want to execute the Web Services. Means client synchronously used the services.  Asynchronous operations allowed  the client  a client to invoke the web service when client want to execute other functions.

Advantages of Web Services :-
1. Interoperability
2. Reusability
3. Deploy ability
XML-RPC :-
RPC is a mechanism to call  function or procedure that are  available on a remote computer. XML-RPC provide facility to computer to call procedure from other remote computer and make function across network. XML-RPC uses the HTTP protocol to transfer information between a client computer and a server computer. It uses XML vocabulary to describe nature of request and response. XML-RPC = HTTP + XML + Remote Procedure Calls.
UDDI :-
UDDI (Universal Description, Discovery and Integration) is a directory for storing information about web services to  discovering, publishing and finding businesses and integrating business services by using the Internet. It is platform-independent framework for describing services.
WSDL :-
WSDL stands for Web Services Description Language and its represent a layer within a web service protocol stack. WSDL containing the Data types for all XML messages and it provide information about all XML messages.
SOAP :-
SOAP stands for Simple Object Access Protocol and it is a protocol which exchange information between computer. Total transaction is the XML based and it is Platform independent.
WebMethod Attribute :-
The WebMethod attribute tells .NET that a particular public method should be exposed as a web-callable method. The WebMethod attribute has six associated properties to document and change the behavior of your web method. They are,
1. Description
2. MessageName
3. EnableSession
4. CacheDuration
5. TransactionOption
6. BufferResponse
W3C :-
World Wide Web Consortium (W3C) is actively pursuing standardization of Web service protocols.
Protocols a .Net Web Service uses :-
In .Net, a web service is bind with three different protocols such as HTTP/POST, HTTP/GET, and SOAP. Http-Get and Http-Post can only be used when name/value pairs of data is dealt with. But when data is complex in nature such as ASP.NET dataset, XML notes etc, then we can use SOAP that serializes data in simpler form before sending.
Document web services :-
[WebService(Name = "Customer Service", Description = "Retrieve the Customer details",Namespace=http://www.apress.com/ProASP.NET/)]
public class Customer : System.Web.Services.WebService
{
     [WebMethod(Description = "Returns Customer Count")]
     public int GetCustomerCount()
     { ... }
     [WebMethod(Description = "Returns the list of Customer")]
     public DataSet GetCustomer()
     { ... }
}
Namespace allows your web service to be uniquely identified. By default, ASP.NET web services use the default XML namespace http://tempuri.org/, which is suitable only for testing. XML namespace simply identifies your web service. XML namespaces usually look like URLs. However, they don't need to correspond to a valid Internet location.
Required ASP.NET web services  :-
ASP.NET web services are the great way to expose your middle tier components via internet. These components offer no issue communicating across firewalls as they use SOAP as transport protocols that transmit structured data using HTTP channel. Thus, message can be easily exchanged through port 80, i.e. through internet data port without being getting hampered by corporate firewalls or proxy server.
Web services are primarily being used for B2B integration like authorizing employees, supplier, electronically signing of invoice etc.
Different distributed technologies :-
The need of distributed technologies arises with the requirement of distributed computing applications. The distributed computing allows partitioning of application logic into units and spreading the unit over different computers of a network or across different networks. This helps in spreading out loads over many computers. The components once developed can be reuse by other applications. There are many technologies developed to allow the distribution and reuse of application logic.
Advantages of the distributed components:
• The key benefit of having distributed components is that they spread out the load over different machines.
• The components can be upgraded without disturbing the clients' code.
• The distributed application improves security. For example, a company who has many agents wouldn’t like those agents to have direct access to its database. Instead, these agents can be granted access to the components running on the corporate server which can be controlled and restricted.
Web Service Standards :-
WSDL
SOAP
HTTP
UDDI
DISCO
Ways of accessing a web service :-
1. Asynchronous Call: Application can make a call to the Webservice and then continue to do whatever it wants to do.When the service is ready it will notify the application.Application  can use BEGIN and END method to make asynchronous call to the webmethod.We can use  either a WaitHandle or a Delegate object when making asynchronous call.
The WaitHandle class share resources between several objects. It provides several  methods which will wait for the resources to become available
The easiest and most powerful way to to implement an asynchronous call is using a  delegate object. A delegate object wraps up a callback function.
2. Synchronous Call: Application has to wait until has completed.
Default namespaces in ASMX files :-
System,
System.Collections,
System.ComponentModel,
System.Data,
System.Diagnostics,
System.Web,
System.Web.Services
XML :-
• Extensible Markup Language (XML) is the universal language for data on the Web
• XML is a technology which allows us to create our own markup language.
• XML documents are universally accepted as a standard way of representing information in platform and language independent manner.
• XML is universal standard for information interchange.
• XML documents can be created in any language and can be used in any language.
Structure of XML document :-
Schema vs DTD :-
Schema
DTD
Schema document is an XML document i.e., the structure of an XML document is specified by another XML document.
DTDs follow SGML syntax.
Schema supports variety of dataTypes similar to programming language.
In DTD everything is treated as text.
In Schema,  It is possible to inherit and create relationship among elements.
This is not possible in DTD without invalidating existing documents.
In Schema, It is possible to group elements and attributes so that they can be treated as single logical unit.
Grouping of elements and attributes is not possible in DTD.
In Schemas, it is possible to specify an upper limit for the number of occurrences of an element
It is not possible to specify an upper limit of an element in DTDs
Complex Element :-
A complex element is an XML element that contains other elements and/or attributes.
There are four kinds of complex elements:
·         empty elements
·         elements that contain only other elements
·         elements that contain only text
·         elements that contain both other elements and text
Simple Element :-
A simple element is an XML element that can contain only text.
·         A simple element cannot have attributes
·         A simple element cannot contain other elements
·         A simple element cannot be empty
·         However, the text can be of many different types, and may have various restrictions applied to it
Namespaces :-
A simple element is an XML element that can contain only text.
·         Namespaces are a simple and straightforward way to distinguish names used in XML documents, no matter where they come from.
·         XML namespaces are used for providing uniquely named elements and attributes in an XML instance
·         They allow developers to qualify uniquely the element names and relationships and make these names recognizable, to avoid name collisions on elements that have the same name but are defined in different vocabularies.
·         They allow tags from multiple namespaces to be mixed, which is essential if data is coming from multiple sources.
Example: a bookstore may define the <TITLE> tag to mean the title of a book, contained only within the <BOOK> element. A directory of people, however, might define <TITLE> to indicate a person's position, for instance: <TITLE>President</TITLE>. Namespaces help define this distinction clearly.
Ways to use namespaces :-
There are two ways to use namespaces:
·         Declare a default namespace
·         Associate a prefix with a namespace, then use the prefix in the XML to refer to the namespace
XML parser  :-
An XML parser is a piece of software which can do following:
·         Check for well-formedness
·         Validate the document
·         Allows us to read, create or modify existing XML documents
SAX :-
SAX-Simple API for XML processing. SAX provides a mechanism for reading data from an XML document. It is a popular alternative to the Document Object Model (DOM).
Why can't datareader by returned from a Web Service's Method:-
Cos, it's not serializable
CDATA section in XML :-
CDATA Sections are used to escape blocks of text containing characters which would otherwise be recognized as markup. All tags and entity references are ignored by an XML processor that treats them just like any character data. CDATA blocks have been provided as a convenience measure when you want to include large blocks of special characters as character data, but you do not want to have to use entity references all the time.
XSL :-
eXtensible Stylesheet Language(XSL)  deals with most displaying the contents of XML documents.XSL consists of three parts:
·         XSLT - a language for transforming XML documents
·         XPath - a language for navigating in XML documents
·         XSL-FO - a language for formatting XML documents
XSLT?
eXtensible Stylesheet Language Transformation (XSLT) deals with transformation of one XML document into XHTML documents or to other XML documents. XSLT uses XPath for traversing an XML document and arriving at a particular node.
Structure of XSLT :-

XSL template :-
Template specifies transformation rules. A Stylesheet document can be made up of at least one template, which acts as an entry point. Every template uniquely identifies a particular node in the source tree.
XPath :-
XPath is an expression language used for addressing parts of an XML document.  XPath is used to navigate through elements and attributes in an XML document.
XSL-FO :-
XSL-FO   deals with formatting XML data. This can be used for generating output in a particular format like XML to PDF, XML to DOC, etc.
XSL & XSLT :-
XSL started out as a standard for specifying stylesheets just like CSS. But soon it grew to be widely used as XML translation language. Adding specific styles and formating attributes to the XML attributes made it more useful for generating the HTML files from the data obtained from XML files. The XSL still is updated continuously as a standard.
XQuery :-
XQuery is used for querying the values found in the XML file as nodes. It is similar to the SQL language that is used for querying the databases for specific values. The XQuery is the one which is used to transform the XML data to a corresponding XHTML file.
An example of XQuery is:
for $x in doc("abc.xml")/abc_name
where $x/a>30
order by $x/b
return $x/c
Does XML let me make up my own tags :-
No, it lets you make up names for your own element types. If you think tags and elements are the same thing you are already in considerable trouble: read the rest of this question carefully.
Can I use JavaScript, ActiveX, etc in XML files :-
XML is about describing information; scripting languages and languages for embedded functionality are software which enables the information to be manipulated at the user's end, so these languages do not normally have any place in an XML file itself, but in stylesheets like XSL and CSS where they can be added to generated HTML.
Include one XML file in another :-
This works exactly the same as for SGML. First you declare the entity you want to include, and then you reference it by name:
<?xml version="1.0"?>
<!DOCTYPE novel SYSTEM "/dtd/novel.dtd" [
  <!ENTITY chap1 SYSTEM "mydocs/chapter1.xml">
  <!ENTITY chap2 SYSTEM "mydocs/chapter2.xml">
  <!ENTITY chap3 SYSTEM "mydocs/chapter3.xml">
  <!ENTITY chap4 SYSTEM "mydocs/chapter4.xml">
  <!ENTITY chap5 SYSTEM "mydocs/chapter5.xml">
]>
<novel>
  <header>
    ...blah blah...
  </header>
  &chap1;
  &chap2;
  &chap3;
  &chap4;
  &chap5;
</novel>
The one thing to make sure of is that the included file must not have an XML or DOCTYPE Declaration on it. If you've been using one for editing the fragment, remove it before using the file in this way.
Do I need to use XML namespaces :-
Maybe, maybe not.
If you don't have any naming conflicts in the XML documents you are using today, as is often the case with documents used inside a single organization, then you probably don't need to use XML namespaces. However, if you do have conflicts today, or if you expect conflicts in the future due to distributing your documents outside your organization or bringing outside documents into your organization, then you should probably use XML namespaces.
Declare an XML namespace in an XML document :-
To declare an XML namespace, you use an attribute whose name has the form:
xmlns:prefix
--OR--
xmlns
These attributes are often called xmlns attributes and their value is the name of the XML namespace being declared; this is a URI. The first form of the attribute (xmlns:prefix) declares a prefix to be associated with the XML namespace. The second form (xmlns) declares that the specified namespace is the default XML namespace.
Example, <Department xmlns:addr="http://www.google.com/ito/addresses" xmlns="http://www.google.com/ito/servers">
Override an XML namespace declaration that uses a prefix :-
To override the prefix used in an XML namespace declaration, you simply declare another XML namespace with the same prefix. For example,
<google:A xmlns:google="http://www.google.org/">
  <google:B>
    <google:C xmlns:google="http://www.bar.org/">
      <google:D>abcd</google:D>
    </google:C>
  </google:B>
</google:A>
Qualified name :-
A qualified name is a name of the following form. It consists of an optional prefix and colon, followed by the local part, which is sometimes known as a local name.
prefix:local-part
--OR--
local-part

For example, both of the following are qualified names. The first name has a prefix of serv; the second name does not have a prefix. For both names, the local part (local name) is Address.
serv:Address
Address
XPointer :-
XPointer is set of recommendations developed by the W3C. The core recommendations are the XPointer Framework which provides an extensible addressing behavior for fragment identifiers in XML media types.
XPointer gains its extensibility through the XPointer Framework, which identifies the syntax and processing architecture for XPointer expressions and through an extensible set of XPointer addressing schemes.
Use <xsl:apply-template> element in XSLT :-
Using <xsl:apply-template>element we can apply a template on current element or can apply on the child nodes of current element.
Example:
<?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>Book Collection</h2>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="book">
    <p>
      <xsl:apply-templates select="title"/>
      <xsl:apply-templates select="author"/>
    </p>
  </xsl:template>
  <xsl:template match="title">
    Title: <span style="color:#ff0000">
      <xsl:value-of select="."/>
    </span>
    <br />
  </xsl:template>
  <xsl:template match="author">
    Author: <span style="color:#00ff00">
      <xsl:value-of select="."/>
    </span>
    <br />
  </xsl:template>
</xsl:stylesheet>
Use <xsl:choose> element inXSLT :-
When we want to use <xsl:choose> element in XSL file.Than we use <xsl:when> and<xsl:otherwise> inside the <xsl:choose> element.Using them we can show multiple conditional test cases.
Syntax:
<xsl:choose>
  <xsl:when test="expression">
    ... Than gives that output ...
  </xsl:when>
  <xsl:otherwise>
    ... Than gives that output ....
  </xsl:otherwise>
</xsl:choose>
Use<xsl:if> element in XSLT :-
Using <xsl:if> element we can test the content of an XSL document.
Syntax:
<xsl:if test="expression">
  ...Write here output it will dispay when condition is true...
</xsl:if>
 I have given you example to use of <xsl:if> element.
<?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>Book Collection</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>Title</th>
            <th>Author</th>
          </tr>
          <xsl:for-each select="catalog/book">
            <xsl:if test="price &gt; 150">
              <tr>
                <td>
                  <xsl:value-of select="title"/>
                </td>
                <td>
                  <xsl:value-of select="author"/>
                </td>
              </tr>
            </xsl:if>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
It will give the title of book with related author which book cost greater than 150.
Use <xsl:sort>element in XSLT :-
We use <xsl:sort> element to sort the given output.
Example:
<?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>Book Collection</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>Title</th>
            <th>Author</th>
          </tr>
          <xsl:for-each select="catalog/book">
            <xsl:sort select="author"/>
            <tr>
              <td>
                <xsl:value-of select="title"/>
              </td>
              <td>
                <xsl:value-of select="author"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Use filtering in XSLT :-
We can filter the XNL output by using filter operators.Some Legal filter operators are given below:
1.=(equal to)
2.!=(not equal to)
3.&lt;(less than)
4.&gt;(greater than)
 I have given you a example. In this I have uses '=' equal to filer operation.
<?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>Book Collection</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>Title</th>
            <th>Author</th>
          </tr>
          <xsl:for-each select="catalog/book[author='Yashwant Kanetkar']">
            <tr>
              <td>
                <xsl:value-of select="title"/>
              </td>
              <td>
                <xsl:value-of select="author"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Above, XML document will give output related to author Yashwant Kanetkar.
Use <xsl:for-each> element in XSLT  :-
Using <xsl:for-each> element we can enable the looping in XSLT.
Example:
<?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>Book Collection</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>Title</th>
            <th>Author</th>
          </tr>
          <xsl:for-each select="catalog/book">
            <tr>
              <td>
                <xsl:value-of select="title"/>
              </td>
              <td>
                <xsl:value-of select="author"/>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Use <xsl:value-of>element in XSLT :-
We use <xsl:value-of>element to extract the value of an selected XML element.
Example:
<?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>Book Collection</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>Title</th>
            <th>Author</th>
          </tr>
          <tr>
            <td>
              <xsl:value-of select="catalog/book/author"/>
            </td>
            <td>
              <xsl:value-of select="catalog/book/author"/>
            </td>
          </tr>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Transform an XML document into another XML document :-
Here, I give you an example which shows you how to transform an XML document into another XML document.
Example:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/persons">
    <root>
      <xsl:apply-templates select="person"/>
    </root>
  </xsl:template>
  <xsl:template match="person">
    <name username="{@username}">
      <xsl:value-of select="name" />
    </name>
  </xsl:template>
</xsl:stylesheet>
We can tranform above XML document into another document like that,
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <name username="Abhi">Abhi</name>
  <name username="Sudi">Sudi</name>
</root>

No comments:

Post a Comment