FordPrefect141
FordPrefect141

Reputation: 205

Unable to use variable in template match. xslt version 1

everyone.

I am trying to create an xslt stylesheet to run a transformation against an xml file. The trouble is that as I have to use version 1 I am unable to use a variable or pararmeter as part of the 'template match' call.

In essence... here is a sample of the xml I am working with...

<tbody>
    <tr layoutcode="" type="categoryhead" level="2">
        <td colname="1">&lt;1&gt;Common stocks[Stop Here] 87.49%</td>
        <td colname="2"/>
        <td colname="3"/>
        <td colname="4"/>
    </tr>
    <tr layoutcode="" type="categoryhead" level="3">
        <td colname="1">&lt;2&gt;Health care 23.42%</td>
        <td colname="2"/>
        <td colname="3"/>
        <td colname="4"/>
    </tr>
    <tr layoutcode="" type="detail" level="5">
        <td colname="1">Gillan Sciences, Inc.[Category Caption]1</td>
        <td colname="2">19,127,226</td>
        <td colname="3">1,583,543</td>
        <td colname="4">4.04</td>
    </tr>

</tbody>

I want to stop at the node that contains the text '[Category caption' and then do something. I can do it like this....

<xsl:template match="tr[@type = 'detail']/td[contains(./text(), '[Category Caption]')]">
    (do something)

.... but I want the text to be a variable and xslt won't let me do this..

<xsl:template match="tr[@type = 'detail']/td[contains(./text(), $VariableName)]">
    (do something)

Does anyone have an idea on how I can do a more generic template match and then maybe have a choose option to determine whether the element text contains whatever is in my variable?

Many thanks Fordprefect141

Upvotes: 0

Views: 359

Answers (3)

stwissel
stwissel

Reputation: 20384

The match conditions on a template are static by design since you can't hand over a variable during the matching process. What you can do is to hand over a parameter and use an <xsl:if> condition to execute only if met. Something along these lines:

 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:param name="outside" /> <!-- your XSLT processor would supply that one -->

 <xsl:template match="something">
     <xsl:call-template name="flexitemp">
        <xsl:with-param name="inside" select="@name" />
     </xsl:call-template>
</xsl:template>

<xsl:template name="flexitemp" match="/">
     <xsl:param name="inside" />
     <xsl:if test="$inside=$outside">
         <!-- Do your thing here -->
     </xsl:if> 
</xsl:template>

 </xsl:stylesheet>

The select and parameters are just the example. You get the idea?

Upvotes: 0

Cylian
Cylian

Reputation: 11181

Try this out:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output indent="yes" method="xml"/>

    <xsl:param name="text2search" select="'[Category Caption]'"/>

    <xsl:template match="/">
        <xsl:call-template name="useVariable">
            <xsl:with-param name="text2search" select="$text2search"/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template name="useVariable">
        <xsl:param name="text2search"/>
        <result>
            <xsl:if test="//tr[@type = 'detail']/td[contains(text(), $text2search)]">
                <xsl:copy-of select="(//tr[@type = 'detail']/td)[1]"/>
            </xsl:if>
        </result>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167716

Well given

<xsl:param name="VariableName" select="'[Category Caption]'"/>

<xsl:template match="tr[@type = 'detail']/td">
  <xsl:choose>
   <xsl:when test="contains(., $VariableName)">...</xsl:when>
   <xsl:otherwise>...</xsl:when>
  </xsl:choose>
</xsl:template>

you can use the variable or parameter inside of the template. An xsl:if might suffice if you don't need the xsl:otherwise.

Upvotes: 1

Related Questions