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!

How to get the relative path of an external entity

Status
Not open for further replies.

rfiroz

Programmer
Oct 19, 2010
2
0
0
US
Hello,

I need a way to get the relative path of an external entity using xsl. Given the following xml:
Code:
<!NOTATION GIF SYSTEM "Graphical Interchange Format">  
<!ENTITY mypicture SYSTEM "temp/me.gif" NDATA GIF>  
   
 ...  
   
<graphic image="mypicture"></graphic>


if I use the following xsl:
Code:
<xsl:value-of select="unparsed-entity-uri('image')"/>

It returns the absolute path from where the transformation occurs. So if I run the transformation from c:\data\xyz\ then unparsed-entity-uri('image') returns c:\data\xyz\temp\me.gif, but I need the value temp/me.gif. How can I do that using xsl.

XML that I need to transform is authored content and I cannot change that. I have to transform the xml to html and it is likely the absolute path will not match the document root, so such references will result in missing graphics when the HTML file is viewed.

Any help is greatly appreciated.
 
If you want to get the relative path, you have to use it in conjunction with resolve-uri(.) with some string manipulation. Like this.
[tt]
<xsl:variable name="abs-path" select="unparsed-entity-uri('image')" />
<xsl:variable name="ref-path" select="replace(resolve-uri(.),'(^.*/)[^/]*$','$1')" />
<xsl:variable name="rel-path" select="substring-after($abs-path,$ref-path)" />
[/tt]
Now, the variable rel-path should hold the desired relative path to the image. (I assume the xsl-engine in use supports well the regex, replace() and substring-after().)
 
Thanks tsuji for the reply. That is cool, but those functions are available in xslt/xpath 2.0. Is there easy way to achieve it in version 1.0?

Thanks in advance.
 
For pure xslt 1.0, I can't think of any, except your application can pass on the base-uri path as a global parameter set on the xsl document. Otherwise, one way or another, extension is most probably needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top