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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting the context node

Status
Not open for further replies.

Milby7

Programmer
Dec 5, 2003
67
GB

does anyone know how to set the context node when processing an xml document with xsl? i.e. how do you move the context node to another node further up/down the document?

i thought i could do it by using <apply-templates select="another node"/>, but i still get stuff outputted from nodes between the node where the apply-templates is called and the one selected!?

thanks!!

 
Try using xsl:match-template.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
i've written a load of code below. if you run it, then you'll see that none of the blocks of document nodes are missed which is what i'm trying to do.

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!-- source document to be transformed -->

<doc>
 <page>
  <content>
   <para>
    <run>
     <text>
      <condition eval="THIS = 'TEST'">&lt;&lt;IF THIS = 'TEST'&gt;&gt;</condition>
     </text>
    </run>
   </para>
   <para>
    <run>
     <text>IF EVALUATED TO TRUE!!!</text>
    </run>
   </para>
   <para>
    <run>
     <text>
      <condition eval="ELSE">&lt;&lt;ELSE&gt;&gt;&lt;&lt;IF THIS = 'TEST'&gt;&gt;</condition>
     </text>
    </run>
   </para>
   <para>
    <run>
     <text>IF EVALUATED TO FALSE!!!</text>
    </run>
   </para>
   <para>
    <run>
     <text>
      <condition eval="END">&lt;&lt;ENDIF&gt;&gt;&lt;&lt;IF THIS = 'TEST'&gt;&gt;</condition>
     </text>
    </run>
   </para>
  </content>
 </page>
</doc>

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!-- source document's condition information (merge-data.xml) -->

<dataset>
 <condition ref="&lt;&lt;IF WEATHER = 'BAD'&gt;&gt;">TRUE</condition>
 <condition ref="&lt;&lt;IF THIS = 'TEST'&gt;&gt;">TRUE</condition>
 <condition ref="&lt;&lt;IF THISDATA = 'RANDOM'&gt;&gt;">TRUE</condition>
</dataset>

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!-- xslt stylesheet -->

<xsl:stylesheet version="1.0"
 xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL]
 xmlns:sdp="[URL unfurl="true"]http://www.sectornet.co.uk/2004/DocPro">[/URL]

 <xsl:output method="html"/>
 
 <xsl:variable name="mergeData" select="document('merge-data.xml')//dataset"/>
 
 <!-- ++++++++++++++++++++++++++++++++++ -->
 <!-- ++++++++++++++++++++++++++++++++++ -->
 <xsl:template match="/">
  <html>
   <head>
    <title>condition test output.</title>
   </head>
   <body>
    <center>
     <h1>Condition Test.</h1>
    </center>
    
    [b]<xsl:apply-templates select="doc/page/content"/>[/b]
   </body>
  </html>
 </xsl:template>
 
 <!-- ++++++++++++++++++++++++++++++++++ -->
 <!-- ++++++++++++++++++++++++++++++++++ -->
 <xsl:template match="para">
  <p>
   [b]<xsl:apply-templates/>[/b]
  </p>
 </xsl:template>
 
 <!-- ++++++++++++++++++++++++++++++++++ -->
 <!-- ++++++++++++++++++++++++++++++++++ -->
 <xsl:template match="text">
  [b]<xsl:apply-templates/>[/b]
 </xsl:template>
 
 <!-- ++++++++++++++++++++++++++++++++++ -->
 <!-- ++++++++++++++++++++++++++++++++++ -->
 <xsl:template match="condition">
  <xsl:variable name="curNode" select="."/>
  
  <xsl:text> (CONDITION) </xsl:text>
  
  <xsl:choose>
   <xsl:when test="$curNode/@eval = 'ELSE'">
    <!-- process else statement -->
    <xsl:variable name="prevIfNode" select="$mergeData/condition[@ref = substring-after($curNode, '&lt;&lt;ELSE&gt;&gt;')]"/>
    
    <xsl:if test="$prevIfNode = 'TRUE'">
     <!-- go to end tag -->
     [b]<xsl:apply-templates select="ancestor::*/following::*/condition[text() = concat('&lt;&lt;ENDIF&gt;&gt;', $prevIfNode/@eval)]"/>[/b]
    </xsl:if>
    
   </xsl:when>
   <xsl:when test="$curNode/@eval != 'END'">
    <!-- process if statement -->
    <xsl:variable name="curIfNode" select="$mergeData/condition[@ref = $curNode]"/>
    
    <xsl:if test="$curIfNode != 'TRUE'">
     <xsl:choose>
      <xsl:when test="ancestor::*/following::*/condition/text() = concat('&lt;&lt;ELSE&gt;&gt;', $curNode)">
       <!-- go to else tag -->
       [b]<xsl:apply-templates select="ancestor::*/following::*/condition[text() = concat('&lt;&lt;ELSE&gt;&gt;', $curNode)]"/>[/b]
      </xsl:when>
      <xsl:otherwise>
       <!-- go to end tag -->
       [b]<xsl:apply-templates select="ancestor::*/following::*/condition[text() = concat('&lt;&lt;ENDIF&gt;&gt;', $curNode)]"/>[/b]
      </xsl:otherwise>
     </xsl:choose>
    </xsl:if>
   </xsl:when>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top