XSLT 可以解析文本字符串吗?

发布于 2024-08-07 17:50:55 字数 248 浏览 12 评论 0原文

这是我第一次使用 XSLT。我正在尝试创建一个文件,将从我使用的程序导出的 XML 数据文件转换为 HTML 报告。

该元素的值之一是图像文件的路径,但生成的路径是绝对路径,例如

C:\Documents and Settings\me\Desktop\xml export\cd000402.jpg

但我想要一个仅到文件名的相对路径。

有没有办法通过 XLST 文件解析出文件名?

This is my first time using XSLT. I'm trying to create a file that will convert an XML data file exported from a program I use to an HTML report.

One of the element's value is path to an image file, but the path generated is an absolute path such as

C:\Documents and Settings\me\Desktop\xml export\cd000402.jpg

but I want a relative path to just the file name.

Is there some way through the XLST file to parse out the file name?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

瞳孔里扚悲伤 2024-08-14 17:50:55

XPath 包含 substring-after 函数它返回另一个字符串第一次出现后的字符串。这本身还不够,但是像下面这样的模板可能会做到这一点:

<xsl:template name="filename-only">
    <xsl:param name="path" />
    <xsl:choose>
        <xsl:when test="contains($path, '\')">
            <xsl:call-template name="filename-only">
                <xsl:with-param name="path" select="substring-after($path, '\')" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$path" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

可用的字符串函数集并不是非常广泛,但我发现它对于 XSLT 中需要的大多数应用程序来说已经足够了。

XPath contains the substring-after function which returns the string following the first occurrence of another string. This isn't enough by itself, but a template such as the following might do it:

<xsl:template name="filename-only">
    <xsl:param name="path" />
    <xsl:choose>
        <xsl:when test="contains($path, '\')">
            <xsl:call-template name="filename-only">
                <xsl:with-param name="path" select="substring-after($path, '\')" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$path" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

The set of string functions available is not terribly extensive, but I've found that it is good enough for most applications that you'll need in XSLT.

神经大条 2024-08-14 17:50:55

This is a little beyond the scope of the question, but Michael Kay has an excellent paper on using XSLT 2 to parse pure text into XML.

殊姿 2024-08-14 17:50:55

是的,请参阅 在 XSLT 2.0 中实现的通用 LR(1) 解析器。(仅 245 行)。

我已经用它实现了 JSON 解析器 和 XPath 2.0 解析器——完全采用 XSLT。

Yes, see a general LR(1) parser implemented in XSLT 2.0. (just in 245 lines).

I have implemented with it a parser for JSON and a parser for XPath 2.0 -- entirely in XSLT.

等风来 2024-08-14 17:50:55

XSLT 借助 XPath 2.0 及其各种 字符串函数 帮助其处理这类事情。

示例:
假设所提到的 [to jpg file] 路径来自类似于

...
<For_HTML>
   <Image1>
      <Path>C:\Documents and Settings\me\Desktop\xml export\cd000402.jpg</Path>
      <Description>Photo of the parking lot</Description>
      <Width>123</Width>
      ...
    </Image1>
</For_HTML>

XSLT 片段的 xml 片段,则看起来像这样:

<xsl:template match='//For_HTML/Image1'> 
     <img src='http://myNewServer.com/ImageBin/{substring-after(./Path,"\xml export\")}'
          alt='{./Description}'
          width=' ....  you got the idea'
     /> 
</xsl:template>

注意:没有时间测试;但这看起来是对的。

XSLT with the help of XPath 2.0 and its various string functions helps it deal with this type of things.

Example:
Assuming the path [to jpg file] mentioned in question comes from an xml snippet similar to

...
<For_HTML>
   <Image1>
      <Path>C:\Documents and Settings\me\Desktop\xml export\cd000402.jpg</Path>
      <Description>Photo of the parking lot</Description>
      <Width>123</Width>
      ...
    </Image1>
</For_HTML>

XSLT snippet would look something like

<xsl:template match='//For_HTML/Image1'> 
     <img src='http://myNewServer.com/ImageBin/{substring-after(./Path,"\xml export\")}'
          alt='{./Description}'
          width=' ....  you got the idea'
     /> 
</xsl:template>

Note: didn't have time to test; but that looks about right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文