Friday, June 3, 2011

Grouping tags based on Condition +XSLT

Here we are going to discus about how to group the tags under some condition in xslt. I will explain this by taking the Employer example.
        Before this we need to know some of the xslt functions and template calling .
  • generate-id()
  • key()
  • <xsl:key>
  • <xsl:template>
Suppose we have one xml which is having many number of branches in a company .Each branch has many number of employees. The xml looks like this.

<?xml version="1.0" ?>
<Employees>
<Employee>
<TeamID>1</TeamID>
<TeamName>Sales</TeamName>
<TaskID>1</TaskID>
<Hours>5</Hours>
<EmployeeID>1</EmployeeID>
<Name>Bob</Name>
<Surname>Shibob</Surname>
</Employee>
<Employee>
<TeamID>1</TeamID>
<TeamName>Sales</TeamName>
<TaskID>2</TaskID>
<Hours>4</Hours>
<EmployeeID>1</EmployeeID>
<Name>Bob</Name>
<Surname>Shibob</Surname>
</Employee>
<Employee>
<TeamID>1</TeamID>
<TeamName>Sales</TeamName>
<TaskID>4</TaskID>
<Hours>7</Hours>
<EmployeeID>2</EmployeeID>
<Name>Sara</Name>
<Surname>Lee</Surname>
</Employee>
<Employee>
<TeamID>2</TeamID>
<TeamName>Finance</TeamName>
<TaskID>5</TaskID>
<Hours>2</Hours>
<EmployeeID>3</EmployeeID>
<Name>John</Name>
<Surname>Smith</Surname>
</Employee>
<Employee>
<TeamID>2</TeamID>
<TeamName>Finance</TeamName>
<TaskID>3</TaskID>
<Hours>4</Hours>
<EmployeeID>4</EmployeeID>
<Name>Penny</Name>
<Surname>Wise</Surname>
</Employee>
<Employee>
<TeamID>2</TeamID>
<TeamName>Finance</TeamName>
<TaskID>5</TaskID>
<Hours>3</Hours>
<EmployeeID>4</EmployeeID>
<Name>Penny</Name>
<Surname>Wise</Surname>
</Employee>
</Employees>

To do this
·        First declare the one key variable
<xsl:key name=" keyEmployeeID" match="Employee" use=" EmployeeID"/>
·        Then write a for loop for iterating the Employee set
<xsl:for-each select="Employee”>
·        Then generate and id for each key value of employes
<xsl:for-each select="…………generate-id(key('keyEmployeeID', EmployeeID)[1])]">
·        Assign all this ids in to a array
<xsl:for-each select="tEmployee[generate-id(.) = generate-id(key('keyEmployeeID', EmployeeID)[1])]">


Valid XSD format values

xs:datetime
Valid xs:datetime value is 2005-05-27T14:11:00.943Z but the following values are not the valid one
  • 2005-05-27 14:11:00.943Z (missing date and time separator "T"),
  • 2005-05-27T14:11:00.943 (missing time zone)
  • 2005-05-27 14:11:00.943 (missing time separator and time zone).
xs:date
2005-05-27Z is a valid xs:date value but 2005-05-27 is not since no time zone is specified

xs:boolean
True is not a valid xs:boolean valied.It sholud be'true'.similarly 'False' is not valid value,it is 'false'

xs:decimal

value 0.12345678 but not 12E-4.to convert scientific number in to the decimal format you neet to use the following template convertSciToNumString (which is published in the previous post)

XStream - Features

XStream is a simple library to serialize objects to XML and back again.
Features
  • Ease of use. A high level facade is supplied that simplifies common use cases.
  • No mappings required. Most objects can be serialized without need for specifying mappings.
  • Performance. Speed and low memory footprint are a crucial part of the design, making it suitable for large object graphs or systems with high message throughput.
  • Clean XML. No information is duplicated that can be obtained via reflection. This results in XML that is easier to read for humans and more compact than native Java serialization.
  • Requires no modifications to objects. Serializes internal fields, including private and final. Supports non-public and inner classes. Classes are not required to have default constructor.
    Full object graph support. Duplicate references encountered in the object-model will be maintained. Supports circular references.
  • Integrates with other XML APIs. By implementing an interface, XStream can serialize directly to/from any tree structure (not just XML).
  • Customizable conversion strategies. Strategies can be registered allowing customization of how particular types are represented as XML.
  • Error messages. When an exception occurs due to malformed XML, detailed diagnostics are provided to help isolate and fix the problem.
  • Alternative output format. The modular design allows other output formats. XStream ships currently with JSON support and morphing