[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Empty input in analyze-string

Subject: Re: Empty input in analyze-string
From: "Mathieu Malaterre" <mathieu.malaterre@xxxxxxxxx>
Date: Fri, 4 Jul 2008 16:28:42 +0200
Re:  Empty input in analyze-string
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.