Click here to Skip to main content
15,886,806 members
Home / Discussions / XML / XSL
   

XML / XSL

 
GeneralRe: XPath navigation with namespaces Pin
Steven Campbell30-Dec-04 4:44
Steven Campbell30-Dec-04 4:44 
GeneralXML validation through XSD Pin
Het210927-Dec-04 23:00
Het210927-Dec-04 23:00 
GeneralRe: XML validation through XSD Pin
DavidNohejl28-Dec-04 9:05
DavidNohejl28-Dec-04 9:05 
GeneralRe: XML validation through XSD Pin
Het210931-Dec-04 21:48
Het210931-Dec-04 21:48 
GeneralW3C XML standards Pin
Nosheen Iqbal21-Dec-04 16:20
Nosheen Iqbal21-Dec-04 16:20 
GeneralI want XML to draw the network topology Pin
Mohammed Aijaz Mohiuddin21-Dec-04 6:00
Mohammed Aijaz Mohiuddin21-Dec-04 6:00 
GeneralRe: I want XML to draw the network topology Pin
DavidNohejl21-Dec-04 8:29
DavidNohejl21-Dec-04 8:29 
GeneralSkip blocks of nodes with XSL Pin
Milby00720-Dec-04 0:22
Milby00720-Dec-04 0:22 
hi!!

i'm trying to process an xml file containing document tags & text, and skip certain blocks of the text based on condition tags. the problem i'm finding is that when i use apply-templates to move on and skip blocks of the document, it doesn't actually miss any of the text that i'm trying to avoid outputting to the resultant html document. i guess the question i have is, can you actually jump around an xml document using apply-templates?? i'm quite new to this stuff so please bear with me if i'm doing something wrong.

i've copied the code below just incase you want to see what i'm actually trying to do & run the transform for yourself. you'll need to create the following three files:

source.xml, "merge-data.xml" & test.xsl
(the file name in quotes has to have that exact name and be in the same directory as the stylesheet unless you want to change the code in the stylesheet).

source.xml:
<?xml version="1.0" encoding="UTF-8"?>
<br>
<!-- SOURCE DOCUMENT TO BE TRANSFORMED -->
<br>
<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>

"merge-data.xml":
<?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>

test.xsl:
<?xml version="1.0" encoding="UTF-8"?>

<!-- xslt stylesheet -->

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

 <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>
    
    <xsl:apply-templates select="doc/page/content"/>
   </body>
  </html>
 </xsl:template>
 
 <!-- ++++++++++++++++++++++++++++++++++ -->
 <!-- ++++++++++++++++++++++++++++++++++ -->
 <xsl:template match="para">
  <p>
   <xsl:apply-templates/>
  </p>
 </xsl:template>
 
 <!-- ++++++++++++++++++++++++++++++++++ -->
 <!-- ++++++++++++++++++++++++++++++++++ -->
 <xsl:template match="text">
  <xsl:apply-templates/>
 </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 -->
     <xsl:apply-templates select="ancestor::*/following::*/condition[text() = concat('&lt;&lt;ENDIF&gt;&gt;', $prevIfNode/@eval)]"/>
    </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 -->
       <xsl:apply-templates select="ancestor::*/following::*/condition[text() = concat('&lt;&lt;ELSE&gt;&gt;', $curNode)]"/>
      </xsl:when>
      <xsl:otherwise>
       <!-- go to end tag -->
       <xsl:apply-templates select="ancestor::*/following::*/condition[text() = concat('&lt;&lt;ENDIF&gt;&gt;', $curNode)]"/>
      </xsl:otherwise>
     </xsl:choose>
    </xsl:if>
   </xsl:when>
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>


thanks!!! Wink | ;)
GeneralRe: Skip blocks of nodes with XSL Pin
Milby00720-Dec-04 3:13
Milby00720-Dec-04 3:13 
GeneralRe: Skip blocks of nodes with XSL Pin
Milby00720-Dec-04 23:46
Milby00720-Dec-04 23:46 
GeneralBasic XML Pin
sharkbait16-Dec-04 3:55
sharkbait16-Dec-04 3:55 
GeneralXslt generated XML string format problem. Pin
titabonbon15-Dec-04 19:27
titabonbon15-Dec-04 19:27 
GeneralRe: Xslt generated XML string format problem. Pin
DavidNohejl17-Dec-04 12:17
DavidNohejl17-Dec-04 12:17 
GeneralRe: Xslt generated XML string format problem. Pin
titabonbon22-Dec-04 15:25
titabonbon22-Dec-04 15:25 
GeneralRe: Xslt generated XML string format problem. Pin
DavidNohejl23-Dec-04 0:06
DavidNohejl23-Dec-04 0:06 
GeneralRe: Xslt generated XML string format problem. Pin
titabonbon23-Dec-04 12:58
titabonbon23-Dec-04 12:58 
GeneralRe: Xslt generated XML string format problem. Pin
DavidNohejl23-Dec-04 13:28
DavidNohejl23-Dec-04 13:28 
GeneralRe: Xslt generated XML string format problem. Pin
titabonbon26-Dec-04 12:14
titabonbon26-Dec-04 12:14 
GeneralRe: Xslt generated XML string format problem. Pin
DavidNohejl26-Dec-04 14:16
DavidNohejl26-Dec-04 14:16 
QuestionDoes anybody know why XmlAttribute has ChildNodes property? Pin
DavidNohejl15-Dec-04 11:46
DavidNohejl15-Dec-04 11:46 
AnswerRe: Does anybody know why XmlAttribute has ChildNodes property? Pin
J4amieC20-Dec-04 3:09
J4amieC20-Dec-04 3:09 
GeneralRe: Does anybody know why XmlAttribute has ChildNodes property? Pin
DavidNohejl20-Dec-04 3:46
DavidNohejl20-Dec-04 3:46 
GeneralAsp.net - Xml Pin
Anonymous13-Dec-04 23:54
Anonymous13-Dec-04 23:54 
GeneralRe: Asp.net - Xml Pin
DavidNohejl14-Dec-04 2:15
DavidNohejl14-Dec-04 2:15 
GeneralConverting xsd via xsl to code Pin
mgaerber13-Dec-04 7:36
mgaerber13-Dec-04 7:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.