tags:

views:

578

answers:

3

I have a situation where the param won't be set, but my attempts to capture & handle the sitation aren't working.

I'm using the following:

<xsl:template match="xs:complexType">
  <xsl:param name="prefix" />

  <xsl:variable name="prefix-no-core">
    <xsl:choose>
      <!-- if no value, default to 'AcRec' -->
      <xsl:when test="not($prefix)"> 
        <xsl:value-of select="'AcRec'" />
      </xsl:when>
      <!-- if 'core', leave as empty string -->
      <xsl:when test="$prefix = 'core'">
      </xsl:when>
      <!-- if 'AcRec', set the value -->
      <xsl:when test="$prefix = 'AcRec'">
        <xsl:value-of select="$prefix" />
      </xsl:when>      
    </xsl:choose>
  </xsl:variable>

  <xs:complexType name="{concat($prefix-no-core, @name)}">
  ...
</xsl:template>

I've also tried $prefix='' in the first test - neither work. but if I use:

<xsl:value-of select="not($prefix)" />

... the value prints out as true. But using that in my xsl:choose doesn't produce any output.

A: 

Note: replaced old answer, check history if you want it.

The following input:

<test xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
    <xs:complexType name="something"/>
    <xs:complexType name="somethingElse"/>
</test>

Fed to the following XSLT:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:template match="/">
     <xsl:for-each select="node()">
      <xsl:apply-templates/>
     </xsl:for-each>
    </xsl:template>

    <xsl:template match="xs:complexType">
     <xsl:param name="prefix" />
     <xsl:variable name="prefix-no-core">
      <xsl:choose>
       <xsl:when test="not($prefix)">AcRec</xsl:when>
       <xsl:when test="$prefix = 'core'"/>
       <xsl:when test="$prefix = 'AcRec'">AcRec</xsl:when>                       
      </xsl:choose>
     </xsl:variable>

     <xs:complexType name="{concat($prefix-no-core, @name)}"/>
    </xsl:template>
</xsl:transform>

Gives the following result:

<xs:complexType name="AcRecsomething" xmlns:xs="http://www.w3.org/2001/XMLSchema"/&gt;
<xs:complexType name="AcRecsomethingElse" xmlns:xs="http://www.w3.org/2001/XMLSchema"/&gt;

I'm not sure what more you're looking for...

Chris Marasti-Georg
I need to support the possibility of other prefixes.
OMG Ponies
Tried it - the xsl:otherwise is giving me false positives, applying the prefix to those it should not.
OMG Ponies
When do you not want to apply the prefix? Again, give us the complete use-case. If prefix=core, you want no prefix. If prefix is not set, you want AcDec as the prefix. What is the other case? Do you want to use the prefix provided, or do you want to have to manually add a case for each "allowed" prefix. If so, what happens the caller provides a prefix you don't recognize?
Chris Marasti-Georg
Does the first code snippet work? If not, what is the problem you are seeing with it?
Chris Marasti-Georg
For the current file, "core" and "AcRec" are the only prefixes I can expect. However, I will not use != 'core' because the source could change. The issue is that the first time the template runs, there won't be a prefix value. But attempts to catch that situation in a WHEN test have all failed - not($prefix) and $prefix='' do not execute when I test it.
OMG Ponies
Try your example where you provide 'core' as the prefix - it won't work. Output ultimately is arbitrary - the point is to accurately handle when a parameter/variable value is/is not set.
OMG Ponies
A: 

It's a hack, but I have had success by first wrapping the parameter with the normalize-space() function before testing for an empty param.

<xsl:value-of select="not(normalize-space($prefix))" />
Ishmael
I tried that as a when clause - it was handling false positives.
OMG Ponies
A: 

Of the suggestions made to date, the only solution that worked was to always set the parameter value when a call to the template is made. For example, previously I had:

<xsl:apply-templates select="//xs:complexType[@name='AddressType']" />

This had to be changed to:

<xsl:apply-templates select="//xs:complexType[@name='AddressType']">
  <xsl:with-param name="prefix" select="'AcRec'" />
</xsl:apply-templates>

I'd really like to know why not($param) and $param='' don't work in a WHEN test, yet I can get the value of not($param) in a xsl:value-of statement.

OMG Ponies