xml - XSLT - 使用 string-after 操作字符串的递归算法

标签 xml xslt recursion substring

我需要一些帮助来尝试“分解”我只想检索其末尾(文件名)的路径。

例如,这是我必须处理的输入 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/

相关文章:

java - 递归方法是否允许创建多个同名变量?

C - 从递归返回后指针值发生变化

SqlBulkCopy 无法尝试复制 XML 列中包含大量内容的行

mysql - 将 XML 导入 phpmyadmin 数据库

java - jaxb 的 XML 解析问题

python - 如何解析grep生成的文件中的数据?

xml - 错误 : Could not find or load main class net. sf.saxon.Transform

java xslt 转换本地名称

xml - 用于匹配文档中除特定元素中的文本节点之外的所有文本节点的 Xpath

algorithm - 不均匀划分递归算法复杂度分析