Discussion:
XSLT : how to link an image ? (relative path problem)
(too old to reply)
Philippe Guglielmetti
2004-01-21 07:19:56 UTC
Permalink
Hello,
I want my XSLT to put an image in the HTML it generates:

<img><xsl:attribute name="src">
<xsl:value-of select="@NAME"/>
</xsl:attribute></img>

@NAME contains a simple filename (the image is in the same dir as the XML
document being parsed)
You guessed : it does not work because the HTML is generated in some temp
directory...
How to get the absolute path (uri) to the XML to concat( ? , @name) ?
Thanks!
--
Philippe Guglielmetti - www.cadml.com
Chris Barber
2004-01-21 10:41:54 UTC
Permalink
What are you using to do the transform and what is the context - an
application, an XML IDE such as Xselerator?

If it's an IDE then you should be able to tell it to generate the transform
in the same dir as the source XSL or XML.

Chris.

"Philippe Guglielmetti" <***@dynabits.com> wrote in message news:400e2818$0$740$***@news.sunrise.ch...
Hello,
I want my XSLT to put an image in the HTML it generates:

<img><xsl:attribute name="src">
<xsl:value-of select="@NAME"/>
</xsl:attribute></img>

@NAME contains a simple filename (the image is in the same dir as the XML
document being parsed)
You guessed : it does not work because the HTML is generated in some temp
directory...
How to get the absolute path (uri) to the XML to concat( ? , @name) ?
Thanks!
--
Philippe Guglielmetti - www.cadml.com
Philippe Guglielmetti
2004-01-21 12:28:42 UTC
Permalink
Post by Chris Barber
What are you using to do the transform and what is the context - an
application, an XML IDE such as Xselerator?
I use MSXML called from some JavaScript on a web page.
Check the "Samples" on www.cadml.org :
1) click the "HTML" on the first "test" line : it works
2) click any "HTML" link under the Delta structure : it doesn't work because
the XML and PNG files are located in a different folder.

How is one supposed to do in "pure" XSLT ?
--
Philippe Guglielmetti - www.dynabits.com
Chris Barber
2004-01-21 12:46:58 UTC
Permalink
You could pass a parameter to the transform holding to root of the folder
reference for the images.

NB: The XSLT isn't generated in a temp folder unless *you* save it there and
as such you should be aware of the folder location and relative filepath for
the images. You could also use a root relative filepath for the images in a
virtual directory such as:

/images/myimage.gif

Chris.
Post by Chris Barber
What are you using to do the transform and what is the context - an
application, an XML IDE such as Xselerator?
I use MSXML called from some JavaScript on a web page.
Check the "Samples" on www.cadml.org :
1) click the "HTML" on the first "test" line : it works
2) click any "HTML" link under the Delta structure : it doesn't work because
the XML and PNG files are located in a different folder.

How is one supposed to do in "pure" XSLT ?
--
Philippe Guglielmetti - www.dynabits.com
Torrent
2004-01-21 13:33:56 UTC
Permalink
eg at the top of your XML/XSLT File do something like
<xsl:variable name="BasePath" select="'../AFolderName/subfolder/'" />

(or you could select it out of the xml document in the same manner

then just go

<img src="{$BasePath}.........">

etc
Post by Chris Barber
You could pass a parameter to the transform holding to root of the folder
reference for the images.
NB: The XSLT isn't generated in a temp folder unless *you* save it there and
as such you should be aware of the folder location and relative filepath for
the images. You could also use a root relative filepath for the images in a
/images/myimage.gif
Chris.
Post by Chris Barber
What are you using to do the transform and what is the context - an
application, an XML IDE such as Xselerator?
I use MSXML called from some JavaScript on a web page.
1) click the "HTML" on the first "test" line : it works
2) click any "HTML" link under the Delta structure : it doesn't work because
the XML and PNG files are located in a different folder.
How is one supposed to do in "pure" XSLT ?
--
Philippe Guglielmetti - www.dynabits.com
Philippe Guglielmetti
2004-01-21 14:26:21 UTC
Permalink
Thanks, but I want a solution that works wherever the xml file is.
Users should be allowed to move the xml (and the pictures) files around...

Now if I put:
<?xml-stylesheet href="http://cadml.org/cadHTML.xslt" type="text/xsl"?>
in my XML files, it works perfectly, meaning that the generated HTML content
"path" is the same as the source XML !

How come that when I run an XSLT explicitely from my JavaScript, the "path"
is the one from the XSLT ?
How to change this to make it work just like with the <?xml-stylesheet ...?>
statement ?
--
Philippe Guglielmetti - www.dynabits.com
Chris Barber
2004-01-21 14:58:18 UTC
Permalink
Because you are considering two separate contexts.

In the first (XML document loaded to browser with referenced stylesheet) the
context of the page is that of the XML.
If you do the second then the context is that of the page that you are
executing the XSLT from.

Can you clarify exactly what you are doing here? I can't see any specific
issue that would be construed as a problem with the browser, XSLT (MSXML),
or the JavaScript.

The location of the XML file and the XSLT file is immaterial - the images
are referenced from wherever the resultant HTML is displayed (I presume you
are loading the HTML into a DIV or frame for display).

Chris.

"Philippe Guglielmetti" <***@dynabits.com> wrote in message news:400e8c08$0$731$***@news.sunrise.ch...
Thanks, but I want a solution that works wherever the xml file is.
Users should be allowed to move the xml (and the pictures) files around...

Now if I put:
<?xml-stylesheet href="http://cadml.org/cadHTML.xslt" type="text/xsl"?>
in my XML files, it works perfectly, meaning that the generated HTML content
"path" is the same as the source XML !

