是否可以使用XSLT1.0切掉URL的结尾?

是否可以使用XSLT1.0切掉URL的结尾?,xslt,string,Xslt,String,仅使用XSLT1.0的字符串函数,如何切掉url的结尾 所以从 http://stackoverflow.com/questions/2981175/is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0 我想提取 是否可以使用xslt-1-0对url的结尾进行切片 这可能吗?不幸的是,XSLT/XPath 1.0中最后一个函数之后没有子字符串。因此,要获取URL的最后一部分,您必须编写递归模板,如下所示: 该模板的名称如下: <x

仅使用XSLT1.0的字符串函数,如何切掉url的结尾

所以从

http://stackoverflow.com/questions/2981175/is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0

我想提取

是否可以使用xslt-1-0对url的结尾进行切片


这可能吗?

不幸的是,XSLT/XPath 1.0中最后一个函数之后没有子字符串。因此,要获取URL的最后一部分,您必须编写递归模板,如下所示:


该模板的名称如下:

<xsl:call-template name="substring-after-last">
  <xsl:with-param name="string" select="$url" />
  <xsl:with-param name="delimiter" select="'/'" />
</xsl:call-template>

I.使用递归调用的命名模板

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

 <xsl:template match="/">
  <xsl:call-template name="eatAllSlashes">
   <xsl:with-param name="pText" select="."/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="eatAllSlashes">
  <xsl:param name="pText"/>

  <xsl:choose>
    <xsl:when test="not(contains($pText,'/'))">
      <xsl:value-of select="$pText"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:call-template name="eatAllSlashes">
       <xsl:with-param name="pText"
        select="substring-after($pText, '/')"/>
     </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
<t>http://stackoverflow.com/questions/2981175/is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0</t>
is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" exclude-result-prefixes="xsl my">
 <xsl:import href="iter.xsl"/>

 <xsl:output method="text"/>

  <my:condition/>
  <my:skipSlash/>

  <xsl:variable name="vfunCondition"
   select="document('')/*/my:condition"/>

  <xsl:variable name="vfunSkipSlash"
   select="document('')/*/my:skipSlash"/>

  <xsl:template match="/">
    <xsl:call-template name="iterUntil">
      <xsl:with-param name="pCond" select="$vfunCondition"/>
      <xsl:with-param name="pFun" select="$vfunSkipSlash"/>
      <xsl:with-param name="arg1" select="string(/)"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="my:condition">
    <xsl:param name="arg1"/>

    <xsl:value-of select="number(not(contains($arg1, '/')))"/>
  </xsl:template>

  <xsl:template match="my:skipSlash">
    <xsl:param name="arg1"/>

    <xsl:value-of select="substring-after($arg1, '/')"/>
  </xsl:template>
</xsl:stylesheet>
is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0
此转换

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

 <xsl:template match="/">
  <xsl:call-template name="eatAllSlashes">
   <xsl:with-param name="pText" select="."/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="eatAllSlashes">
  <xsl:param name="pText"/>

  <xsl:choose>
    <xsl:when test="not(contains($pText,'/'))">
      <xsl:value-of select="$pText"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:call-template name="eatAllSlashes">
       <xsl:with-param name="pText"
        select="substring-after($pText, '/')"/>
     </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
<t>http://stackoverflow.com/questions/2981175/is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0</t>
is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" exclude-result-prefixes="xsl my">
 <xsl:import href="iter.xsl"/>

 <xsl:output method="text"/>

  <my:condition/>
  <my:skipSlash/>

  <xsl:variable name="vfunCondition"
   select="document('')/*/my:condition"/>

  <xsl:variable name="vfunSkipSlash"
   select="document('')/*/my:skipSlash"/>

  <xsl:template match="/">
    <xsl:call-template name="iterUntil">
      <xsl:with-param name="pCond" select="$vfunCondition"/>
      <xsl:with-param name="pFun" select="$vfunSkipSlash"/>
      <xsl:with-param name="arg1" select="string(/)"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="my:condition">
    <xsl:param name="arg1"/>

    <xsl:value-of select="number(not(contains($arg1, '/')))"/>
  </xsl:template>

  <xsl:template match="my:skipSlash">
    <xsl:param name="arg1"/>

    <xsl:value-of select="substring-after($arg1, '/')"/>
  </xsl:template>
</xsl:stylesheet>
is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0
II。使用库

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

 <xsl:template match="/">
  <xsl:call-template name="eatAllSlashes">
   <xsl:with-param name="pText" select="."/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="eatAllSlashes">
  <xsl:param name="pText"/>

  <xsl:choose>
    <xsl:when test="not(contains($pText,'/'))">
      <xsl:value-of select="$pText"/>
    </xsl:when>
    <xsl:otherwise>
     <xsl:call-template name="eatAllSlashes">
       <xsl:with-param name="pText"
        select="substring-after($pText, '/')"/>
     </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
<t>http://stackoverflow.com/questions/2981175/is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0</t>
is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my" exclude-result-prefixes="xsl my">
 <xsl:import href="iter.xsl"/>

 <xsl:output method="text"/>

  <my:condition/>
  <my:skipSlash/>

  <xsl:variable name="vfunCondition"
   select="document('')/*/my:condition"/>

  <xsl:variable name="vfunSkipSlash"
   select="document('')/*/my:skipSlash"/>

  <xsl:template match="/">
    <xsl:call-template name="iterUntil">
      <xsl:with-param name="pCond" select="$vfunCondition"/>
      <xsl:with-param name="pFun" select="$vfunSkipSlash"/>
      <xsl:with-param name="arg1" select="string(/)"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="my:condition">
    <xsl:param name="arg1"/>

    <xsl:value-of select="number(not(contains($arg1, '/')))"/>
  </xsl:template>

  <xsl:template match="my:skipSlash">
    <xsl:param name="arg1"/>

    <xsl:value-of select="substring-after($arg1, '/')"/>
  </xsl:template>
</xsl:stylesheet>
is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0
请注意:

<t>http://stackoverflow.com/questions/2981175/is-it-possible-to-slice-the-end-of-a-url-with-xslt-1-0</t>
  • 模板
    iterUntil
    有三个参数:

    --
    pCond
    ——检查当前结果的条件并可能发出“停止信号”(1)的函数(模板引用)

    --
    pFun
    ——用于从当前结果(或最初从输入参数$arg1)生成下一个当前结果的函数(模板引用)

    --
    arg1
    ——初始应用
    pFun
    函数的输入参数

  • 我们的
    pCond
    函数是匹配
    my:condition
    的模板。仅当作为
    $arg1
    传递的字符串不包含任何“/”字符时,它才会发出“停止信号”(输出1)

  • 我们的
    pFun
    函数是匹配
    my:skipSlash
    的模板。它将丢弃字符串
    $arg1

  • 初始输入参数在
    $arg1
    中定义,它是字符串值,只有最后一个“/”之后的文本才能从中生成

  • 使用FXSL的主要优点是,这避免了编写显式递归代码的需要以及执行此操作时出错的可能性。此外,模板/函数非常通用且功能强大,可用于解决大量类似问题


  • my:
    名称空间做什么?@Eric:使用
    的子元素的名称空间元素是一种众所周知的技术,它允许开发人员定义可由XSLT代码访问的全局常量(使用
    文档(“”)/*/prefix:name
    XPath表达式)。好问题(+1)。请参阅我的答案,了解两种不同的完整解决方案。