[Home] [By Thread] [By Date] [Recent Entries]
I am attempting to parse test items using unparsed-text and analyze-
string as per Michael Kay's stylesheet "analyze-names.xsl" in chapter
7 of XSLT 2.0. I am trying to add elements to different parts of test
items. A section of my unparsed text input looks like this (not
actual items!):
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 Note that the item numbers and letters ABCD are surrounded by tabs and the the G_code in parentheses is preceded by a tab. So far I have been able to surround the answer choices with <choice- x> elements by using this edited version of Michael's analyze-names sheet: <?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="\t([A-D])\t(.*) ">
<xsl:matching-substring>
<xsl:variable name="choiceLetter" select="regex-group(1)"/>
<xsl:variable name="choiceLower" select="lower-case($choiceLetter)"/>
<xsl:element name="choice-{$choiceLower}">
<xsl:value-of select="regex-group(2)"/>
</xsl:element><xsl:text> </xsl:text>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template></xsl:stylesheet> Output of the following sheet looks like this:
My problem occurs when I get greedy and add the <notes> element around the G_code in parenthesis like this: <xsl:analyze-string select="$in" regex="\((G[3-8]U[1-7]S\d\d?)\) \t([A-D])\t(.*) "> <xsl:matching-substring> <xsl:element name="notes"> <xsl:value-of select="regex-group(1)"/> </xsl:element><xsl:text> </xsl:text> <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> </xsl:text> </xsl:matching-substring> . . . That regular expression produces the following output: 1 XSLT means-- <notes>G6U1S01</notes> <choice-a>extensible stylesheet language transformations.</choice-a> B extremely sly lexical transformations. C XML stylesheet language transformations. D all the above Which makes sense, since the regex is anchored by the ((G[3-8]U[1-7]S \d\d?)\) expression. My question: how do I design a single sheet that will iterate through various regex expressions. 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? My goal is to use XSLT in the place of a manual markup process if at all possible. Any hints would be most appreciated. Terry P.S. I tried adding another <xsl:analyze-string select="$in" template. That approach put two copies of the input document into the output, which, again makes sense. My regular expressions worked at least!
|

Cart



