Santosh-Sidd
Santosh-Sidd

Reputation: 49

Using a variable outside the scope of XSLT template

I am trying to transform an xml from one format to another and i need some help as i am new to xslt. My input/source xml is like this:

<field name="Duration">
    <Duration value="00:13:56:544" />
    <DurationFrames value="20057" />
    <DurationSMPTE value="00:13:56:13" />
</field>

And the desired output xml should be like this:

<field internal="yes" name="Frames" value="20057"/>
<field internal="yes" name="Duration_msec" value="00:13:56:544"/>
<field internal="yes" name="Duration_SMPTE" value="00:13:56:13"/>
<field name="Duration" value="00:13:56:544(13)"/>

Here the value of Duration:00:13:56:544(13) is a combination of two fields:

1) Duration_msec (00:13:56:544)

2) The last part of Duration_SMPTE (00:13:56:13) which is 13

This is a small segment of a larger XML, i am only posting the XML node where i am finding difficulty.

Here is what the XSLT i have written:

<xsl:when test="@name='Duration'">                    
    <xsl:apply-templates select="node()" />
 </xsl:when>

and here are my templates:

  <xsl:template match="Duration">
    <xsl:variable name="var_Duration_val" select="@value" />
    <field internal="yes" name="Duration_msec" value="{$var_Duration_val}"/>
  </xsl:template>

  <xsl:template match="DurationFrames">
    <xsl:variable name="var_Duration_Frames" select="@value" />
    <field internal="yes" name="Frames" value="{$var_Duration_Frames}"/>
  </xsl:template>

  <xsl:template match="DurationSMPTE">
    <xsl:variable name="var_Duration_smpte" select="@value" />
    <field internal="yes" name="Duration_SMPTE" value="{$var_Duration_smpte}"/>
  </xsl:template>

I am facing problem in achieving the last part , ie,

<field name="Duration" value="00:13:56:544(13)"/>

The variables are in scope of different templates. Please help me in achieving the last line of output.

Upvotes: 0

Views: 1105

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117165

How about simply:

<xsl:template match="field[@name='Duration']">
    <field internal="yes" name="Frames" value="{DurationFrames/@value}"/>
    <field internal="yes" name="Duration_msec" value="{Duration/@value}"/>
    <field internal="yes" name="Duration_SMPTE" value="{DurationSMPTE/@value}"/>
    <field name="Duration" value="{Duration/@value}({substring(DurationSMPTE/@value, 10,2)})"/>
</xsl:template>

Upvotes: 2

Related Questions