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

Re: child nodes and regex

Subject: Re: child nodes and regex
From: Mike Sokolov <sokolov@xxxxxxxxxxxx>
Date: Thu, 18 Jun 2009 17:12:57 -0400
Re:  child nodes and regex
Maybe acting on the first text node, and then subsequent nodes will work:

<xsl:template match="choice">
<xsl:analyze-string select="./text()[1]" regex="^ ?([A-J]) ?(.*)">
...
<xsl:element name="choice-{$choiceLetter}">
<xsl:value-of select="regex-group(2)"/>
<xsl:apply-templates select="./text()[1]/following-sibling::node()" />
</xsl:element>
... </xsl:template>


?





Terry Ofner wrote:
I am having trouble outputting child nodes as part of regex
matching-substring.

XML input contains the following:

<question>
<text><p> 32 What is the most effective way to revise sentence 1?</p></text>
<choice> F insert a comma after <i>American</i></choice>
<choice> G use dashes . . . was Samuel Clemens</choice>
<choice> H insert a period after Clemens</choice>
<choice> J use quotation marks. . . was Samuel Clemens</choice>
</question>

I am attempting to use the capital letter following each <choice> opening
element to create the following elements:

<choice-f>insert a comma after <i>American</i></choice-f>
<choice-g>use dashes to set off the clause whose real name was Samuel
Clemens</choice-g>
etc.

Here is the relevant template from the 2.0 stylesheet:

<xsl:template match="choice">
   <xsl:analyze-string select="." regex="^ ?([A-J]) ?(.*)">
     <xsl:matching-substring>
        <xsl:variable name="choiceLetter">
          <xsl:value-of select="lower-case(regex-group(1))"/></xsl:variable>
            <xsl:element name="choice-{$choiceLetter}">
               <xsl:value-of select="regex-group(2)"/>
            </xsl:element>
      </xsl:matching-substring>
    </xsl:analyze-string>
</xsl:template>

This works up to a point. The only problem is that it strips out the <i>
elements, leaving the text in place. Notice in this output that <i> elements
have been removed around American:

<question><text><p> 32 What is the most effective way to revise sentence
1?</p></text>
   <choices>
       <choice-f>insert a comma after American</choice-f>
       <choice-g>use dashes . . . was Samuel Clemens</choice-g>
       <choice-h>insert a period after Clemens</choice-h>
       <choice-j>use quotation . . . was Samuel Clemens</choice-j>
   </choices>
       . . .
</question>

I seem to recall matching choice/text() in prior cases. But I have not
been able to get that to work properly in this current situation.

Any suggestions would be most appreciated.

Terry Ofner

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.