Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an XSLT parse a string of text?

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?

like image 484
Eric Anastas Avatar asked Oct 14 '09 23:10

Eric Anastas


People also ask

What is the difference between substring and XSLT?

Substring is primarily used to return a section of a string or truncating a string with the help of the length provided.XSLT is incorporated with XPath while manipulating strings of text when XSLT access. The Substring function takes a string as an argument with the numbers one or two and an optional length.

What is splitting and manipulating strings in XSLT?

Splitting and Manipulating Strings. XSLT is a language for manipulating XML documents, and XML documents are text. When you're manipulating text, functions for searching strings and pulling out substrings are indispensable for rearranging documents to create new documents.

What is XPath in XSLT?

The XPath string functions incorporated by XSLT give you a lot of power when you're manipulating element character data, attribute values, and any other strings of text that your stylesheet can access.

How to apply XSLT to XML document?

In the same directory create cos.xsl file with the following content. <xsl:value-of select = "." /> This demonstration shows how to apply XSLT to XML Document. The above code selects appropriate nodes from the select and match type. Here the first and last name strings are retrieved before and after the sub-strings. The result is shown as:


3 Answers

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.

like image 122
Greg Hewgill Avatar answered Oct 28 '22 01:10

Greg Hewgill


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.

like image 20
James Sulak Avatar answered Oct 28 '22 01:10

James Sulak


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.

like image 25
Dimitre Novatchev Avatar answered Oct 28 '22 02:10

Dimitre Novatchev