jQuery in Action, 3rd edition
The moose likes XML and Related Technologies and the fly likes How to dynamically create  or modify  XSL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to dynamically create  or modify  XSL " Watch "How to dynamically create  or modify  XSL " New topic

How to dynamically create or modify XSL

Sumit Israni

Joined: Jun 20, 2001
Posts: 28
I am an XML beginner ..looking to create/modify an XSL document dynamically using a Java program..which will be applied to XML to generate HTML
In the example stylesheet which has been created manually ,I need to change the bgcolor and border properties of the 2 table tags. I dont know how to reference the appropriate table(Node).I know that XSL document can be created by any XML API ..but i find that too cumbersome. Is there any short cut(programmatically of course) by which i can modify the properties of the table nodes?I googled, but with little luck.
Can anyone please guide me the right solution.
Thanks a lot

/********************XSL document ****************************/

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl utput method="html"/>
<xsl:template match="/">

<xsl:template match="query">
<table bgcolor="yellow" border="1">


<xsl:for-each select="definition/element">
<td><xsl:value-of select="@type"/></td>
<br> <b> Table Information</b></br>
<table bgcolor="cyan" border="1">
<xsl:for-each select="definition/element">
<xsl:for-each select="data/row">
<xsl:for-each select="*">
/********************End of XSL document ****************************/
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
You seem like a total XSLT newbie, so sorry if I go too fast (with instructions or that assumption). But, here are my points:

+ All use of XSLT is done with Java (doing the transformation). You can, however, create XSLT documents and write them however you want, e.g. Vi, Notepad, etc.
+ You can treat the transformation of an XSLT document the same as any other XML document, with the exception that you need to create an xslt-namespace-alias (I forget exact tag name), so it will not evaluate XSLT tags that you want merely written to the result stream.
+ Access elements within your transformed document with regular XPath expressions.

That should be a start. Honestly, I understood about 10% of what your problem was, so this may not be of any help to you. I'd recommend using zvon.org's website for a tutorial and reference on XSLT.

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Sumit,
you might want to look at the given problem. Your example is about changing colors etc. There you better use a customized CSS File, that is plain text. For processing the XML you have 2 options:
- Use something like JDOM to access the nodes to change them
- If you know in advance what parts of your stylesheet are supposed to change, you can use <xsl: param name="myownname" value="black" /> to define a parameter (use it with $myowname or "{$myownname}" depending where u use it).
This parameters (black is only the default value) can be supplied when you execute the XSLT transformation.
;-) stw
Consider Paul's rocket mass heater.
subject: How to dynamically create or modify XSL
It's not a secret anymore!