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!

Skipping blocks in XML document (XSL)

Status
Not open for further replies.

Milby7

Programmer
Dec 5, 2003
67
GB
does anyone know how to skip part of an xml document when a particular tag is met? i've got an xml (WordML) document with if statements surrounded by <condition> tags as follows:

Code:
[teal]<!-- some document tags/text etc. -->[/teal]

<sdp:condition eval="MYNAME = 'BOB'">&lt;&lt;IF MYNAME = 'BOB'&gt;&gt;</sdp:condition>

[teal]<!-- some document tags/text etc. -->[/teal]

<sdp:condition eval="ELSE">&lt;&lt;ELSE&gt;&gt;&lt;&lt;IF MYNAME = 'BOB'&gt;&gt;</sdp:condition>

[teal]<!-- some document tags/text etc. -->[/teal]

<sdp:condition eval="END">&lt;&lt;ENDIF&gt;&gt;&lt;&lt;IF MYNAME = 'BOB'&gt;&gt;</sdp:condition>

[teal]<!-- some document tags/text etc. -->[/teal]

the purpose of the conditions is to be able to produce documents with different content depending on certain factors. in the example above, one block of text should be in the document if "MYNAME" (dataname/variable) is equal to "BOB", and another load of text if not.

i've also got an xml file (merge-data.xml) that actually holds the results of if statements (among other things). below is a short sample:

Code:
<docpro-print-entry>
  <dataset>
    <condition ref="&lt;&lt;IF MYNAME = 'BOB'&gt;&gt;">TRUE</condition>
    [teal]<!-- bla bla bla -->[/teal]
  </dataset>
</docpro-print-entry>

so when processing the document i need to refer to the data in the xml file above.

i've written a match template, but the condition tags (blocks) aren't being processed properly. i'm quite new to this stuff & am pretty stuck. i'm getting text from both the if & else blocks, plus the else block of text is being repeated 4 times, then the end of the if (tag) is hit 4 times!? the template is being applied whenever the tag exists within a <w:t> block using a simple <xsl:apply-templates/>. if anyone has any ideas i'd really appreciate the help!!

Code:
<xsl:variable name="mergeData" select="document('merge-data.xml')"/>

<xsl:template match="sdp:condition">
  <xsl:variable name="curNode" select="."/>

  <xsl:choose>
    <xsl:when test="$curNode/@eval = 'ELSE'">
      [teal]<!-- figure out which if statement this else belongs to, then determine whether to output, or skip the else block -->[/teal]
      <xsl:variable name="prevIfNode" select="$mergeData//docpro-print-entry/dataset/condition[substring-after($curNode, '&lt;&lt;ELSE&gt;&gt;')]"/>

      <xsl:choose>
        [teal]<!-- previous if statement evaluated to TRUE, so skip else block -->[/teal]
        <xsl:when test="$prevIfNode = 'TRUE'">
          <xsl:variable name="endNodeText" select="concat('&lt;&lt;ENDIF&gt;&gt;', $prevIfNode/@ref)"/>
          <xsl:apply-templates select="following::node()[$endNodeText]"/>
        </xsl:when>
        [teal]<!-- previous if statement evaluated to FALSE, so output else block -->[/teal]
        <xsl:otherwise>
          [teal]<!-- don't output anything, & continue -->[/teal]
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:when test="$curNode/@eval = 'END'">
      [teal]<!-- don't output anything, & continue -->[/teal]
    </xsl:when>
    <xsl:otherwise>
      <xsl:variable name="curIfNode" select="$mergeData//docpro-print-entry/dataset/condition[@ref = $curNode]"/>

      <xsl:choose>
        [teal]<!-- condition evaluates to true -->[/teal]
        <xsl:when test="$curIfNode = 'TRUE'">
	</xsl:when>
        [teal]<!-- condition evaluates to false -->[/teal]
        <xsl:otherwise>
          <xsl:variable name="elseNodeText" select="concat('&lt;&lt;ELSE&gt;&gt;', $curNode/@ref)"/>
          <xsl:apply-templates select="following::node()[$elseNodeText]"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top