Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
XSLT transformation
The xslt
library provides an API to transform XML content to HTML using XSL transformation.
For more information on the underlying module, see the xslt
module.
import ballerina/io;
import ballerina/xslt;
public function main() returns error? {
// Gets an `XML` value, which needs to be transformed.
xml sourceXml = getXml();
// Gets an `XSL` style sheet represented in an XML value.
xml xsl = getXsl();
// Transforms the `XML` content to another format.
// For details, see https://lib.ballerina.io/ballerina/xslt/latest#transform.
xml target = check xslt:transform(sourceXml, xsl);
io:println("Transformed XML: ", target);
}
// Returns an `XML` element, which needs to be transformed.
function getXml() returns xml {
return xml `<samples>
<song>
<title>Summer of 69</title>
<artist>Bryan Adams</artist>
<country>Canada</country>
<year>1984</year>
</song>
<song>
<title>Zombie</title>
<artist>Bad Wolves</artist>
<country>USA</country>
<year>2018</year>
</song>
</samples>`;
}
// Returns an `XSL` style sheet represented by an XML element.
function getXsl() returns xml {
return xml
`<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>All time favourites</h2>
<table border="1">
<tr bgcolor="#9acd33">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="samples/song">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="artist"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>`;
}
To run this sample, use the bal run
command.:
$ bal run xslt_transformation.balTransformed XML: <html><body><h2>All time favourites</h2><table border="1"><tr bgcolor="#9acd33"><th>Title</th><th>Artist</th></tr><tr><td>Summer of 69</td><td>Bryan Adams</td></tr><tr><td>Zombie</td><td>Bad Wolves</td></tr></table></body></html>
PreviousUUID operations
NextXML to JSON conversion