views:

148

answers:

1

So I have the following code:

<redirect:write select="concat('..\\folder\\,string(filename),'.xml')">

Where "filename" is a tag in the xml source. My problem occurs when filename is null or blank. And this is the case for several of the xml filename tags. So what I am trying to implement is a checking method. This is what I have done:

<xsl-if test = "filename != ''">
        <xsl:variable name = "tempName"  select = "filename" />
        </xsl-if>
        <xsl-if test ="filename = ''">
        <xsl:variable name = "tempName" select = "filenameB"/>
        </xsl-if>

<redirect:write select="concat('..\\folder\\,string($tempName),'.xml')">

I seem to be getting NPEs when I compile my Java code, saying the Variable not resolvable: tempName

+2  A: 

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:redirect="my:redirect"
 >
 <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="filename">
   <xsl:variable name="tempName">
     <xsl:choose>
      <xsl:when test="text()">
        <xsl:value-of select="."/>
      </xsl:when>
      <xsl:otherwise>filenameB</xsl:otherwise>
     </xsl:choose>
   </xsl:variable>

   <redirect:write select="..\\folder\\{$tempName}.xml"/>
 </xsl:template>
</xsl:stylesheet>

when applied to this XML document:

<t xmlns:redirect="my:redirect">
  <filename>Z</filename>
  <filename/>
</t>

produces the wanted, correct result:

   <t xmlns:redirect="my:redirect">
    <redirect:write select="..\\folder\\Z.xml" />
    <redirect:write select="..\\folder\\filenameB.xml" />
   </t>

Do note: Whenever the value of a variable is established based on a condition, this condition is implemented using <xsl:choose> inside the body of the variable.

Dimitre Novatchev
good one :). XSL is really an amazing language.
Paarth