我需要一些帮助来尝试“分解”我只想检索其末尾(文件名)的路径。
例如,这是我必须处理的输入 xml 的类型:
<?xml version="1.0" encoding="utf-16" ?><?xml-stylesheet type="text/xsl" href="C:\Program Files\XenData\Archive Series\Reports\AllDate.xsl"?>
<tape>
<titleheader>Some data</titleheader>
<titleheader2>Barcode:xxxx</titleheader2>
<profile><filename> /this/path/to/some/SHEET.mov </filename></profile>
<profile><filename> /another/path/to/stuff.mov </filename></profile>
...
</tape>
...
<tape>
...
</tape>
在上面,我需要抓取“SHEET”和“stuff”(之前没有任何内容,也没有“.mov”)。
不知道为什么它不起作用 - 也许我只是不理解模板的用法以及 xsl 处理返回值的方式?
这是我的 XSL 尝试:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0" xmlns:XslUtils="java:com.dalet.ip.XslUtils" xmlns:myfunc="/"
exclude-result-prefixes="XslUtils myfunc">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="//tape">
<xsl:variable name="tapeHeader2"><xsl:value-of select="./titleheader2"/></xsl:variable>
<!--<header2><xsl:value-of select="$tapeHeader2"/></header2>-->
<Barcode><xsl:value-of select="substring-after($tapeHeader2, 'Barcode:')"/></Barcode>
<xsl:for-each select="//profile">
<Title>
<xsl:variable name="itemCode"><xsl:value-of select="./filename"/></xsl:variable>
<header2><xsl:value-of select="$itemCode"/></header2>
<xsl:variable name="itemC"><xsl:value-of select="normalize-space(substring-before($itemCode, '.mov'))"/></xsl:variable>
<-!-- calling template to explode the path -->
<fileKey>
<xsl:call-template name="extractItemCode">
<xsl:with-param name="datastring"><xsl:value-of select="$itemC"/></xsl:with-param>
</xsl:call-template>
</fileKey>
</Title>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template name="extractItemCode">
<xsl:param name="datastring"/>
<xsl:if test="starts-with($datastring,'/')">
<xsl:call-template name="extractItemCode">
<xsl:with-param name="datastring"><xsl:value-of select="substring-after($datastring,'/')"/></xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
感谢您的帮助。
最佳答案
In the above, I need to grab "SHEET", and "stuff" (without anything before, and without the '.mov').
怎么样:
<xsl:template name="extractFilename">
<xsl:param name="path"/>
<xsl:choose>
<xsl:when test="contains($path, '/')">
<xsl:call-template name="extractFileName">
<xsl:with-param name="path" select="substring-after($path, '/')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before($path, '.')" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
关于xml - XSLT - 使用 string-after 操作字符串的递归算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24072828/