Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt 子串操作_Xslt_Xslt 2.0 - Fatal编程技术网

Xslt 子串操作

Xslt 子串操作,xslt,xslt-2.0,Xslt,Xslt 2.0,我正在将html转换为XML,一切都很好,但我在将原始路径结构转换为新路径结构方面有点困难 下面的示例说明了我想要实现的目标: 我现在拥有的(简化示例): ../../../folder1/folder2/folder3/folder4/somefile.png ../../../folderra/folderb/folder/some_other_file.gif 文件夹\u x/yet\u另一个\u file.jpg 我想要的是: <links> <link>so

我正在将html转换为XML,一切都很好,但我在将原始路径结构转换为新路径结构方面有点困难

下面的示例说明了我想要实现的目标:

我现在拥有的(简化示例):


../../../folder1/folder2/folder3/folder4/somefile.png
../../../folderra/folderb/folder/some_other_file.gif
文件夹\u x/yet\u另一个\u file.jpg
我想要的是:

<links>
<link>somefile.png</link>
<link>some_other_file.gif</link>
<link>yet_another_file.jpg</link>
</links>

somefile.png
some_other_file.gif
还有另一个文件.jpg
或者换句话说,使用xslt(2)获取文本中最后一个“/”后面的字符串的最简单方法是什么


谢谢你的建议

编辑:这不起作用,请参见注释

您可以使用
子字符串after
函数来实现这一点。迭代
链接
节点,路径将为

<xsl:variable name="lastPart" select="substring-after(text(.), '/')"/>


$lastPart
现在将包含字符串中最后一个
/
右侧的内容。如果没有这样的字符,这将返回空字符串,因此您可能需要对此进行额外检查。

编辑:这不起作用,请参阅注释

您可以使用
子字符串after
函数来实现这一点。迭代
链接
节点,路径将为

<xsl:variable name="lastPart" select="substring-after(text(.), '/')"/>

$lastPart
现在将包含字符串中最后一个
/
右侧的内容。如果没有这样的字符,这将返回空字符串,因此您可能需要对此进行额外检查。

尝试使用

<xsl:output method="text"/>

<xsl:function name="xfn:substringAfterLast">
  <xsl:param name="arg"/> 
  <xsl:param name="delim"/> 

  <xsl:sequence select="replace ($arg,concat('^.*',xfn:escapeForRegex($delim)),'')"/>
</xsl:function>

<xsl:function name="xfn:escapeForRegex" >              
    <xsl:param name="arg"/> 
   <xsl:sequence select="replace($arg, '(\.|\[|\]|\\|\||\-|\^|\$|\?|\*|\+|\{|\}|\(|\))','\\$1')"/>
</xsl:function>

<xsl:template match="/">
    <xsl:value-of select="xfn:substringAfterLast('../../../folder1/folder2/folder3/folder4/somefile.png', '/')"/>
    <xsl:apply-templates/>
</xsl:template>

当然,这还需要额外的名称空间声明。

尝试使用

<xsl:output method="text"/>

<xsl:function name="xfn:substringAfterLast">
  <xsl:param name="arg"/> 
  <xsl:param name="delim"/> 

  <xsl:sequence select="replace ($arg,concat('^.*',xfn:escapeForRegex($delim)),'')"/>
</xsl:function>

<xsl:function name="xfn:escapeForRegex" >              
    <xsl:param name="arg"/> 
   <xsl:sequence select="replace($arg, '(\.|\[|\]|\\|\||\-|\^|\$|\?|\*|\+|\{|\}|\(|\))','\\$1')"/>
</xsl:function>

<xsl:template match="/">
    <xsl:value-of select="xfn:substringAfterLast('../../../folder1/folder2/folder3/folder4/somefile.png', '/')"/>
    <xsl:apply-templates/>
</xsl:template>

当然,这还需要额外的名称空间声明

<xsl:template match="link">
  <xsl:copy>
    <xsl:value-of select="tokenize(., '/')[last()]"/>
  </xsl:copy>
</xsl:template>

当然还有身份转换,例如

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>

简单地做例如

<xsl:template match="link">
  <xsl:copy>
    <xsl:value-of select="tokenize(., '/')[last()]"/>
  </xsl:copy>
</xsl:template>

当然还有身份转换,例如

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* , node()"/>
  </xsl:copy>
</xsl:template>


最好检查一下你的推荐信。。。“函数后的子字符串返回第一个参数字符串的子字符串,该字符串位于第二个参数字符串第一次出现之后…”最好检查引用。。。“函数后的子字符串返回第一个参数字符串的子字符串,该子字符串位于第二个参数字符串第一次出现之后…”同意:-)使用Martin Honnen建议的标记化解决方案更好!同意:-)使用Martin Honnen建议的令牌化解决方案更好!