Altova Mailing List Archives>Archive Index >xml-dev Archive Home >Recent entries >Thread Prev - >Thread Next - Re: [xml-dev] XSLT Stylesheet query (newbie) XSLT Stylesheet query (newbie)To: xml-dev@-----.---.--- Date: 1/12/2009 10:53:00 PM Hi All,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p> <o:p> </o:p> I need some advice on how to retrieve the value of 2 attributes (@href, @title) in state.xml as part of transformation as follows:<o:p></o:p> <o:p> </o:p> <?xml version="1.0" encoding="UTF-8" ?> <o:p></o:p> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><o:p></o:p> - <html xmlns="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml"><o:p></o:p> - <head><o:p></o:p> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <o:p></o:p> ………..<o:p></o:p> </head><o:p></o:p> - <body><o:p></o:p> <a shape="rect" name="top" /> <o:p></o:p> + <div id="container"><o:p></o:p> - <div id="content"><o:p></o:p> + <div id="as1"><o:p></o:p> + <div id="alphalinks"><o:p></o:p> <h1> Listing of all the states</h1> <o:p></o:p> + <p><o:p></o:p> - <table class="sresults"><o:p></o:p> - <tr><o:p></o:p> - <td colspan="1" rowspan="1"><o:p></o:p> <a shape="rect" href="http://www.abc.com/areas/CA/fairy+land" title="<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:place><st1:City>Fairy Land</st1:City>, <st1:State><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 10pt; COLOR: windowtext; FONT-FAMILY: Arial; mso-bidi-font-family: 'Courier New'">CA</st1:State></st1:place>"><st1:place><st1:PlaceName>Fairy</st1:PlaceName> <st1:PlaceType>Land</st1:PlaceType></st1:place></a> <o:p></o:p> </td><o:p></o:p> - <td colspan="1" rowspan="1"><o:p></o:p> <a shape="rect" href="http://www.abc.com/areas/CA/wonder+land" title="<st1:place><st1:City>Wonder Land</st1:City>, <st1:State>CA</st1:State></st1:place><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 10pt; COLOR: windowtext; FONT-FAMILY: Arial; mso-bidi-font-family: 'Courier New'">"><st1:place><st1:PlaceName>Wonder</st1:PlaceName> <st1:PlaceType>Land</st1:PlaceType></st1:place></a> <o:p></o:p> </td><o:p></o:p> ……….<o:p></o:p> </tr><o:p></o:p> </body><o:p></o:p> </html><o:p></o:p> <o:p> </o:p> <o:p> </o:p><o:p> </o:p> Below is the content of stateStyleSheet.xsl:<o:p></o:p> <o:p> </o:p> <?xml version="1.0" encoding="ISO-8859-1"?><o:p></o:p> <xsl:stylesheet version="1.0"<o:p></o:p> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><o:p></o:p> <o:p></o:p> <xsl:template match="/"><o:p></o:p> <html><o:p></o:p> <body><o:p></o:p> <h2>Transformed State Detail</h2><o:p></o:p> <table border="1"><o:p></o:p> <tr bgcolor="lightblue"><o:p></o:p> <th align="left">Area Link</th><o:p></o:p> <th align="left">Area Name</th><o:p></o:p> </tr><o:p></o:p> <xsl:for-each select="/html/body/div[@id='content']/table[@class='sresults']/tr/td/a"><o:p></o:p> <tr><o:p></o:p> <td><xsl:value-of select="@href"/></td><o:p></o:p> <td><xsl:value-of select="@title"/></td><o:p></o:p> </tr><o:p></o:p> </xsl:for-each><o:p></o:p> </table><o:p></o:p> </body><o:p></o:p> </html><o:p></o:p> </xsl:template><o:p></o:p> <o:p></o:p> </xsl:stylesheet><o:p></o:p> <o:p> </o:p> <o:p> </o:p> <o:p></o:p>Here is state.java together with XML Catalog to bring everything together:<o:p></o:p> <o:p> </o:p> SAXBuilder stateBuilder = new SAXBuilder("org.ccil.cowan.tagsoup.Parser", false); <o:p></o:p> FileInputStream stateIS = new FileInputStream("E:\\state.xml");<o:p></o:p> BufferedInputStream stateBIS = new BufferedInputStream(stateIS);<o:p></o:p> Document stateOriginaljdomDocument = stateBuilder.build(stateBIS);<o:p></o:p> <o:p></o:p> TransformerFactory stateFactory = TransformerFactory.newInstance();<o:p></o:p> FileInputStream styleSheetIS = new FileInputStream("E:\\stateStyleSheet.xsl");<o:p></o:p> BufferedInputStream styleSheetBIS = new BufferedInputStream(styleSheetIS);<o:p></o:p> Transformer stateTransformer = stateFactory.newTransformer(new StreamSource(styleSheetBIS));<o:p></o:p> ……<o:p></o:p> <o:p> </o:p> Output from transformation process gives:<o:p></o:p> <o:p> </o:p> <?xml version="1.0" encoding="UTF-8"?><o:p></o:p> <html><o:p></o:p> <body><o:p></o:p> <h2>Transformed State Detail</h2><o:p></o:p> <table border="1"><o:p></o:p> <tr bgcolor="lightblue"><o:p></o:p> <th align="left">Area Link</th><o:p></o:p> <th align="left">Area Name</th><o:p></o:p> </tr><o:p></o:p> </table><o:p></o:p> </body><o:p></o:p> </html> <o:p></o:p> <o:p></o:p> I am not sure whether namespace has anything to do with it. The following XPath statement has worked on the same state.xml document:<o:p></o:p> <o:p> </o:p> XPath stateXPath = XPath.newInstance(<o:p></o:p> "/ns:html/ns:body/ns:div[@id='content']/ns:table[@class='sresults']/ns:tr/ns:td/ns:a");<o:p></o:p> stateXPath.addNamespace("ns", "http://www.w3.org/1999/xhtml");<o:p></o:p> <o:p> </o:p> I am using JDK1.6, Sax6.5.5, TagSoup 1.2, Resolver 1.2, JDOM1.1, Netbeans 6.1 on Windows XP platform.<o:p></o:p> <o:p> </o:p> Any assistance would be appreciated.<o:p></o:p> <o:p> </o:p> Thanks,<o:p></o:p> <o:p> </o:p> Jack<o:p></o:p> <o:p> </o:p> Stay connected to the people that matter most with a smarter inbox. Take a look. | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
