1

I am writing text output files reading an XML file using XSL.

Here i am trying to check weather a particular content is available in the source XML and write that content to a file if available.

But if the content is not available ( not fulfilling "<XSL:if>" condition), then output file would be an empty file.

So I want to add an else condition and in that else condition to avoid XSL output file being created at runtime.

Any body having any clue?

<xsl:message terminate="yes"> wont help because it does generate the output but only terminating the further processing of XSL.

Can any body help or even suggest any other approach to be taken in java code even without deleting files after they have created. [By reading them and identifying empty files]

Currently I am using java to read the created empty files and delete them explicitly. Thanks in adavance.

Dilruk
  • 379
  • 5
  • 8

1 Answers1

1

I will give two examples how this can be done -- the second is what I recommend:

Suppose we have this XML document:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

and we want to produce another one from it, in which the num elements with even numbers are "deleted".

One way of doing this is:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
     <nums>
       <xsl:apply-templates/>
     </nums>
 </xsl:template>

 <xsl:template match="num">
  <xsl:choose>
   <xsl:when test=". mod 2 = 1">
     <num><xsl:value-of select="."/></num>
   </xsl:when>
   <!-- <xsl:otherwise/> -->
  </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

The wanted result is produced:

<nums>
   <num>01</num>
   <num>03</num>
   <num>05</num>
   <num>07</num>
   <num>09</num>
</nums>

Do notice: For "not doing anything" you even don't need the <xsl:otherwise> and it is commented out.

A better solution:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="num[. mod 2 = 0]"/>
</xsl:stylesheet>

This produces the same correct result.

Here we are overriding the identity rule with a template matching num elements with even value and with empty body -- which does the "delete".

Do notice:

Here we don't use any "if-then-else" explicit instructions at all -- just Xtemplate pattern matching, which is the most distinguishing feature of XSLT.

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Thanks a lot for your time sir, what i need is to avoid the output file being generated at run time inside the else part. I have some feeling that this cannot be done inside the XSL but would bring a nice topic for all of us to think of. Even if we can overcome this problem at the java end yur help is highly appreciated. Thanks again :) – Dilruk May 30 '12 at 16:18
  • @dilruk: I have answered your question to the best extent possible, given the provided information. If you feel that this doesn't solve your problem, please, edit the question and give a complete (as small as possible), real example. Don't leave people trying to *guess* what the problem is. – Dimitre Novatchev May 30 '12 at 16:33
  • What i need is to avoid output file being generated at run time. [ After checking a condition ] lets say I want to check whether number 11 is there in the above xml file. And if it doesn't exist i don't want to have an output file because i only need files which are having num 11. Because after that I can assume all the output files being generated are having num 11 so I can do further processing without doing further filtering at java end. – Dilruk May 30 '12 at 17:10
  • @dilruk: I see now... Could you, please edit the question and explain this there? As seen from the comments, other people are confused, too. THis problem has a nice solution with XSLT 2.0 -- can you use an XSLT 2.0 processor? – Dimitre Novatchev May 30 '12 at 18:43
  • WOW! really amazed to hear that and really looking forward for that. I guess I can get the use of XSLT2.0. I would be glad if you can tell if there are any sort of solution in prior version too. I added a new question its in this link i am extremely sorry for the trouble. But really appreciate the commitment of yours :) http://stackoverflow.com/questions/10823145/avoid-creating-output-files-from-xsl-files-within-itself – Dilruk May 30 '12 at 19:49