开发者

Getting the stylesheet directory from within the XSL file- for configureable xml location

I have a xsl file for an xml. The location of the the xml files should be configur开发者_运维知识库able (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


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">
 <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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