1
我想用xsl樣式表將xml文件轉換爲html。xslt - 不能訪問帶屬性選擇器的curent節點
這是Java
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(classLoader.getResourceAsStream("driving.xsl")));
StreamResult drivingHtml = new StreamResult(new StringWriter());
transformer.transform(new StreamSource(classLoader.getResourceAsStream("driving.xml")), drivingHtml);
System.out.println(drivingHtml.getWriter().toString());
這是一些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>
這是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>
當我運行的是,我得到「不能編譯樣式表「
@DanielBrockman:不客氣。 –