xsl-list
[Top] [All Lists]

Re: [xsl] unparsed-text and analyze-string

2008-01-21 15:29:29
At 2008-01-21 17:03 -0500, Terry Ofner wrote:
My question: how do I design a single sheet that will iterate through
various regex expressions.

You don't need to.

I would like to avoid a situation where I
am piping the output of one stylesheet through another if at all
possible. Also, is it possible to use unparsed-text with text that
already contains some elements?

No, because the element structure is not preserved.

My goal is to use XSLT in the place of a manual markup process if at
all possible.

A good goal to have!

Any hints would be most appreciated.

You didn't notice that there is an "alternatives" operator "|" available for regular expressions. "a|b" matches either the pattern "a" or the pattern "b".

I hope the working example below helps.

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


t:\ftemp>type terry.txt
        1       XSLT means--    (G6U1S01)
        A       extensible stylesheet language transformations.
        B       extremely sly lexical transformations.
        C       XML stylesheet language transformations.
        D       all the above

        2       Stem
                display sentence (optional)
                stem part 2 (optional)  (G6U1S01)
        A       answer choice
        B       answer choice
        C       answer choice
        D       answer choice

t:\ftemp>type terry.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    version="2.0">

<xsl:param name="input-uri"/>
<xsl:output indent="yes"/>

<xsl:template name="main">
  <xsl:variable name="in"
              select="unparsed-text($input-uri, 'UTF-8')"/>

<xsl:analyze-string select="$in"
  regex="\((G[3-8]U[1-7]S\d\d?)\)&#10;|\t([A-D])\t(.*)&#10;">
  <xsl:matching-substring>
    <xsl:choose>
      <xsl:when test="matches(.,'\(G')">
        <xsl:element name="notes">
          <xsl:value-of select="regex-group(1)"/>
        </xsl:element><xsl:text>&#10;</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="choiceLetter" select="regex-group(2)"/>
        <xsl:variable name="choiceLower" select="lower-case($choiceLetter)"/>
        <xsl:element name="choice-{$choiceLower}">
          <xsl:value-of select="regex-group(3)"/>
        </xsl:element><xsl:text>&#10;</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:matching-substring>
  <xsl:non-matching-substring>
     <xsl:value-of select="."/>
  </xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>saxon9 -it:main terry.xsl input-uri=terry.txt

t:\ftemp>java -Xms200m -Xmx700m -jar p:\xml\xslt\saxon9\saxon9.jar -it:main terry.xsl input-uri=terry.txt <?xml version="1.0" encoding="UTF-8"?> 1 XSLT means-- <notes>G6U1S01</notes>
<choice-a>extensible stylesheet language transformations.</choice-a>
<choice-b>extremely sly lexical transformations.</choice-b>
<choice-c>XML stylesheet language transformations.</choice-c>
<choice-d>all the above</choice-d>

        2       Stem
                display sentence (optional)
                stem part 2 (optional)  <notes>G6U1S01</notes>
<choice-a>answer choice</choice-a>
<choice-b>answer choice</choice-b>
<choice-c>answer choice</choice-c>
<choice-d>answer choice</choice-d>

t:\ftemp>

--
World-wide corporate, govt. & user group XML, XSL and UBL training
RSS feeds:     publicly-available developer resources and training
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--