posted 15 years ago
Hi All,
I need some advice on how to retrieve the value of 2 attributes (@href, @title) in state.xml as part of transformation as follows:
[code] <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml">
- <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
………..
</head>
- <body>
<a shape="rect" name="top" />
+ <div id="container">
- <div id="content">
+ <div id="as1">
+ <div id="alphalinks">
<h1> Listing of all the states</h1>
+ <p>
- <table class="sresults">
- <tr>
- <td colspan="1" rowspan="1">
<a shape="rect" href="http://www.abc.com/areas/CA/fairy+land" title="Fairy Land, CA">Fairy Land</a>
</td>
- <td colspan="1" rowspan="1">
<a shape="rect" href="http://www.abc.com/areas/CA/wonder+land" title="Wonder Land, CA">Wonder Land</a>
</td>
……….
</tr>
</body>
</html>
Below is the content of stateStyleSheet.xsl:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Transformed State Detail</h2>
<table border="1">
<tr bgcolor="lightblue">
<th align="left">Area Link</th>
<th align="left">Area Name</th>
</tr>
<xsl:for-each select="/html/body/div[@id='content']/table[@class='sresults']/tr/td/a">
<tr>
<td><xsl:value-of select="@href"/></td>
<td><xsl:value-of select="@title"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Here is state.java together with XML Catalog to bring everything together:
SAXBuilder stateBuilder = new SAXBuilder("org.ccil.cowan.tagsoup.Parser", false);
FileInputStream stateIS = new FileInputStream("E:\\state.xml");
BufferedInputStream stateBIS = new BufferedInputStream(stateIS);
Document stateOriginaljdomDocument = stateBuilder.build(stateBIS);
TransformerFactory stateFactory = TransformerFactory.newInstance();
FileInputStream styleSheetIS = new FileInputStream("E:\\stateStyleSheet.xsl");
BufferedInputStream styleSheetBIS = new BufferedInputStream(styleSheetIS);
Transformer stateTransformer = stateFactory.newTransformer(new StreamSource(styleSheetBIS));
……
Output from transformation process gives:
<?xml version="1.0" encoding="UTF-8"?>
<html>
<body>
<h2>Transformed State Detail</h2>
<table border="1">
<tr bgcolor="lightblue">
<th align="left">Area Link</th>
<th align="left">Area Name</th>
</tr>
</table>
</body>
</html> [/code]I am not sure whether namespace has anything to do with it. The following XPath statement has worked on the same state.xml document:
XPath stateXPath = XPath.newInstance(
"/ns:html/ns:body/ns:div[@id='content']/ns:table[@class='sresults']/ns:tr/ns:td/ns:a");
stateXPath.addNamespace("ns", "http://www.w3.org/1999/xhtml");
I am using JDK1.6, Sax6.5.5, TagSoup 1.2, Resolver 1.2, JDOM1.1, Netbeans 6.1 on Windows XP platform.
Any assistance would be appreciated.
Thanks,
Jack