Download Java And XSLT
Transcript
<xsl:text> </xsl:text> <xsl:if test="$includeMiddle = 'yes'"> <xsl:value-of select="middleName"/> <xsl:text> </xsl:text> </xsl:if> <xsl:value-of select="lastName"/> </xsl:element> <xsl:element name="birthDate"> <xsl:value-of select="birthDate/@month"/> <xsl:text>/</xsl:text> <xsl:value-of select="birthDate/@day"/> <xsl:text>/</xsl:text> <xsl:value-of select="birthDate/@year"/> </xsl:element> </xsl:element> </xsl:template> </xsl:stylesheet> The job of this stylesheet is to transform XML data into a more concise format as shown in Example 9-6. Example 9-6. Expected output <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE person SYSTEM "condensed.dtd"> <person> <name>Aidan Garrett Burke</name> <birthDate>6/25/1999</birthDate> </person> Finally, the DTD for the condensed XML file is shown in Example 9-7. Example 9-7. condense.dtd <!ELEMENT person (name, birthDate)> <!ELEMENT birthDate (#PCDATA)> <!ELEMENT name (#PCDATA)> By providing a DTD for the expected XML output, a unit test can easily validate the result tree after performing one or more transformations. Such a test simply writes the transformation results to a file and then attempts to parse them using a validating XML parser. 9.2.1 JUnit JUnit is an open source testing framework available from http://www.junit.org. It is a lightweight tool designed to be used by programmers specifically for unit tests. Other tools are generally better for integration testing and functional testing, but these are not discussed here. Since XSLT transformations can be performed independently of the remainder of an application, they are a perfect candidate for automated unit testing. A technology such as JSP, however, is quite difficult to test in an automated fashion because JSPs must be executed within the context of a JSP container and web browser. An automated test is one that reports "success" or "failure" after execution and does not require a human being to interact as the test is running. For instance, requiring a user to type in specific values into HTML form fields and then look at the resulting web page is clearly not automated.