[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Tag with no data or missing tag - supply default


Subject: RE: [xsl] Tag with no data or missing tag - supply default
From: cknell@xxxxxxxxxx
Date: Thu, 16 Sep 2004 20:09:42 -0400

I had to modify your sample input in order to make it a well-formed document by enclosing your "recordx" elements in a parent element. Having done this, the stylesheet below will produce the output you say you want, but I suspect that your actual input doesn't match the sample you provided. Be careful of what you ask for. You may get it.

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

  <xsl:template match="records/*">
    <xsl:choose>
      <xsl:when test="test='' or not(test)">
        <xsl:element name="{local-name()}">
          <test>fail</test>
          <data><xsl:value-of select="data" /></data>
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="." />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
-- 
Charles Knell
cknell@xxxxxxxxxx - email



-----Original Message-----
From:     Mila P. Mitra <pmitra@xxxxxxxxxxxxxxxxx>
Sent:     Thu, 16 Sep 2004 15:48:45 -0700
To:       <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Subject:  [xsl] Tag with no data or missing tag - supply default

Hello,
 I would like some help on how to use a stylesheet to supply a default value
if there is a certain element tag with no data within it, or if the element
is missing altogether. For example, here is an xml document extract.

I would like the output to always have a tag for <test> and if it doesnt
find a value to insert a default value of 'fail' in it. This is bacause my
output file will become input to another program which needs that tag and a
default value.

I would appreciate it if someone could please help me with the xsl code that
could do this transformation (formatting, i.e. whether output is HTML or XML
or text does ot matter, I can do that part):
<record1>
	<test>pass</test>
	<data>zzz</data>
<record1>
<record2>
	<test></test>
	<data>xxx</data>
</record2>
<record3>
	<data>yyy</data>
</record3>

Desired output:
record1:
pass
zzz

record2:
xxx
fail

record3:
yyy
fail

Thanks in advance,
Mila


---------------------------------------------
Mila (Patralekha) Mitra
650.604.1814
Northrop Grumman IT, NASA Ames, CA


--+------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx>
--+--


Current Thread
Keywords