xsl-list
[Top] [All Lists]

Re: [xsl] Need help with function to split string and return concatenated string

2010-05-06 18:01:40
The following is sort of a brute-force approach. I bet others on this
list could do a nicer job. And of course, no error checking, so
leading space, etc., just kills it.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema"; exclude-result-prefixes="xs"
  xmlns:xmp="http://www.example.org/ns/17";
  version="2.0">
  <xsl:function name="xmp:coord" as="xs:string">
    <xsl:param name="value" as="xs:string"/>
    <xsl:analyze-string select="$value" regex="^([0-9]+)\.([0-9]+)\.([0-9]+) 
([NSEW])$">
      <xsl:matching-substring>
        <xsl:value-of
          select="concat( regex-group(1), '&#xB0; ', regex-group(2), '&#x2032; 
', regex-group(3), '&#x2033; ', regex-group(4) )"
        />
      </xsl:matching-substring>
      <xsl:non-matching-substring>
        <xsl:text>ERROR</xsl:text>
      </xsl:non-matching-substring>
    </xsl:analyze-string>
  </xsl:function>
  <xsl:template match="/">
    <Wrapper>
      <xsl:apply-templates select="//location"/>
    </Wrapper>
  </xsl:template>
  <xsl:template match="location">
    <Data>
      <xsl:value-of select="xmp:coord(@latitude)"/>
    </Data>
    <Data>
      <xsl:value-of select="xmp:coord(@longitude)"/>
    </Data>
  </xsl:template>
</xsl:stylesheet>

I'm still knocking the rust off my brain and I've go to the point
where I wish to split the value of an attribute and concatenate the
pieces to return a second string.

I have attributes in my input document which represent latitude and
longitude. They are presented this way:

34.56.12 N

Where the digits to the left of the first period represent degrees,
the digits between the two periods represent minutes, and the
digits between the second period and the space represent seconds.

The output I'm looking for given the input above is

34deg 56min 12sec N

While I used to be a wiz with regular expression when I regularly
programmed in perl, I have lost it. I would appreciate some
guidance.

My idea is to create a function that would perform this string
manipulation so that I could use something like this in my
stylesheet:

<Data><xsl:value-of select="coord(@latitude)"/></Data>
<Data><xsl:value-of select="coord(@longitude)"/></Data>

My input would look like:

<location latitude="34.56.12 N" longitude="125.6.15 E"/>

Thanks.

--~------------------------------------------------------------------
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>
--~--