Stage 2 · Topic 2 - JSP · Working with XML
| Prefix | URI | Gives you |
|---|---|---|
x |
http://java.sun.com/jsp/jstl/xml |
x:transform, x:parse,
x:set, x:eval,
x:output |
c |
http://java.sun.com/jsp/jstl/core |
c:set and c:import, which feed the XML
library its input |
These are the JSTL 1.2 URIs, matching the rest of the course. Tomcat ships the
implementation, so there is nothing to install — but the
jstl-1.2.jar API jar is in
WEB-INF/lib so the pages compile.
| Tags | API | |
|---|---|---|
| For output | — | contentType="text/xml" on the page directive |
| For formatting | x:transform |
JAXP TransformerFactory |
| For reading values | x:parse / x:eval |
JAXP DocumentBuilderFactory and DOM |
Choosing between them is the whole of Session 8. If you want HTML on the page, transform. If you want individual values — a title, a count, a total — parse and read them.
Xerces and Xalan are not needed. Slide 13 says to copy
XercesImpl.jar and xalan.jar into Tomcat's
lib folder. That instruction dates from the Java 1.4 era.
Since Java 9 an XML parser and an XSLT processor are part of the JDK
itself, and DocumentBuilderFactory.newInstance() and
TransformerFactory.newInstance() find them automatically. This
project runs on Java 21 with nothing copied anywhere.
s8-04 is the example that would have needed
Xerces.
x:transform does not need docId or a
var here. The slide calls it bare, and that is correct: with
no var, the result document is written to the response, which is
exactly what a formatting demo wants. Add var="out" if you would
rather capture the HTML in a variable and decide later.
The slide's c:import URL is hard-coded.
http://localhost:8086/JSPDemo/style.xsl only resolves on one
machine. Use a context-relative URL.
Do not prepend the context path to a c:import URL.
c:import already prepends it to a /-rooted URL, so
writing ${pageContext.request.contextPath}/xml/style.xsl
produces /jsp-demo/jsp-demo/xml/style.xsl and a 404. This is the
same trap as s7-03.
<?xml ... ?> declaration must be the very first
thing in the document. Not after a newline, not after a comment that
prints whitespace.<%-- ... --%>) prints nothing, so it
is the safe way to carry slide text on an XML-producing page. But it
must not contain its own end marker, or the comment closes early and the
remainder is written into the XML.<br> is invalid XML; use
<br/>.text/xml — the browser will show
its XML viewer or offer a download. That is the point, not a fault.getFirstChild().getNodeValue() is wrong on pretty-printed
XML — the first child is a whitespace text node. Use
getTextContent().<price>100</price> arrives
as the string "100". Integer.parseInt will
throw on a document you have not checked.${node.class.name} does not parse in EL —
class is a reserved word. Use
${node['class'].name}.| File | Purpose |
|---|---|
| xml/books.xml | the source document; the xml-stylesheet instruction at its top makes a browser transform it directly |
| xml/style.xsl | the stylesheet: identity-plus-HTML, with sum(book/price) for the total row |