tags:

views:

34

answers:

1

I have a xsl file for an xml. The location of the the xml files should be configurable (that's done by configuring the href path to stylesheet in the xml) but the xsl is using some images and some othe javaScript files and need to hav teh path to them. The path is right near the stylesheet file so once I can get the xsl directory I can ge to them. for example: in my xml I have:?xml-stylesheet type="text/xsl" href=".\Files\Style\test.xsl"> I want from within the xsl to point to ".\Files\Style" for the images' location Hoe can I do this

+1  A: 

Here is an XSLT 1.0 solution (XSLT 2.0 has much more powerful features for string processing, such as regular expressions):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output method="text"/>

 <xsl:template match="processing-instruction()">
   <xsl:variable name="vpostHref"
    select="substring-after(., 'href=')"/>

   <xsl:variable name="vhrefData1"
    select="substring($vpostHref,2)"/>

   <xsl:variable name="vhrefData2"
    select="substring($vhrefData1, 1,
                      string-length($vhrefData1)-1
                      )"/>

   <xsl:call-template name="stripBackwards">
    <xsl:with-param name="pText"
      select="$vhrefData2"/>
    <xsl:with-param name="pTextLength"
     select="string-length($vhrefData2)"/>
   </xsl:call-template>
 </xsl:template>

 <xsl:template name="stripBackwards">
  <xsl:param name="pText"/>
  <xsl:param name="pStopChar" select="'\'"/>
  <xsl:param name="pTextLength"/>

  <xsl:choose>
   <xsl:when test="not(contains($pText, $pStopChar))">
     <xsl:value-of select="$pText"/>
   </xsl:when>
   <xsl:otherwise>
     <xsl:variable name="vLastChar"
       select="substring($pText,$pTextLength,1)"/>
     <xsl:choose>
       <xsl:when test="$vLastChar = $pStopChar">
        <xsl:value-of select="substring($pText,1,$pTextLength -1)"/>
       </xsl:when>
       <xsl:otherwise>
        <xsl:call-template name="stripBackwards">
          <xsl:with-param name="pText"
           select="substring($pText,1,$pTextLength -1)"/>
          <xsl:with-param name="pTextLength" select="$pTextLength -1"/>
          <xsl:with-param name="pStopChar" select="$pStopChar"/>
        </xsl:call-template>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<?xml-stylesheet type="text/xsl" href=".\Files\Style\test.xsl"?>
<t/>

the correct result is produced:

.\Files\Style
Dimitre Novatchev
Thank you!!!looks exactly what I was looking for I'll try it out