xsl-list
[Top] [All Lists]

[xsl] analyze-string regex

2014-03-26 15:30:28
Hi,

I am trying to use xsl:analyze-string to process a json response, the model of 
which is fixed.

The target environment is saxon-ce, but I am using oxygen to get things working.

I was under the impression that the regex would perform multiple matches, 
allowing me to generate a sequence of elements from a string containing 
multiple objects that I match.  If that is true (the spec says it should be, I 
think), then my regex is faulty.  Could you offer any suggestions about the 
following, please?

When I use dot-all mode (@flags="s"), I get a single <option> corresponding to 
the last object in the array.  When no flags, an empty sequence.  Any 
suggestions appreciated.

My objective is to generate a sequence of <option> elements matching the json 
objects:

xslt:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; 
xmlns:ex="http://example.org/namespace"; exclude-result-prefixes="ex">
  <xsl:output method="xml" omit-xml-declaration="yes"/>
  <xsl:template match="json">
    <datalist>
      <xsl:copy-of select="ex:json2Options(.)"/>
    </datalist>
  </xsl:template>
  <xsl:function name="ex:json2Options">
    <xsl:param name="json"/><!--           1       2                   3        
     4                              5                       6               7   
                     8            9              -->
    <xsl:variable name="regexps" 
select="'(\{.*(&quot;title&quot;:.*&quot;(.*?)&quot;).*(&quot;qualifier&quot;:.*&quot;(.*?)&quot;).*(&quot;type&quot;:.*&quot;(.*?)&quot;).*(&quot;geometry&quot;:.*(\{.*?\})).*\}{1,})'"/>
    <xsl:analyze-string select="$json" regex="{$regexps}">
      <xsl:matching-substring>
        <xsl:element name="option">
          <xsl:attribute name="data-title" 
select="regex-group(3)"></xsl:attribute>
          <xsl:attribute name="data-qualifier" select="regex-group(5)"/>
          <xsl:attribute name="data-type" select="regex-group(7)"/>
          <xsl:attribute name="data-geometry" select="regex-group(9)"/>
          <xsl:value-of select="regex-group(3)"/>
        </xsl:element>
      </xsl:matching-substring>
    </xsl:analyze-string>
  </xsl:function>
</xsl:stylesheet>

Input document:

<json>
[
{
    "title": "Ottawa, Carleton, Ontario (Unincorporated area)", 
    "qualifier": "LOCATION",
    "type": "Geoname",
    "bbox": [-75.6903367156744, 45.373570070138, -75.6524412843256, 
45.427540929862],
    "geometry": {"type":"Point","coordinates":[-75.671387,45.400555]}
},
{
    "title": "Ottawa, Carleton; Russell, Ontario (City)", 
    "qualifier": "LOCATION",
    "type": "Geoname",
    "bbox": [-76.3555857, 44.9617738, -75.2465783, 45.5376514],
    "geometry": {"type":"Point","coordinates":[-75.58429,45.333389]}
},
{
    "title": "031G OTTAWA", 
    "qualifier": "LOCATION",
    "type": "NTS",
    "bbox": [-76.0, 45.0, -74.0, 46.0],
    "geometry": {"type":"Point","coordinates":[-75,45.5]}
}]</json>


Cheers, and thanks,
Peter Rushforth




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


<Prev in Thread] Current Thread [Next in Thread>