Re: [xsl] Empty input in analyze-string

Subject: Re: [xsl] Empty input in analyze-string
From: "Mathieu Malaterre" <mathieu.malaterre@xxxxxxxxx>
Date: Fri, 4 Jul 2008 16:28:42 +0200
That works ! Thanks everybody :)

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:variable name="myregex">^([CF]\.[0-9\.]+)\s*(.*)$</xsl:variable>
  <xsl:template name="get-section-reference">
    <xsl:param name="article"/>
    <xsl:param name="n"/>
    <xsl:variable name="para" select="preceding::para[$n]"/>
    <xsl:choose>
      <xsl:when test="matches($para, $myregex)">
        <xsl:analyze-string select="$para" regex="{$myregex}">
          <xsl:matching-substring>
            <match>
              <xsl:value-of select="$para"/>
            </match>
          </xsl:matching-substring>
<!-- no need to non matching-substring case -->
        </xsl:analyze-string>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="get-section-reference">
          <xsl:with-param name="article" select="."/>
          <xsl:with-param name="n" select="$n+1"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
<!--
-->
  <xsl:template match="/">
    <output>
      <xsl:for-each select="//table">
        <xsl:call-template name="get-section-reference">
          <xsl:with-param name="article" select="."/>
          <xsl:with-param name="n" select="1"/>
        </xsl:call-template>
      </xsl:for-each>
    </output>
  </xsl:template>
</xsl:stylesheet>

On Fri, Jul 4, 2008 at 4:11 PM, Mathieu Malaterre
<mathieu.malaterre@xxxxxxxxx> wrote:
> On Fri, Jul 4, 2008 at 3:42 PM, Michael Kay <mike@xxxxxxxxxxxx> wrote:
>>>
>>> if para2 is an empty element, then the regex success and
>>> return an empty string. How do I express in my regex that
>>> empty is a non-matching regex ?
>>>
>>
>> Further to Joe's response: analyze-string partitions the input string into a
>> sequence of substrings, passing substrings that match the regex to
>> xsl:matching-substring and those that don't match to
>> xsl:non-matching-substring. If the input is empty, there will be no
>> substrings, therefore no calls on either xsl:matching-substring or
>> xsl:non-matching-substring.
>>
>> If you want to test whether the string as a whole matches the regex, use the
>> matches() function.
>
> Ok I think I am getting closer now. My goal is simply to recursively
> iterate over preceeding <para> element in search for a matching regex.
> I must have made a mistake with the recusion calls...
>
> <article>
> <para>C.1. section</para>
> <para>C.1.1 section</para>
> <para>bla</para>
> <para>C.1.1.1 section</para> <!-- correct solution -->
> <para>foo</para>
> <para/>
> <para>bar</para>
> <table>mytable</table>
> </article>
>
> <?xml version="1.0"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">
>  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
>  <xsl:variable name="myregex">^([CF]\.[0-9\.]+)\s*(.*)$</xsl:variable>
>  <xsl:template name="get-section-reference">
>    <xsl:param name="article"/>
>    <xsl:param name="n"/>
>    <xsl:variable name="para" select="article/preceding::para[$n]"/>
>    <xsl:choose>
>      <xsl:when test="matches($para, $myregex)">
>        <xsl:analyze-string select="." regex="{$myregex}">
>          <xsl:matching-substring>
>            <match>
>              <xsl:value-of select="."/>
>            </match>
>          </xsl:matching-substring>
> <!-- no need to non matching-substring case -->
>        </xsl:analyze-string>
>      </xsl:when>
>      <xsl:otherwise>
>        <xsl:call-template name="get-section-reference">
>          <xsl:with-param name="article" select="."/>
>          <xsl:with-param name="n" select="$n+1"/>
>        </xsl:call-template>
>      </xsl:otherwise>
>    </xsl:choose>
>  </xsl:template>
> <!--
> -->
>  <xsl:template match="/">
>    <output>
>      <xsl:for-each select="//table">
>        <xsl:call-template name="get-section-reference">
>          <xsl:with-param name="article" select="."/>
>          <xsl:with-param name="n" select="1"/>
>        </xsl:call-template>
>      </xsl:for-each>
>    </output>
>  </xsl:template>
> </xsl:stylesheet>
>
>
> --
> Mathieu
>



-- 
Mathieu

Current Thread