Setting global parameters in a Java program

page 66 public class GlobalParameters { public static void parseAndProcessString sourceID, String xslID, String outputID { try { TransformerFactory tfactory = TransformerFactory.newInstance; Transformer transformer = tfactory.newTransformernew StreamSourcexslID; Use the setParameter method to set global parameters transformer.setParameterstartX, new Integer50; transformer.setParameterbaseColor, magenta; transformer.transformnew StreamSourcenew FilesourceID, new StreamResultnew FileoutputID; } catch TransformerConfigurationException tce { System.err.printlnException: + tce; } catch TransformerException te { System.err.printlnException: + te; } } public static void mainString argv[] throws java.io.IOException, org.xml.sax.SAXException { GlobalParameters gp = new GlobalParameters; gp.parseAndProcessxyz.xml, params.xsl, output.text; } } Notice that we used the setParameter method to set global parameters for the Transformer object before we invoke the transform method. This transformation generates the following results in output.text: Global parameters example The value of startX is: 50 The value of baseColor is: magenta

4.5 Variables

If we use logic to control the flow of our stylesheets, well probably want to store temporary results along the way. In other words, well need to use variables. XSLT provides the xsl:variable element, which allows you to store a value and associate it with a name. The xsl:variable element can be used in three ways. The simplest form of the element creates a new variable whose value is an empty string . Heres how it looks: xsl:variable name=x This element creates a new variable named x , whose value is an empty string. Please hold your applause until the end of the section. You can also create a variable by adding a select attribute to the xsl:variable element: xsl:variable name=favouriteColour select=blue In this case, weve set the value of the variable to be the string blue. Notice that we put single quotes around the value. These quotes ensure that the literal value blue is used as the page 67 value of the variable. If we had left out the single quotes, this would mean the value of the variable is that of all the blue elements in the current context, which definitely isnt what we want here. Some XSLT processors dont require you to put single quotes around a literal value if the literal value begins with a number. This is because the XML specification states that XML element names cant begin with a number. If I say the value should be 35 , Xalan, XT, and Saxon all assume that I mean 35 as a literal value, not as an element name. Although this works with many XSLT processors, youre safer to put the single quotes around the numeric values anyway. A further aside: the value here is the string 35, although it can be converted to a number easily. The third way to use the xsl:variable element is to put content inside it. Heres a brief example: xsl:variable name=y xsl:choose xsl:when test=x gt; 7 xsl:text13xsl:text xsl:when xsl:otherwise xsl:text15xsl:text xsl:otherwise xsl:choose xsl:variable In this more complicated example, the content of the variable y depends on the test attribute of the xsl:when element. This is the equivalent of this procedural programming construct: int y; if x 7 y = 13; else y = 15; 4.5.1 Are These Things Really Variables?