I am trying to transform an xml file with xsl stylesheet into html.
this is the java
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(classLoader.getResourceAsStream("driving.xsl")));
StreamResult 开发者_Go百科drivingHtml = new StreamResult(new StringWriter());
transformer.transform(new StreamSource(classLoader.getResourceAsStream("driving.xml")), drivingHtml);
System.out.println(drivingHtml.getWriter().toString());
this is some of the xml:
<?xml version="1.0" encoding="UTF-8"?>
<user xmlns="http://notreal.org/ns1" xmlns:poi="http://notreal2.org/ns2">
<address type="primary">
<street>1031 Court St.</street>
<city>Monhegan, NY</city>
</address>
<address type="secondary">
<street> Elm St.</street>
</address>
this is the xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>User</title>
</head>
<body>
<p>Detailed Addresses</p>
<table>
<th>Primary</th>
<th>Secondary</th>
<tr>
<xsl:apply-templates select="/user/address"/>
</tr>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="address">
<td>
<xsl:value-of select=".[@type='primary']/street" />
<xsl:value-of select=".[@type='secondary']/street" />
</td>
<td>
<xsl:value-of select=".[@type='primary']/city" />
<xsl:value-of select=".[@type='secondary']/city" />
</td>
</xsl:template>
</xsl:stylesheet>
when i run that, i get "cannot compile stylesheet"
Your main problem, based on the XML and XSLT code provided, is that your code doesn't address at all the fact that the elements in the XML document are in a default namespace.
How to process an XML document with a default namespace is a FAQ -- just search the xslt and xpath tags and you'll find numerous good answers.
Here is one possible solution:
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://notreal.org/ns1"
exclude-result-prefixes="x">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<html>
<head>
<title>User</title>
</head>
<body>
<p>Detailed Addresses</p>
<table>
<thead>
<xsl:apply-templates select="x:address/@type"/>
</thead>
<tr>
<xsl:apply-templates select="x:address/x:street"/>
</tr>
<tr>
<xsl:apply-templates select="x:address/x:city"/>
</tr>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="@type">
<th><xsl:value-of select="."/></th>
</xsl:template>
<xsl:template match="x:address/*">
<td><xsl:value-of select="."/></td>
</xsl:template>
</xsl:stylesheet>
when applied on a complete and well-formed XML document that includes the XML fragment provided in the question:
<user xmlns="http://notreal.org/ns1"
xmlns:poi="http://notreal2.org/ns2">
<address type="primary">
<street>1031 Court St.</street>
<city>Monhegan, NY</city>
</address>
<address type="secondary">
<street>203 Elm St.</street>
<city>Pittsburgh, PA</city>
</address>
</user>
produces (what seems to be) the wanted, correct result:
<html>
<head>
<title>User</title>
</head>
<body>
<p>Detailed Addresses</p>
<table>
<thead>
<th>primary</th>
<th>secondary</th>
</thead>
<tr>
<td>1031 Court St.</td>
<td>203 Elm St.</td>
</tr>
<tr>
<td>Monhegan, NY</td>
<td>Pittsburgh, PA</td>
</tr>
</table>
</body>
</html>
精彩评论