user1454141
user1454141

Reputation: 3

Using template parameters as modifiers to xml pointers (xpaths)

I have searched all morning and yesterday afternoon and still cannot find an solution to my problem. It would be wonderful if you would help.

I am working on an xsl-fo file that will be translated to PDF format with apache fop. I am trying to use parameters to modify xpaths to my xml file. It is a much more complicated system than the example following, but I have simplified it to the root of the problem.

Suppose my xml file looks like:

<sc931>
    <whyoming>
              <train>
                   <miles>98</miles>
                   <time>9888</time>
    </whyoming>
    <georgia>
              <train>
                   <miles>98</miles>
                   <time>9888</time>
    </georgia>
</sc931>

What I am trying to do is use a template parameter to access these values like so:

Declaration of parameter

<xsl:param name="state" select="whyoming"/>

Call

<xsl:value-of select="concat(/sc931/, $state, /train/miles)"/>

But this call does not work because the concat function returns a string rather than a pointer to the proper node. What is the correct way to use a template parameter to modify an address given to a value-of statement?

Upvotes: 0

Views: 261

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167716

Before XSLT 3.0 there is no dynamic XPath evaluation but for your simple case it should suffice to use <xsl:value-of select="/sc931/*[local-name() = $state]/train/miles"/>.

[edit]I overlooked that your param is defined as <xsl:param name="state" select="whyoming"/>, you need to change that to <xsl:param name="state" select="'whyoming'"/> for my suggestion to work.

Upvotes: 3

Related Questions