Using an xsl param as argument to XPath function

1.4k Views Asked by At

I've been trying to figure out a way to use a param/variable as an argument to a function. At the very least, I'd like to be able to use basic string parameters as arguments as follows:

 <xsl:param name="stringValue" default="'abcdef'"/>
 <xsl:value-of select="substring(string($stringValue),1,3)"/>

The above code generates no output.

I feel like I'm missing a simple way of doing this. I'm happy to use exslt or some other extension if an xslt 1.0 processor does not allow this.

Edit:

I am using XSL 1.0 and transforming using Nokogiri, which supports XPATH 1.0 . Here is a more complete snippet of what I am trying to do:

I want to pass column numbers as parameters using nokogiri as follows

document = Nokogiri::XML(File.read('table.xml'))
template = Nokogiri::XSLT(File.read('extractTableData.xsl'))
transformed_document = template.transform(document,
                ["tableName","'Problems'",      #Table Heading
                    "tablePath","'Table'",      #Takes an absolute XPATH String
                    "nameColumnIndex","2",      #column number 
                    "valueColumnIndex","3"])    #column number
 File.open('FormattedOutput.xml', 'w').write(transformed_document)

My xsl then wants to access every TD[valueColumnIndex] and and retrieve the first 3 characters at that position, which is why I am using a substring function. So I want to do something like:

<xsl:value-of select="substring(string(TD[$valueColumnIndex]),1,3)"/>

Since I was unable to do that, I tried to extract TD[$valueColumnIndex] to another param valueCode and then do substring(string(valueCode),1,3)

That did not work either (which is to say, no text was output, whereas <xsl:value-of select="$valueCode"/> gave me the expected output).

As a result, i decided to understand how to use parameters better, I would just use a hard coded string, as mentioned in my earlier question.

Things I have tried:

using single quotes around abcdef (and not) while using string() around the param name (and not)

Based on the comments below, it seems I am handicapped in my ability to understand the error because Nokogiri does not report an error for these situations. I am in the process of installing xsltproc right now and seeing if I receive any errors.

Finally, here is my entire xsl. I use a separate template forLoop because of the valueCode param I am creating. The lines of interest are the last 5 or so. I cannot include the xml as there are data use issues involved.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ext="http://exslt.org/common"
  xmlns:dyn="http://exslt.org/dynamic"
  exclude-result-prefixes="ext dyn">
<xsl:param name="tableName" /> 
<xsl:param name="tablePath" />
<xsl:param name= "nameColumnIndex" />
<xsl:param name= "valueColumnIndex"/>

<xsl:template match="/"> 
  <xsl:param name="tableRowPath">
    <xsl:value-of select="$tablePath"/><xsl:text>/TR</xsl:text>
  </xsl:param>
  <!-- Problems -->
  <section>
    <name>
      <xsl:value-of select="$tableName" />
    </name>
    <!-- <xsl:for-each select="concat($tablePath,'/TR')"> -->
    <xsl:for-each select="dyn:evaluate($tableRowPath)">
      <!-- Encode record section -->
      <xsl:call-template name="forLoop"/>
    </xsl:for-each>
  </section>

</xsl:template>
<xsl:template name="forLoop">
  <xsl:param name="valueCode">
    <xsl:value-of select="./TD[number($valueColumnIndex)][text()]"/>
  </xsl:param>
  <xsl:param name="RandomString" select="'Try123'"/>
 <section>
    <name>
      <xsl:value-of select="./TD[number($nameColumnIndex)]"/>
    </name>
    <code>
      <short>
        <xsl:value-of select="substring(string($valueCode),1,3)"/>
      </short>
      <long>
        <xsl:value-of select="$valueCode"/>
      </long>
    </code>
  </section>
</xsl:template>
</xsl:stylesheet>

1

There are 1 best solutions below

1
On BEST ANSWER

Use it this way:

<xsl:param name="stringValue" select="'abcdef'"/>
<xsl:value-of select="substring($stringValue,1,3)"/>