james4563
james4563

Reputation: 169

XSL variable seems to be returning null

I have the following XSL file from the DCM4CHEE DICOM project and I was trying to adjust it slightly. The code I'm actually trying to get working is commented out, but even the variable assignment seems to actually be returning null. The DCM4CHEE logs are throwing Java exceptions with a 'null' seeming to be coming from the XSL template when it compiles it.

<xsl:call-template name="attr">
      <xsl:with-param name="tag" select="'00100040'"/>
      <xsl:with-param name="vr" select="'CS'"/>

      <xsl:variable name="testing" select="string(field[8]/text())" />    
      <xsl:with-param name="val" select="$testing" />

      <!-- 
      <xsl:variable name="sexString" select="string(field[8]/text())" />
      <xsl:variable name="sex">
          <xsl:choose> 
            <xsl:when test="$sexString='1'">M</xsl:when> 
            <xsl:when test="$sexString='2'">F</xsl:when> 
            <xsl:when test="$sexString='9'">U</xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$sexString"/>
            </xsl:otherwise> 
          </xsl:choose>
      </xsl:variable>

      <xsl:with-param name="val" select="$sex" /> -->

</xsl:call-template>

The normal XSL is just one simple line:

<xsl:with-param name="val" select="string(field[8]/text())" />

I'm probably doing something very wrong, but can someone explain why I'm not able to assign field[8]/text() to a variable and then pass it to the with-param?

Upvotes: 1

Views: 593

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243599

<xsl:call-template name="attr">
      <xsl:with-param name="tag" select="'00100040'"/>
      <xsl:with-param name="vr" select="'CS'"/>
      <xsl:variable name="testing" select="string(field[8]/text())" />
      <xsl:with-param name="val" select="$testing" />
</xsl:call-template>

I'm probably doing something very wrong, but can someone explain why I'm not able to assign field[8]/text() to a variable and then pass it to the with-param?

Yes, the code is so wrong that the XSLT processor should throw an error message without compiling/executing it.

According to the W3C XSLT 1.0 specification, the only allowed element as child of xsl:call-template is xsl:with-param.

The presented code clearly violates this syntactic rules by placing other elements (xsl:variable) as children of xsl:call-template).

Solution: Move the variables out (before) the xsl:call-template:

<xsl:variable name="testing" select="string(field[8]/text())" />

<xsl:call-template name="attr">
    <xsl:with-param name="tag" select="'00100040'"/>
    <xsl:with-param name="vr" select="'CS'"/>
    <xsl:with-param name="val" select="$testing" />
</xsl:call-template>

The code above is syntactically correct.

Upvotes: 1

Related Questions