tags:

views:

67

answers:

2

I have the following XML file :

<?xml version="1.0"?>
<Table69>
</Table69>

And want to read the body of element "Table69", I have used the following XSL file :

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output method="xml"/>
  <xsl:template match="/">    
    <xsl:variable name="table" >     
        <xsl:choose>
        <xsl:when test="normalize-space(.) != ''" >
           <xsl:value-of select="." />
        </xsl:when>
        <xsl:otherwise>
            <Exception>
                field was missing 
            </Exception>                 
        </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <Table id="69">
        <xsl:value-of select="$table" />
    </Table>

  </xsl:template>
</xsl:stylesheet>

The problem is : When the xml file contains value under Table69 tage, this value is beging printed successfully, but; when this tag doesn't contains a value, the xsl vairalbe "table" should contain the following :

<Exception>
    field was missing 
</Exception>

but, it doesn't include tage, here's a sample result of transforming :

<?xml version="1.0"?>
<Table id="69">
    field was missing       <!-- where's the Exception tag surrounding the text field was missing ??? -->
</Table>
+2  A: 

xsl:value-of will print the text value of the selected contents, use xsl:copy-of to output the entire contents including any nodes.

<xsl:copy-of select="$table" />
Thiyagaraj
A: 

Do the following instead of <Exception>:

  <xsl:element name="Exception">
    <xsl:text>Field was missing</xsl:text>
  </xsl:element>

That should generate your <Exception> XML element. Remove the <xsl:text> tags if you need newlines surrounding the text.

0x6adb015
Unfortunately, the value-of will still output the text value which would omit the nodes.
Thiyagaraj