<?xml version="1.0" encoding="UTF-8"?>


<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:ns3="http://purl.org/dc/terms/" xmlns:ns4="http://lojjic.net/ns/rdf/comments/" xmlns:ns2="http://lojjic.net/ns/rdf/blog/" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">

<rdf:Description rdf:about="http://lojjic.net/blog/20031127-211048">
	<ns2:entry-date rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2003-11-24T10:10:57-07:00</ns2:entry-date>
</rdf:Description>

<rdf:Description rdf:about="http://lojjic.net/ns/rdf/blog/entry-date">
	<rdfs:domain rdf:resource="http://lojjic.net/ns/rdf/blog/Entry" />
</rdf:Description>

<rdf:Description rdf:about="http://lojjic.net/blog/20031127-211048">
	<rdf:type rdf:resource="http://lojjic.net/ns/rdf/blog/Entry" />
	<ns2:entry-title rdf:datatype="http://www.w3.org/2001/XMLSchema#normalizedString">URI Parsing in XSLT</ns2:entry-title>
</rdf:Description>

<rdf:Description rdf:about="http://lojjic.net/ns/rdf/blog/entry-title">
	<rdfs:domain rdf:resource="http://lojjic.net/ns/rdf/blog/Entry" />
</rdf:Description>

<rdf:Description rdf:about="http://lojjic.net/blog/20031127-211048">
	<ns2:entry-content rdf:parseType="Literal"><p xmlns="http://www.w3.org/1999/xhtml">In creating the <acronym xmlns="http://www.w3.org/1999/xhtml" title="eXtensible Stylesheet Language Transformations">XSLT</acronym> transformations for this site, I often ran into situations where I needed to extract either the path or the filename from a <acronym xmlns="http://www.w3.org/1999/xhtml" title="Uniform Resource Identifier">URI</acronym>.  In most programming/scripting languages this is easy to do with standard string manipulation or regular expression functions, but such things are not easily done in XSLT as anyone who&apos;s used it extensively knows. (XSLT 2.0 should supposedly make it much easier, but it&apos;ll be a while before it&apos;s standardized and supported by XSLT tools.)</p>
<p xmlns="http://www.w3.org/1999/xhtml">The solution I found is to create some named templates, which recursively call themselves to process the URI piece-by-piece.  Here are my functions:</p>
<pre xmlns="http://www.w3.org/1999/xhtml"><code xmlns="http://www.w3.org/1999/xhtml">&lt;xsl:template name=&quot;getPath&quot;&gt;
  &lt;xsl:param name=&quot;uri&quot; /&gt;
  &lt;xsl:if test=&quot;contains($uri,&apos;/&apos;)&quot;&gt;
    &lt;xsl:value-of select=&quot;concat(substring-before($uri,&apos;/&apos;),&apos;/&apos;)&quot; /&gt;
    &lt;xsl:variable name=&quot;afterSlash&quot; select=&quot;substring-after($uri,&apos;/&apos;)&quot; /&gt;
    &lt;xsl:call-template name=&quot;getPath&quot;&gt;
      &lt;xsl:with-param name=&quot;uri&quot; select=&quot;$afterSlash&quot; /&gt;
    &lt;/xsl:call-template&gt;
  &lt;/xsl:if&gt;
&lt;/xsl:template&gt;

&lt;xsl:template name=&quot;getFilename&quot;&gt;
  &lt;xsl:param name=&quot;uri&quot; /&gt;
  &lt;xsl:choose&gt;
    &lt;xsl:when test=&quot;contains($uri,&apos;/&apos;)&quot;&gt;
      &lt;xsl:variable name=&quot;afterSlash&quot; select=&quot;substring-after($uri,&apos;/&apos;)&quot; /&gt;
      &lt;xsl:call-template name=&quot;getFilename&quot;&gt;
        &lt;xsl:with-param name=&quot;uri&quot; select=&quot;$afterSlash&quot; /&gt;
      &lt;/xsl:call-template&gt;
    &lt;/xsl:when&gt;
    &lt;xsl:otherwise&gt;
      &lt;xsl:value-of select=&quot;$uri&quot; /&gt;
    &lt;/xsl:otherwise&gt;
  &lt;/xsl:choose&gt;
