tags:

views:

720

answers:

2

The following isn't working as intended:

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

  <xsl:if test="$prefix='core'">
    <xsl:variable name="prefix" select=""/>
  </xsl:if>

  <xs:complexType name="{concat($prefix, @name)}">
    <xsl:apply-templates select="node()" />
  </xs:complexType>
  <xsl:apply-templates select=".//xs:element" />
</xsl:template>

The idea is that if the prefix variable value is "core", I don't want it to be added onto the name attribute value. Any other value, I'd like to be added. IE:

<xs:complexType name="coreBirthType">

...is not acceptable, while the following would be:

<xs:complexType name="BirthType">

But I have to allow for this to occur:

<xs:complexType name="AcRecHighSchoolType">

I tried this in a block, but saxon complains about not finding a closing node:

<xsl:choose>
  <xsl:when test="starts-with(.,'core')">
    <xs:complexType name="{@name)}">
  </xsl:when>
  <xsl:otherwise>
    <xs:complexType name="{concat($prefix, @name)}">
  </xsl:otherwise>
</xsl:choose>
  <xsl:apply-templates select="node()" />
</xs:complexType>

What is the best way to handle this?

+1  A: 

Well, you could use a if inside the variable; but in this case I think I'd try <xsl:attribute>:

<xs:complexType>
   <xsl:attribute name="name"><xsl:if test="$prefix != 'core'"><xsl:value-of select-"$prefix"/></xsl:if><xsl:value-of select="@name"/></xsl:attribute>
   <!-- etc -->
</xs:complexType>

the if approach:

<xsl:variable name="finalPrefix"><xsl:if test="$prefix != 'core'"><xsl:value-of select="$prefix"/></xsl:if></xsl:variable>
...
<xs:complexType name="{$finalPrefix}{@name}">
  <!-- etc -->
</xs:complexType>
Marc Gravell
+2  A: 

In XSLT, as a pure language with no side effects, variables are immutable. You cannot change a variable value. If you declare another <xsl:variable> with the same name, you define a new variable which shadows the old one.

Here's how you can do this:

<xsl:param name="prefix" />

<xsl:variable name="prefix-no-core">
  <xsl:if test="$prefix != 'core'">
    <xsl:value-of select="$prefix" />
  </xsl:if>
</xsl:variable>

<xs:complexType name="{concat($prefix-no-core, @name)}">
...
Pavel Minaev
Yes, once set, you can't reasign a value to prefix.
Mercer Traieste