Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting a range of paragraphs in WordML

Status
Not open for further replies.

LuckyKoen

Programmer
Feb 14, 2005
57
BE
I have a WordML document of which I would like to select a certain heading. WordML however only consists of paragraphs that follow eachother. A paragraph (<w:p>) consists of paragraph properties (<w:pPr>) and text (<w:t>). A basic WordML document would look like this :

<w:p>
<w:pPr>
<w:pStyle w:val="Heading1"/>
</w:pPr>
<w:r>
<w:t>
The first heading
</w:t>
</w:r>
</w:p>

<w:p>
<w:r>
<w:t>
The first paragraph after heading 1...
</w:t>
</w:r>
</w:p>

<w:p>
<w:r>
<w:t>
The 2nd paragraph after heading 1...
</w:t>
</w:r>
</w:p>

<w:p>
<w:pPr>
<w:pStyle w:val="Heading1"/>
</w:pPr>
<w:r>
<w:t>
The 2nd heading
</w:t>
</w:r>
</w:p>

<w:p>
<w:r>
<w:t>
The first paragraph after heading 2...
</w:t>
</w:r>
</w:p>

<w:p>
<w:r>
<w:t>
The 2nd paragraph after heading 2...
</w:t>
</w:r>
</w:p>

What I need is an xslt that selects a heading and all the paragraphs that follow it until the next heading. So for heading 1 this xslt would show :

The first heading
The first paragraph after heading 1...
The 2nd paragraph after heading 1...

I'm a bit clueless on how to handle this. As far as I know there is nothing similar to 'until' or a boolean that can be set to true or false in xsl. How should I handle this problem ?

 
What's the problem? It's in that order anyway. An empty stylesheet will give that output.

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
The problem is that I only want some of the paragraphs. Let's say that a wordML document contains 50 paragraphs. Paragraph 1, 10, 20, 30 and 40 are headings that contain the </w:pPr> element and all the other paragraphs only contain text. An empty stylesheet would give me all 50 paragraphs, I need an xsl stylesheet that would give me one of the heading paragraphs and all the paragraphs that follow it until the next heading paragraph. For example : if I wanted the first heading I should get paragraphs 1 through 9.
 
Ahh, I see. You'll have to count the w:p nodes that precede the heading you're after and the following one (or the end of the doc if its the last heading) and take the nodes in between:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] xmlns:w="D">
  <xsl:output omit-xml-declaration="yes"/>
  <xsl:param name="paragraph" select="2"/>
  <xsl:template match="/*">
    <xsl:variable name="start" select="count(w:p[w:pPr][$paragraph]/preceding-sibling::*)"/>
    <xsl:variable name="end">
      <xsl:choose>
        <xsl:when test="$paragraph = count(w:p[w:pPr])">
          <xsl:value-of select="count(w:p)"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="count(w:p[w:pPr][$paragraph+1]/preceding-sibling::*)"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:for-each select="w:p[position() &gt; $start and position() &lt;= $end]">
      <xsl:value-of select="w:r/w:t"/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
(This presumes your structure is /root/w:p)

Jon

"There are 10 types of people in the world... those who understand binary and those who don't.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top