Re: [xsl] parsing parens in the park

Subject: Re: [xsl] parsing parens in the park
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Sun, 28 Sep 2008 09:57:40 -0400
At 2008-09-28 12:29 -0400, Syd Bauman wrote:
In an XSLT 2.0 stylesheet I have a string (call it $park) that is of
the format
       name1 (value1) name2 (value2) name3 (value3) ...
where the whitespace is optional, but the parentheses are not. Every
name is guaranteed to be an unqualified XML Name. Every name is
guaranteed to have an associated value, which could be a string of
most anything, including the null string or one that contains
*matched pairs of parentheses*. (A previous validation stage has, in
theory, guaranteed that there are no unmatched single parentheses in
a value.)

I need to produce a copy of this string with certain adjustments,
e.g., dropping any name-value pair where the name is a particular
string, or selecting only name-value pairs for which the value
matches a given regexp (which may include the local name of the
context node).
...
So it seems like a good first step would be to parse the string into
what Perl calls a hash table, or lisp calls an alist. I think that
means what I wish I could do is generate a key ala the following
(presuming that my get functions return a sequence).
  <xsl:key name="park-alist"
           match="syd:get-values( $park )"
           use="syd:get-names( $park )"/>
If I understand correctly, this isn't possible because you can't call
a function from the match= attribute, as it is a pattern, not an
expression.

True, but a key table always contains tree nodes and is indexed by values ... you cannot load up a key table with strings.


b) there's gotta be a better way.

So, any thoughts on how to parse that string, or a better method of
approaching the whole problem, are appreciated.

I would have used analyze-string, as below, except that I haven't got the time right now to figure out how to check for and preserve a value that contains nested parentheses. So the answer below is incomplete.


I hope this helps.

. . . . . . . . . . . Ken

t:\ftemp>type syd.xml
<?xml version="1.0" encoding="US-ASCII"?>
<tests>
  <test>name1 (value1) name2 (value2) name3 (value3)</test>
  <test>name1 (value1)name2 () name3(value3)</test>
  <test>name1(value1)name2()name3()</test>
</tests>

t:\ftemp>type syd.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:xsd="http://www.w3.org/2001/XMLSchema";
                exclude-result-prefixes="xsd"
                version="2.0">

<xsl:output method="text"/>

<xsl:template match="test">
  With: <xsl:value-of select="."/>
  <xsl:text>
</xsl:text>
  <xsl:analyze-string select="." regex="\s*(\i\c*)\s*\((.*?)\)\s*">
    <xsl:matching-substring>
      <xsl:choose>
        <xsl:when test="regex-group(1)='name-check'">
          <!--do something-->
        </xsl:when>
        <xsl:when test="regex-group(2)='value-check'">
          <!--do something-->
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="'[Got',regex-group(1),
                                'with',regex-group(2),']'"/>
          <xsl:text>
</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:matching-substring>
    <xsl:non-matching-substring>
      <xsl:message>
        <xsl:text>Whoops!  What happened here: </xsl:text>
        <xsl:value-of select="."/>
      </xsl:message>
    </xsl:non-matching-substring>
  </xsl:analyze-string>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>xslt2 syd.xml syd.xsl con


With: name1 (value1) name2 (value2) name3 (value3) [Got name1 with value1 ] [Got name2 with value2 ] [Got name3 with value3 ]


With: name1 (value1)name2 () name3(value3) [Got name1 with value1 ] [Got name2 with ] [Got name3 with value3 ]


With: name1(value1)name2()name3() [Got name1 with value1 ] [Got name2 with ] [Got name3 with ]


t:\ftemp>





-- Upcoming XSLT/XSL-FO hands-on courses: Wellington, NZ 2009-01 Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Male Cancer Awareness Nov'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread