[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] analyze-string regex


Subject: Re: [xsl] analyze-string regex
From: Liam R E Quin <liam@xxxxxx>
Date: Thu, 27 Mar 2014 15:19:36 -0400

On Thu, 2014-03-27 at 17:06 +0000, Rushforth, Peter wrote:
[...]

> What I came up seems to work ok:
> 
>   <xsl:function name="ex:locationJson2Options">
>     <xsl:param name="json"/><!--           1    2                            3             4                              5                6                         7              8 9 10 11                  12         13                       14                     15 16                      17                                     -->
>     <xsl:variable name="regexps" select="'(\{.*?(&quot;title&quot;:.*?&quot;(.*?)&quot;).*?(&quot;qualifier&quot;:.*?&quot;(.*?)&quot;).*?(&quot;type&quot;:.*?&quot;(.*?)&quot;).*?((((&quot;bbox&quot;:.*?\[(.*?)\]).*?(&quot;geometry&quot;:.*?(\{.*?\})).*?\}{1,}))|((&quot;geometry&quot;:.*?(\{.*?\})).*?\}{1,})))'"/>
>     <xsl:analyze-string select="$json" regex="{$regexps}" flags="s">

[...]

> I am glad it didn't lead to madness, [...]

It's subject to debate :-)
For example,
  "title" : "The \"liteness\" of Bering: a sales qualifier"
will probably fail.; that may or may not matter for you.

One way to deal with \ would be to replace \" with some unicode string
that you first checked didn't appear, and then your expression (modulo
putting \s in there instead of .* in places) would be simpler.

I'd also note you use &quot; a lot, so change them to " and use '....'
and &apos; instead. You can also build up a complex expression by making
smaller variables (with comments) and using concat() at the end.

Liam

-- 
Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/
Ankh: irc.sorcery.net irc.gnome.org freenode/#xml


Current Thread
Keywords