&lt;/xsl:template&gt;</code></pre>
<p xmlns="http://www.w3.org/1999/xhtml">And they might be used like:</p>
<pre xmlns="http://www.w3.org/1999/xhtml"><code xmlns="http://www.w3.org/1999/xhtml">&lt;xsl:variable name=&quot;theURI&quot;&gt;http://lojjic.net/blog.rdf.html&lt;/xsl:variable&gt;

&lt;xsl:text&gt;The URI: &lt;/xsl:text&gt;
&lt;xsl:value-of select=&quot;$theURI&quot; /&gt;

&lt;xsl:text&gt; --- The Path: &lt;/xsl:text&gt;
&lt;xsl:call-template name=&quot;getPath&quot;&gt;
  &lt;xsl:with-param name=&quot;uri&quot; select=&quot;$theURI&quot; /&gt;
&lt;/xsl:call-template&gt;

&lt;xsl:text&gt; --- The Filename: &lt;/xsl:text&gt;
&lt;xsl:call-template name=&quot;getFilename&quot;&gt;
  &lt;xsl:with-param name=&quot;uri&quot; select=&quot;$theURI&quot; /&gt;
&lt;/xsl:call-template&gt;</code></pre>
<p xmlns="http://www.w3.org/1999/xhtml">Which should give:</p>
<p xmlns="http://www.w3.org/1999/xhtml"><code xmlns="http://www.w3.org/1999/xhtml">The URI: http://lojjic.net/blog.rdf.html --- The Path: http://lojjic.net/blog/ --- The Filename: index.rdf.html</code></p></ns2:entry-content>
</rdf:Description>

<rdf:Description rdf:about="http://lojjic.net/ns/rdf/blog/entry-content">
	<rdfs:domain rdf:resource="http://lojjic.net/ns/rdf/blog/Entry" />
</rdf:Description>

<rdf:Description rdf:about="http://lojjic.net/blog/20031127-211048">
	<ns3:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#long">1139767943</ns3:modified>
</rdf:Description>

<rdf:Description rdf:about="http://purl.org/dc/terms/modified">
	<rdfs:domain rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource" />
</rdf:Description>

<rdf:Description rdf:about="http://lojjic.net/blog/20031127-211048">
	<rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource" />
</rdf:Description>

<rdf:Description rdf:about="http://www.w3.org/1999/02/22-rdf-syntax-ns#type">
	<rdfs:domain rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource" />
</rdf:Description>

<rdf:Description rdf:about="http://lojjic.net/blog/20031127-211048">
	<rdf:type rdf:resource="http://lojjic.net/ns/rdf/comments/CommentableResource" />
	<ns4:allow-comments rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean">true</ns4:allow-comments>
</rdf:Description>

<rdf:Description rdf:about="http://lojjic.net/ns/rdf/comments/allow-comments">
	<rdfs:domain rdf:resource="http://lojjic.net/ns/rdf/comments/CommentableResource" />
</rdf:Description>

<rdf:Description rdf:about="http://lojjic.net/blog/20031127-211048">
	<ns4:comments rdf:nodeID="node14dadkou5x86" />
</rdf:Description>

<rdf:Description rdf:about="http://lojjic.net/ns/rdf/comments/comments">
	<rdfs:domain rdf:resource="http://lojjic.net/ns/rdf/comments/CommentableResource" />
</rdf:Description>

<rdf:Description rdf:nodeID="node14dadkou5x86">
	<rdf:type rdf:resource="http://lojjic.net/ns/rdf/comments/CommentList" />
	<rdf:_1 rdf:resource="http://lojjic.net/blog/20031127-211048/comment-20031124-165120" />
	<rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Resource" />
	<rdf:type rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq" />
	<rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Container" />
	<rdfs:member rdf:resource="http://lojjic.net/blog/20031127-211048/comment-20031124-165120" />
</rdf:Description>

</rdf:RDF>
