RE: [xsl] enclosing siblings in an element

Subject: RE: [xsl] enclosing siblings in an element
From: "Scott Trenda" <Scott.Trenda@xxxxxxxx>
Date: Wed, 23 Jan 2008 16:52:50 -0600
Here's what I have, based on the pattern I see (questions' classes start
with "stem_"). You'll want to remember the key pattern here for future
reference.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
  <xsl:output encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
  <xsl:key name="questions" match="part/*"
use="generate-id((preceding-sibling::par|self::par)[starts-with(@class,
'stem_')][last()])"/>
  <xsl:template match="part">
    <xsl:for-each select="par[starts-with(@class, 'stem_')]">
      <question>
        <xsl:copy-of select="key('questions', generate-id())"/>
      </question>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

Originally I had omitted the [last()] predicate from the use case,
thinking the preceding-sibling axis would give me the immediate
preceding par as the first result of the node-set, but I was wrong; it
gave the node-set in document order instead, so you need the last one.
The good news is, it works as you'd expect. This is a fairly
straightforward case of grouping, where the generate-id() in the key
links the groups to the nodes that you choose in your template later.

~ Scott

-----Original Message-----
From: Terry Ofner [mailto:tofner@xxxxxxxxxxx]
Sent: Wednesday, January 23, 2008 4:27 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] enclosing siblings in an element

I am a bit stuck on this one. After converting a Word document to rtf
and using upCast I end up with a "flat" structure such as this:
<document>
<part>
     <par class="stem_mc">&#9;1&#9;Read the sentence. Then choose the
best synonym to replace the boldfaced word.</par>
     <par class="display">The threat of rain makes it <inline
style="font-weight: bold;">dubious</inline> that the game will go on
as scheduled.</par>
     <par class="choice-a">&#9;A&#9;doubtful</par>
     <par class="choice-b">&#9;B&#9;likely</par>
     <par class="choice-c">&#9;C&#9;hopeful</par>
     <par class="choice-d">&#9;D&#9;obvious</par>
     <par class="Answer">A</par>
     <par class="g_code">G5U1S5</par>

     <par class="stem_mc">&#9;2&#9;As used in the sentence below,
what is the denotation of the word <inline style="font-style:
italic;">mutt?</inline></par>
     <par class="display">"What is that mutt doing in our yard?"
asked Dad.</par>
     <par class="choice-a">&#9;A&#9;animal</par>
     <par class="choice-b">&#9;B&#9;dog </par>
     <par class="choice-c">&#9;C&#9;creature</par>
     <par class="choice-d">&#9;D&#9;pet</par>
     <par class="Answer">B</par>
     <par class="g_code">G5U1S8</par>

     <par class="stem_sa">&#9;3&#9;Describe the connotation of the
word <inline style="font-style: italic;">mutt</inline> and explain
wheeher it is positive or negative. </par>
     <par class="Answer"><inline style="font-style: italic;"><target
id="OLE_LINK1" /><target id="OLE_LINK2" />Answers will vary. Possible
answer:</inline> The word <inline style="font-style: italic;">mutt</
inline> has the negative connotation of a dog that isn't worth very
much or that isn't good-looking.<endtarget id="OLE_LINK1" /
 ><endtarget id="OLE_LINK2" /></par>
     <par class="g_code">G5U1S8</par>
<--much more like this-->

</part>
</document>

My goal is the wrap each question in a question element:
<question>
<par class="stem_mc">&#9;1&#9;Read the sentence. Then choose the best
synonym to replace the boldfaced word.</par>
     <par class="display">The threat of rain makes it <inline
style="font-weight: bold;">dubious</inline> that the game will go on
as scheduled.</par>
     <par class="choice-a">&#9;A&#9;doubtful</par>
     <par class="choice-b">&#9;B&#9;likely</par>
     <par class="choice-c">&#9;C&#9;hopeful</par>
     <par class="choice-d">&#9;D&#9;obvious</par>
     <par class="Answer">A</par>
     <par class="g_code">G5U1S5</par>
</question>
<question>
     <par class="stem_mc">&#9;2&#9;As used in the sentence below,
what is the denotation of the word <inline style="font-style:
italic;">mutt?</inline></par>
     <par class="display">"What is that mutt doing in our yard?"
asked Dad.</par>
     <par class="choice-a">&#9;A&#9;animal</par>
     <par class="choice-b">&#9;B&#9;dog </par>
     <par class="choice-c">&#9;C&#9;creature</par>
     <par class="choice-d">&#9;D&#9;pet</par>
     <par class="Answer">B</par>
     <par class="g_code">G5U1S8</par>
</question>
<question>
<par class="stem_sa">&#9;3&#9;Describe the connotation of the word
<inline style="font-style: italic;">mutt</inline> and explain wheeher
it is positive or negative. </par>
     <par class="Answer"><inline style="font-style: italic;"><target
id="OLE_LINK1" /><target id="OLE_LINK2" />Answers will vary. Possible
answer:</inline> The word <inline style="font-style: italic;">mutt</
inline> has the negative connotation of a dog that isn't worth very
much or that isn't good-looking.<endtarget id="OLE_LINK1" /
 ><endtarget id="OLE_LINK2" /></par>
     <par class="g_code">G5U1S8</par>
</question>

Notice that each question starts with a stem attribute and is
anchored with a g_code attribute. But there can be various <par>
elements between. Is there an easy way to "select" all the <par>
elements between two <par> elements?


Terry Ofner

Current Thread