October 19th, 2010, 10:55 AM
How to get the relative path of an external entity
I need a way to get the relative path of an external entity using xsl. Given the following xml:
if I use the following xsl:
<!NOTATION GIF SYSTEM "Graphical Interchange Format">
<!ENTITY mypicture SYSTEM "temp/me.gif" NDATA GIF>
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.
October 19th, 2010, 10:21 PM
I don't think you can with basic XSLT 1.0. What are you using to do the transformations?
November 12th, 2011, 04:00 AM
If you want to get the relative path, you have to use it in conjunction with resolve-uri(.) with some string manipulation. Like this.
<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)" />
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().)