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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

tricky problem: determine size styles of a picture in xsl 2

Status
Not open for further replies.

Crowley16

Technical User
Jan 21, 2004
6,931
0
0
GB
Hi

I'm pretty new to xml so forgive me if this is very simple...

I've got a xml file, and am writing a xslt for wordml.

basically, when I'm linking pictures, I'd like to determine the size of the picture and dynamically set the style properties to fit the picture to the page and maintain aspect ratio.

at the moment all I have is hard coded width and height properties...

Code:
		<w:p>
			<w:pict>
				<v:shape id="" type="" style="[b]width:420pt;height:260pt[/b];z-index:1pt">
					<v:imagedata src="{@src}" o:title="{@id}"/>
					<w10:bordertop type="single" width="4"/>
					<w10:borderleft type="single" width="4"/>
					<w10:borderbottom type="single" width="4"/>
					<w10:borderright type="single" width="4"/>					
				</v:shape>
			</w:pict>
		</w:p>

any ideas on how to achieve this?

Thanks

--------------------
Procrastinate Now!
 
Can the process creating the XML document also place the height and width in the XML document? If so, then the solution is obvious.

Tom Morrison
 
unfortunately no...

the xml document is also being hand written by myself...

I am thinking maybe there's something I can do with a function or something that will work out the values to use, but frankly I don't even know where to start with that...

Like I said, I'm new to xml...

--------------------
Procrastinate Now!
 
Perhaps my previous answer is a bit too terse (but it is the easiest solution from the perspective of the stylesheet writer).

Harder to accomplish is to use extension functions, which might be useful if you have access at translation time to the images. Further advice on extension functions probably would require us to know what XSL processor you are using.

Tom Morrison
 
doing this all by hand, and viewing the results in word 2003...

--------------------
Procrastinate Now!
 
Ok, deep breath... :-D

From your stylesheet fragment, we can tell that you are in the context of an element representing an image, and you have at least two attributes named src and id. If you can place additional attributes in the XML (which you can because it is hand coded!), then let's do so. For the purpose of this example, call those attributes iheight and iwidth. Then the following would do the trick:
Code:
<v:shape id="" type="" >
      <xsl:attribute name="style">
          <xsl:text>width:</xsl:text>
          <xsl:value-of select="@iwidth"/>
          <xsl:text>;height:</xsl:text>
          <xsl:value-of select="@iheight"/>
          <xsl:text>;z-index:1pt</xsl:text>
      </xsl:attribute>
      <v:imagedata src="{@src}" o:title="{@id}"/>

xsl:attribute allows you to piece together an attribute for an output element.

Tom Morrison
 
yeah, the attribute tag, thanks, have a star...

however, is there any way to automatically determine the size of the picture without having to hard code the size into the xml?

there will most likely be lots of pictures, so I was hopeing of a way to avoid manually working out the picture size of each one...

--------------------
Procrastinate Now!
 
Not entirely sure what the benefits of using xsl:attribute are in this instance.

Surely a cleaner way to write the above would be:

<v:shape id="" type="" style="width:{@iwidth};height:{@iheight};zindex:1pt">

In regards to your original question you would have to look into extension functions.
 
Crowley16 said:
I was hopeing of a way to avoid manually working out the picture size of each one...

(Trying to get this answered before end-of-day in GB...)

There probably is a way to determine automatically the picture geometry if the pictures are available to the XSLT processor at translation time. This is where extension functions come to your aid. Please specify which XSLT processor you are using (including software version number) and the computing platform (Windows, Linux, etc) that you are using (could be all Windows, since you are targeting Word, but better to know for sure). Extension functions are an area where standardization has been less successful.

MrGrey said:
Not entirely sure what the benefits of using xsl:attribute are in this instance.
Attribute value templates (those things inside the brace characters) are favored by some, but I find them a source of hard-to-find coding errors. While I may have coded more source code, I deem it more readable and maintainable, metrics that I value more than brevity.

Tom Morrison
 
I've actually modified my code to calculate the aspect ratio and set the picture dimensions based on the max width of page and the aspect ratio of original picture, and this was much easier using the xsl:attribute tag. but if I didn't need to do that, then I probably would have gone the {} course, just cos I'm lazy...

anyway, the xslt processor I'm using is word 2003, and I'm on xp sp2... probably not going to be likely that word can process vbscript/javascript

--------------------
Procrastinate Now!
 
Crowley16 said:
the xslt processor I'm using is word 2003
It is most likely, since you are using Word 2003, that your XSLT processor is MSXML. The question to be answered now (if you would still need extension functions) is what version of MSXML are you using. Microsoft has made some changes in the extension function capability, I believe. Here is a starting spot for exploring the options you have available to extend MSXML.

Tom Morrison
 
that's a very promising link...

although quite a lot to get through :)

--------------------
Procrastinate Now!
 
right, have finally cracked it after ages of messing about and getting distracted with "work" *shudders at prospect*

anyway, the relevant code snippets are below for anyone who's interested:

Code:
<xsl:stylesheet version="1.0" 
	xmlns:ms="urn:schemas-microsoft-com:xslt" 
	xmlns:vb="urn:the-xml-files:xslt-vb"
>

   <ms:script language="VBScript" implements-prefix="vb">
   <![CDATA[
      function GetPictureAspect(path)
         set pic = LoadPicture(path)
         GetPictureAspect = pic.height/pic.width
      end function 
   ]]>
   </ms:script>	

   <xsl:template match="img">
      <w:p></w:p>
      <w:p>
         <w:pict>
            <v:shape id="" type="">
               <xsl:attribute name="style">
                  <xsl:text>width:420pt;height:</xsl:text>
                  <xsl:value-of select="vb:GetPictureAspect(string(@src)) * 420"/>
                  <xsl:text>pt;z-index:1pt</xsl:text>
               </xsl:attribute>
               <v:imagedata src="{@src}" o:title="{@id}"/>
               <w10:bordertop type="single" width="4"/>
               <w10:borderleft type="single" width="4"/>
               <w10:borderbottom type="single" width="4"/>
               <w10:borderright type="single" width="4"/>	
            </v:shape>
         </w:pict>
      </w:p>
      <w:p></w:p>
   </xsl:template>

thanks for all the help guys :)
and if anyone can see a better way of doing this, then please speak up...

--------------------
Procrastinate Now!
 
Crowley16,

Thank you for carrying through, finding the answer, and reporting back. That definitely earns a star!

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top