How come that when I run an XSLT explicitely from my JavaScript, the "path"
is the one from the XSLT ?
How to change this to make it work just like with the <?xml-stylesheet ...?>
statement ?
--
Philippe Guglielmetti - www.dynabits.com
Philippe Guglielmetti
2004-01-21 15:34:45 UTC
Permalink
Post by Chris Barber
In the first (XML document loaded to browser with referenced stylesheet) the
context of the page is that of the XML.
Good, that's what I want. I'm happy with
<?xml-stylesheet href="http://cadml.org/cadHTML.xslt" type="text/xsl"?>
as long as it is for a demo...
Post by Chris Barber
If you do the second then the context is that of the page that you are
executing the XSLT from.
Mmmmh... I see... You're right, it's the context of the JavaScript thing, no
the XSLT....

Any way to tweak the "context" to the one from the XML ?
-OR- a way to reach the "context" (path) of the XML file from within the
XSLT (There *should* be an XPath function for this...)
because in production users will want their own stylesheet and process it
the way they want...
--
Philippe Guglielmetti - www.dynabits.com
Chris Barber
2004-01-21 18:22:35 UTC
Permalink
The only way to change context is to load the page that the JavaScript is
executing in from another location in the website (perhaps a consistent
location so that image refs are correct).
You could also code image refs as beginning with '/' and place all images in
a virtual dir off the root of the website?

Chris.
Post by Chris Barber
In the first (XML document loaded to browser with referenced stylesheet) the
context of the page is that of the XML.
Good, that's what I want. I'm happy with
<?xml-stylesheet href="http://cadml.org/cadHTML.xslt" type="text/xsl"?>
as long as it is for a demo...
Post by Chris Barber
If you do the second then the context is that of the page that you are
executing the XSLT from.
Mmmmh... I see... You're right, it's the context of the JavaScript thing, no
the XSLT....

Any way to tweak the "context" to the one from the XML ?
-OR- a way to reach the "context" (path) of the XML file from within the
XSLT (There *should* be an XPath function for this...)
because in production users will want their own stylesheet and process it
the way they want...
--
Philippe Guglielmetti - www.dynabits.com
Neil Smith
2004-01-21 22:19:21 UTC
Permalink
I would try something like the following :

Modify your XSL document :

Find
<xsl:template match="DOCUMENT">

Add
<xsl:template match="DOCUMENT">
<xsl:param name="basehref" select="''"/>

In your createXMLDocument() function, add the following line after >

xslDocument.load(xslt);

Add
var basehref=xml.substr(0,xml..lastIndexOf('/'))+'/';
xslDocument.selectSingleNode("//xsl:param[@name='basehref']").setAttribute("select",basehref);


Finally, add this param to your image href

<img><xsl:attribute name="src"><xsl:value-of select="$basehref"
<xsl:value-of select="concat(@NAME,'.png')"/></xsl:attribute></img>

=============

What this does is, it finds the node within your XSL sheet (which is
*also* an XML document ;-), called 'xsl:param' with an attribute
(@name) called 'basehref. This happens on the selectSingleNode line in
your javascript.

Then, it sets the value of the select attribute in the found node
(using setAttribute), to the value you pass in, your javascript
variable 'basehref'.

In your case, you pass in the relative path to the XML file
(xml.substr(0,xml.lastIndexOf('/')) which you then add to the param.

When your XSL comes to contrstuct the path to the IMG, it concats the
param name="basehref" select value (which is now what you passed in
from your javascript) with the @NAME value and .png.

This should then give you a reference to an image in the same
directory as your XML file ;-)

The technique above is called 'parameterising your stylesheet' and is
something Dimitre showed me once, and is way cool.

Have a hack around with the script - its untested but might only need
minor syntax checks to make it work correctly.

Cheers - Neil.



On Wed, 21 Jan 2004 13:28:42 +0100, "Philippe Guglielmetti"
Post by Philippe Guglielmetti
Post by Chris Barber
What are you using to do the transform and what is the context - an
application, an XML IDE such as Xselerator?
I use MSXML called from some JavaScript on a web page.
1) click the "HTML" on the first "test" line : it works
2) click any "HTML" link under the Delta structure : it doesn't work because
the XML and PNG files are located in a different folder.
How is one supposed to do in "pure" XSLT ?
--
Philippe Guglielmetti - www.dynabits.com
========================================================
CaptionKit http://www.captionkit.com : Produce subtitled
internet media, transcripts and searchable video. Supports
Real Player, Quicktime and Windows Media Player.

VideoChat with friends online, get Freshly Toasted every
day at http://www.fresh-toast.net : NetMeeting solutions
for a connected world.
Dimitre Novatchev
2004-01-22 05:42:31 UTC
Permalink
Post by Philippe Guglielmetti
Hello,
<img><xsl:attribute name="src">
</xsl:attribute></img>
@NAME contains a simple filename (the image is in the same dir as the XML
document being parsed)
You guessed : it does not work because the HTML is generated in some temp
directory...
This might be of interest to you:

http://groups.google.com/groups?q=relative+path+dimitre&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=b8ujil%24dpl9r%241%40ID-152440.news.dfncis.de&rnum=1


Cheers,

Dimitre Novatchev.
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
DFN-CIS NetNews Service
2004-01-21 09:41:10 UTC
Permalink
On 21/01/2004, around 09:40, Philippe Guglielmetti wrote:
PG> How to get the absolute path (uri) to the XML to concat( ? , @name) ?

If the calling routine knows the full path to the image dir then it
could be passed in as a parameter to the stylesheet.
--
Stuart
Suicide is the most sincere form of self-criticism.
Loading...