【问题标题】:Getting the stylesheet directory from within the XSL file- for configureable xml location从 XSL 文件中获取样式表目录 - 用于可配置的 xml 位置
【发布时间】:2011-01-21 20:57:51
【问题描述】:

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

【问题讨论】:

  • 查看您的问题的解决方案 :)
  • 提示:如果提供的答案解决了问题,您可以通过单击问题左上方的复选标记接受。您也可以投票,具体取决于您目前获得的分数(我认为您必须有 50 分才能获得投票权)。 :)
  • 谢谢,我仍然需要你的帮助 :) 我有一个 xsl 转换为我的 xsl 中的 html 我有一些模板和一些 java 脚本函数将信息传递给其他模板的正确方法是什么对于 jScript - 如果我在相同的字段 2 中使用 match 模板化是否有问题?因为我没能走上这条路。这可能是一个初学者的问题,但我在这个领域很新。感谢您的帮助
  • 这是一个新的问题,而且表述得不是很好——请将其作为一个单独的问题提出,提供代码示例并尽可能准确地表述您的问题。

标签: xslt xslt-2.0


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多