|
Subject: Re: [xsl] decoding percent-escaped octet sequences From: Julian Reschke <julian.reschke@xxxxxx> Date: Thu, 26 May 2011 10:56:02 +0200 |
Hi,
do XSLT/XPath2 offer an elegant way to convert percent-escaped octet sequences to strings (both for ISO-8859-1 and UTF-8).
So far I found codepoints-to-string, but that would mean that I'd still have to to
1) percent-escaped-string to sequence of octets, and 2) sequence-of UTF-8 octets to seequence of codepoints.
Did I miss something here?
Best regards, Julian ...
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:myns="mailto:julian.reschke@xxxxxxxxxxxxx?subject=pctdecode" exclude-result-prefixes="myns" >
<xsl:template match="/">
<results>
<xsl:copy-of select="myns:test('utf-8','A%20C')"/>
<xsl:copy-of select="myns:test('iso-8859-1','A%20C')"/>
<xsl:copy-of select="myns:test('utf-8','A%C3%A4')"/>
<xsl:copy-of select="myns:test('iso-8859-1','A%E4')"/>
<xsl:copy-of select="myns:test('utf-8','A%E4')"/>
</results>
</xsl:template><xsl:function name="myns:test"> <xsl:param name="enc"/> <xsl:param name="value"/>
<result>
<input>
<enc><xsl:value-of select="$enc"/></enc>
<value><xsl:value-of select="$value"/></value>
</input>
<parsed>
<xsl:variable name="raw" select="myns:pct-decode($value)"/> <xsl:choose>
<xsl:when test="lower-case($enc)='iso-8859-1'">
<xsl:copy-of select="myns:decode-iso-8859-1($raw)"/>
</xsl:when>
<xsl:when test="lower-case($enc)='utf-8'">
<xsl:copy-of select="myns:decode-utf-8($raw)"/>
</xsl:when>
<xsl:otherwise>
<!-- unsupported encoding -->
</xsl:otherwise>
</xsl:choose>
</parsed>
</result>
</xsl:function><xsl:function name="myns:pct-decode"> <xsl:param name="s"/>
<xsl:function name="myns:decode-iso-8859-1"> <xsl:param name="s"/>
<xsl:variable name="result">
<xsl:for-each select="$s">
<xsl:choose>
<xsl:when test="self::octet">
<xsl:choose>
<xsl:when test=". > 127 and . < 160">
<illegal-octet><xsl:value-of select="."/></illegal-octet>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="codepoints-to-string(.)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable><xsl:function name="myns:decode-utf-8"> <xsl:param name="s"/>
<xsl:variable name="result">
<xsl:call-template name="internal-utf8">
<xsl:with-param name="octets" select="$octets/*"/>
</xsl:call-template>
</xsl:variable><xsl:template name="internal-utf8"> <xsl:param name="octets"/>
<?xml version="1.0" encoding="UTF-8"?> <results xmlns:xs="http://www.w3.org/2001/XMLSchema"> <result> <input> <enc>utf-8</enc> <value>A%20C</value> </input> <parsed> <string>A C</string> </parsed> </result> <result> <input> <enc>iso-8859-1</enc> <value>A%20C</value> </input> <parsed> <string>A C</string> </parsed> </result> <result> <input> <enc>utf-8</enc> <value>A%C3%A4</value> </input> <parsed> <string>Ad</string> </parsed> </result> <result> <input> <enc>iso-8859-1</enc> <value>A%E4</value> </input> <parsed> <string>Ad</string> </parsed> </result> <result> <input> <enc>utf-8</enc> <value>A%E4</value> </input> <parsed> <illegal-octet>228</illegal-octet> </parsed> </result> </results>
| Current Thread |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| Re: [xsl] decoding percent-escaped , Julian Reschke | Thread | Fw: [xsl] decoding percent-escaped , Hermann Stamm-Wilbra |
| Re: [xsl] need xsl template for thi, Matthieu Ricaud-Duss | Date | [xsl] Multiple/conditional import p, Martynas Jusevicius |
| Month |