Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can xslt determine an image width?

Status
Not open for further replies.

mountainbiker

Programmer
Aug 21, 2002
122
GB
when rending a xml instance to html using xslt, i need to determine the width (and height) of an image. the only info i have from the xml is its source. i am using the xalan-j xslt processor. are there any extensions build-in to help with this? will i have to add a java extension? or is there some other clever way?
 
here is some rough code that i need to convert to xslt, but it gives an idea of one possible solution to my problem. i'd use xpath to grab all the "picture" elements to build the preloadimages javascript line. i would like to have the dtd and xml instance provide the width and height (but that won't be possible for sometime. the joy of doing things by committee.) still looking for some other cool solutions like a java class and/or xalan extension, so don't be shy--answer away.

Code:
<html>
	<head>
	
		<title>my title</title>
		
		<script language=&quot;javascript&quot;>
var myimages=new Array()
function preloadimages(){
//alert(&quot;entered&quot;)
for (i=0;i<preloadimages.arguments.length;i++){
myimages[i]=new Image()
myimages[i].src=preloadimages.arguments[i]
}
}
preloadimages(&quot;Geri-Halliwell.jpg&quot;,&quot;a.gif&quot;,&quot;b.jpg&quot;,&quot;c.gif&quot;);

function imagetable(id, title, caption)
{
		var s = &quot;&quot;
		
		s += &quot;<table width=\&quot;&quot; + myimages[id].width + &quot;\&quot;>&quot;
		s += 		&quot;<tr>&quot;
		s += 				&quot;<td align=\&quot;right\&quot;>&quot; + title + &quot;</td>&quot;
		s += 		&quot;</tr>&quot;
		s += 		&quot;<tr>&quot;
		s += 				&quot;<td>&quot;
		s += 						&quot;<img src=\&quot;&quot; + myimages[id].src + &quot;\&quot; width=\&quot;&quot; +  myimages[id].width   + &quot;\&quot;>&quot;
		s += 				&quot;</td>&quot;
		s += 		&quot;</tr>&quot;
		s += 		&quot;<tr>&quot;
		s += 				&quot;<td>&quot; + caption + &quot;</td>&quot;
		s += 		&quot;</tr>&quot;
		s += &quot;</table>&quot;
		
		alert(s)
		document.writeln(s)
}

function imageimg(id)
{
		s = &quot;<img src=\&quot;&quot; + myimages[id].src + &quot;\&quot; width=\&quot;&quot; +  myimages[id].width   + &quot;\&quot;>&quot;
		//alert(s)
		return(s)
}

</script>
<body onload=&quot;preloadimages()&quot;>
	</head>
	<body>
		<p>look:</p>
		<script language=&quot;javascript&quot; type=&quot;text/javascript&quot;>
      //alert(myimages[0].src)
      </script>
      
	<script language=&quot;javascript&quot; type=&quot;text/javascript&quot;>
		imagetable(0, &quot;geri&quot;, &quot;ahahah ahahah hahahh ananann ananan ananna annnana nannana anannan annanana anannan annnana annnana&quot;)
	</script>

	<script language=&quot;javascript&quot; type=&quot;text/javascript&quot;>
//      alert(document.images[0].width)
      </script>
	</body>
</html>
 
okay i found Marco Schmidt's ImageInfo java class:


It has methods getWidth() and getHeigth(), but I am having a heck of time trying to call em from my XSLT and using the Xalan-Java XSLT processor.

I get the following error from Xalan

Code:
file:javatest.xsl; Line #13; Column #88; XSLT Error (javax.xml.transform.TransformerException): java.lang
.NoSuchMethodException: For extension function, could not find method static ImageInfo.setInput([ExpressionContext,] #STRING).

My javatest.xsl looks like

Code:
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<xsl:stylesheet
  xmlns:xsl=&quot;[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform&quot;[/URL]
  xmlns:java=&quot;[URL unfurl="true"]http://xml.apache.org/xalan/java&quot;[/URL]
  version=&quot;1.1&quot;>

  <xsl:output method=&quot;html&quot;/>

  <xsl:template match=&quot;/&quot;>
    <html>
      <body bgcolor=&quot;#FFFFFF&quot;>
        <h2>Test</h2>
        <xsl:variable name=&quot;img&quot; select=&quot;'c:\WORLD.gif'&quot;/>
        <xsl:variable name=&quot;imgObject&quot; select=&quot;java:ImageInfo.setInput($img)&quot;/>
      
<!-- ZoneLabs Popup Blocking Insertion -->
<script language='javascript'>postamble();</script>
</body>
    </html>
  </xsl:template>
</xsl:stylesheet>

I am able to call ImageInfo form the command line with

Code:
java ImageInfo c:\WORLD.gif

I appreciate if you can show me the errors of my ways.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top