1

我有一个用于 xml 的 xsl 文件。xml 文件的位置应该是可配置的(通过在 xml 中配置样式表的 href 路径来完成),但是 xsl 使用一些图像和一些其他 javaScript 文件,并且需要它们的路径。该路径就在样式表文件附近,因此一旦我可以获取 xsl 目录,我就可以找到它们。例如:在我的 xml 我有:?xml-stylesheet type="text/xsl" href=".\Files\Style\test.xsl"> 我想从 xsl 中指向“.\Files\Style”对于图像的位置,我可以这样做吗

4

1 回答 1

1

这是一个 XSLT 1.0 解决方案(XSLT 2.0 具有更强大的字符串处理功能,例如正则表达式):

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

当此转换应用于以下 XML 文档时

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

产生正确的结果

.\Files\Style
于 2010-03-01T14:39:14.393 回答