Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


XSLT Stylesheet query (newbie)

From: Jack Bush <netbeansfan@-----.---.-->
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.


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent