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?
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.
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.
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.
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:
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With