|
Subject: Re: [xsl] parsing parens in the park From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> Date: Sun, 28 Sep 2008 17:44:28 -0400 |
I think I may have solved it below ... please let me know if there are edge cases I didn't think of.
<xsl:template match="tests">
<results>
<xsl:apply-templates select="test"/>
</results>
</xsl:template><xsl:template match="test">
<!--
Debug: <xsl:value-of select="."/>
<xsl:text>
</xsl:text>
-->
<result>
<!--use a called template because of the need to generate structure-->
<xsl:call-template name="c:values"/>
</result>
</xsl:template><!--process a name/value specification--> <xsl:template name="c:name-value-pair"> <xsl:param name="name"/> <xsl:param name="value"/>
<xsl:attribute name="{$name}" select="$value"/>
</xsl:template><!--find the first value specification in a string of value specifications-->
<xsl:template name="c:values">
<xsl:param name="rest" select="."/>
<xsl:if test="$rest">
<!--walk through the string-->
<xsl:analyze-string select="$rest" regex="\s*(\i\c*)\s*\((.*?)([()])(.*)">
<xsl:matching-substring>
<xsl:choose>
<xsl:when test="regex-group(3)='('">
<!--at the start of a nested parenthesized value, so find it-->
<xsl:variable name="this-rest"
select="c:nested-value('',regex-group(4),1)"/>
<!--process the name/value pair-->
<xsl:call-template name="c:name-value-pair">
<xsl:with-param name="name" select="regex-group(1)"/>
<xsl:with-param name="value" select="concat(regex-group(2),'(',
$this-rest[1],')')"/>
</xsl:call-template>
<!--find the next value specification-->
<xsl:call-template name="c:values">
<xsl:with-param name="rest" select="$this-rest[2]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!--process the name/value pair-->
<xsl:call-template name="c:name-value-pair">
<xsl:with-param name="name" select="regex-group(1)"/>
<xsl:with-param name="value" select="regex-group(2)"/>
</xsl:call-template>
<!--find the next value specification-->
<xsl:call-template name="c:values">
<xsl:with-param name="rest" select="regex-group(4)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:message>
<xsl:text>Whoops! What happened here? Not expected: </xsl:text>
<xsl:value-of select="."/>
</xsl:message>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:if>
</xsl:template><!--recursively determine a parenthesized value assuming balanced parens-->
<xsl:function name="c:nested-value">
<!--returning a sequence of two values: the value and the rest-->
<xsl:param name="this"/>
<xsl:param name="rest"/>
<xsl:param name="depth"/>
<xsl:if test="$rest">
<!--there is still more to do-->
<xsl:analyze-string select="$rest" regex="(.*?)([()])(.*)">
<xsl:matching-substring>
<xsl:choose>
<xsl:when test="regex-group(2)='('">
<!--yet another nested value-->
<xsl:sequence select="c:nested-value( concat($this,regex-group(1),
'('),
regex-group(3),
$depth + 1)"/>
</xsl:when>
<xsl:when test="$depth=1">
<!--found the last balanced parenthesis-->
<xsl:sequence select="concat($this,regex-group(1)),
regex-group(3)"/>
</xsl:when>
<xsl:otherwise>
<!--found a nested balanced parenthesis-->
<xsl:sequence select="c:nested-value(concat($this,regex-group(1),
')'),
regex-group(3),
$depth - 1)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:message>
<xsl:text>Nested whops! Not expected:</xsl:text>
<xsl:value-of select="."/>
</xsl:message>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:if>
</xsl:function></xsl:stylesheet> t:\ftemp>rem Done!
-- 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 |
|---|
|
| <- Previous | Index | Next -> |
|---|---|---|
| Re: [xsl] parsing parens in the par, Liam Quin | Thread | [xsl] Counting indent level, Nicholas Orr |
| Re: [xsl] parsing parens in the par, Dimitre Novatchev | Date | Re: [xsl] parsing parens in the par, Liam Quin |
| Month |