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

setting nulls to a default value

Status
Not open for further replies.

ianbrad

Technical User
Aug 8, 2006
4
GB
Hi can anyone help with this problem? I have xml file which occasionally has data missing from it like this Post Code example:

<Street>20 Any Street</Street>
<City>London</City>
<Post Code></Post Code>

This data is then formatted using an XSL file. What I'd like to know is if there's any way I can set post code to be a default value in the XSL file if its NULL in the XML file? The XSL file formats like so:

<xsl:value-of select="Street"/>
<xsl:value-of select="City"/>
<xsl:value-of select="Post Code"/>

Thanks, I'm new to XML/XSL and I'm sure this is an easy one for some of you guys.

Thanks,
Ian
 
Like this for instance.
[tt]
<xsl:if test="string-length(normalize-space())=0">
<xsl:text>some default message</xsl:text>
</xsl:if>
<xsl:if test="not(string-length(normalize-space())=0)">
<xsl:value-of select="normalize-space()" />
</xsl:if>
[/tt]
 
Ah yes something along those lines. How do I set up the field to be referenced in the if statement?

Say I have this postcode field:

<xsl:value-of select="Post Code"/>

How would I tie that in with the if statement? Do I have to put another tag around it like so:

<postcode>
<xsl:value-of select="Post Code"/>
<xsl:if postcode="string-length(normalize-space())=0">
<xsl:text>some default message</xsl:text>
</xsl:if>
<xsl:if postcode="not(string-length(normalize-space())=0)">
<xsl:value-of select="normalize-space()" />
</xsl:if>
</postcode>

Or am on the wrong tracks? Does this code allow me to check to see if a specific field is null or does this act for all nulls in any fields? Ideally I'd like a solution to be specific for one field.
 
[1] Your node name "Post Code" is unacceptable. Having space(s) in it is not acceptable. So it should be "PostCode" at least.

[2] Suppost those nodes are under the root node named "root", then this shows you in what context those elements xsl:if are applied.
[tt]
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:eek:utput method="xml" encoding="utf-8" />
<xsl:template match="root">
<xsl:element name="{name()}">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:if test="string-length(normalize-space())=0">
<xsl:text>some default message</xsl:text>
</xsl:if>
<xsl:if test="not(string-length(normalize-space())=0)">
<xsl:value-of select="normalize-space()" />
</xsl:if>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
[/tt]
 
Ok thanks for your reply. At the moment I have this:

<xsl:template match="Rec">
<xsl:value-of select="Street"/>
<xsl:value-of select="City"/>
<xsl:value-of select="Postcode"/>
</xsl:template>

So is there a way I can check only the postcode node for null values? I'm not clear on the syntax of how your example would integrate with mine. Sorry for the basic questions, this is my first day looking and XSL!
 
>Sorry for the basic questions, this is my first day looking and XSL!
Just my level. If you see I am not responding, it will be because it is too advanced for me to give advice.

To answer to what you have---you won't eventually like it, I don't like it---but you then have to figure it out yourself after. Fair? or ask your prof.
[tt]
<xsl:template match="Rec">
<xsl:if test="string-length(normalize-space(Street))=0">
<xsl:text>some default message</xsl:text>
</xsl:if>
<xsl:if test="not(string-length(normalize-space(Street))=0)">
<xsl:value-of select="normalize-space(Street)" />
</xsl:if>
<xsl:if test="string-length(normalize-space(City))=0">
<xsl:text>some default message</xsl:text>
</xsl:if>
<xsl:if test="not(string-length(normalize-space(City))=0)">
<xsl:value-of select="normalize-space(City)" />
</xsl:if>
<xsl:if test="string-length(normalize-space(Postcode))=0">
<xsl:text>some default message</xsl:text>
</xsl:if>
<xsl:if test="not(string-length(normalize-space(Postcode))=0)">
<xsl:value-of select="normalize-space(Postcode)" />
</xsl:if>
</xsl:template>
[/tt]
 
Hi thanks for your help, I'll give this a try. I don't have a prof to ask since I've been asked to look at this in my job! Would be nice if I did though.
 
ianbrad,

Don't take offense. You fit the profile of a student posting -- first post ever on Tek-Tips, etc. But help was offered by one of the best, though tsuji is somewhat modest about his 'level'. :)

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top