1

I'm running an xsl transition on some xml and need to be able to set some default values on a few tags if they appear as empty. For example, my xml has

<record>
<name>Bob</name>
<latitude>51.23645</latitude>
<longitude>-0.1254</longitude>
<rank></rank>
</record>

<record>
<name>Chantel</name>
<latitude></latitude>
<longitude></longitude>
<rank>5</rank>
</record>

and I would like to set some defaults to output:

<record>
<name>Bob</name>
<latitude>51.23645</latitude>
<longitude>-0.1254</longitude>
<rank>0</rank>
</record>

<record>
<name>Chantel</name>
<latitude>0.00</latitude>
<longitude>0.00</longitude>
<rank>5</rank>
</record>

I thought this would be straightforward but can't seem to crack it.

Thanks in advance.

Edit: This is what I was trying to do. Still learning so just a fumble in the dark!

<xsl:template match="record">
  <xsl:when test="name()='latitude'">
    <xsl:element name="latitude">
      <xsl:choose>
        <xsl:when test="text()=''">
          <latitude>0.00</latitude>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="latitude"></xsl:value-of>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:element>
  </xsl:when>
</xsl:template>
2
  • Can you show how your XSL 'transition'? Are you using parameters? Commented May 20, 2011 at 14:49
  • @empo I've just edited my post to show my (poor) attempt at writing this transition... Commented May 20, 2011 at 15:48

1 Answer 1

3

Try this:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="rank[not(text())]">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:text>0</xsl:text>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="latitude[not(text())]|longitude[not(text())]">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:text>0.00</xsl:text>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Add more templates to set default values for other tags.

How it works: The first template is known as the "identity template", and copies nodes from input to output unchanged. The second template matches "rank" nodes without text child nodes (i.e. empty "rank" nodes), copies them to the output with their attributes and then inserts the default.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but I'm unable to get this to work. Just the tags that already have a value are processed. Empty xml tags don't output.
This works for me using Oxygen/XML's XSLT processor (Saxon). Which XSLT processor are you using? Did you add templates, using the pattern of the second template in my reply, for ALL the different tags for which you want to set default values?
Thanks Jim. I'm using MS Access as the processor. I have lots of tags that need a default (the above is just a demonstration) up so tested this on a single tag - would that make a difference?
I've modified the reply to include defaults for rank, latitude and longitude.
Jim - thanks! It's working perfectly and +1 for your explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.