Forum Moderators: open

Message Too Old, No Replies

Use attribute to specify variable name.?

         

Claes100

1:03 pm on Feb 11, 2008 (gmt 0)

10+ Year Member



Hi,
can anyone please help me with this:

I would like to have about 10 variables
<xsl:variable name="syr">System Responsible</xsl:variable>
<xsl:variable name="sur">Support Responsible</xsl:variable>
<xsl:variable name="sar">Sales Responsible</xsl:variable>
and so on.

In my xml source I have an element <activity responsible="#*$!"...>. The responsible attribute can have one of the 10 values above, i.e. "syr, sur, sar ..." and so on.
When outputting an activity, I would like to use the responsible attribute to get the variable name and print out the value of that variable, like:

<xsl:value-of select="$(@responsible)" /> (in plain words :-))

but I cannot work it out...

Thanks in advance
/Claes

Claes100

1:09 pm on Feb 11, 2008 (gmt 0)

10+ Year Member



Whoops,
I guess my triple x as the attribute value was censured.
The activity element is: <activity responsible="some_value"...>

/Claes

cmarshall

1:54 pm on Feb 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Boy, I'm having trouble figuring out exactly what you want here. Let me see if I have it right:

You want the name of the variable in the select XPath to be dictated by the XML?

If so, I'll have to think about it. May not be able to get there from here, but we can usually find a workaround.

BTW: Are you restricted to XSLT 1 (i.e. use it in PHP)?

Claes100

2:35 pm on Feb 11, 2008 (gmt 0)

10+ Year Member



Hi,
sorry for not making myself clear... Yes, you understood it correctly. It would work the same way as:
(activity element selected)

<xsl:choose>
<xsl:when test="@responsible='syr'">
<xsl:text>System Responsible</xsl:text>
</xsl:when>
<xsl:when test="@responsible='sur'">
<xsl:text>Support Responsible</xsl:text>
</xsl:when>
:
:
</xsl:choose>

Instead I'd like to use @responsible attribute of the activity element and use it to "fetch" the right variable to print out "System Responsible", "Support Responsible" or other.

Yes, for the moment I am stuck with XSLT 1.0

Thanks again!
/Claes

httpwebwitch

3:38 pm on Feb 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hey
Instead of using variables, create a template, then call it with <xsl:call-template> putting @responsible in the <xsl:with-param>.
You'll achieve the same thing, and it's just as easy to maintain.

Below is a working example

XML


<?xml version="1.0" encoding="UTF-8"?>
<x>
<activity responsible="sur" />
<activity responsible="syr" />
<activity responsible="sar" />
</x>

XSL


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/x/activity">
<div>
<xsl:call-template name="responsible">
<xsl:with-param name="r" select="@responsible"></xsl:with-param>
</xsl:call-template>
</div>
</xsl:template>
<xsl:template match="responsible" name="responsible">
<xsl:param name="r"></xsl:param>
<xsl:if test="$r='syr'">System Responsible</xsl:if>
<xsl:if test="$r='sur'">Support Responsible</xsl:if>
<xsl:if test="$r='sar'">Sales Responsible</xsl:if>
</xsl:template>
</xsl:stylesheet>

Output


<?xml version="1.0" encoding="UTF-8"?>
<div>Support Responsible</div>
<div>System Responsible</div>
<div>Sales Responsible</div>

Claes100

7:27 pm on Feb 11, 2008 (gmt 0)

10+ Year Member



Oohh... so simple...,
I always end up complicating things...
Thanks!

/C

cmarshall

7:36 pm on Feb 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oohh... so simple...,
I always end up complicating things...
Thanks!

That guy's pretty good.

My experience is is that XSLT is a "non-linear language." Many of the solutions I have found come from not only thinking outside the box, but taking psychedelics, and then working on the issue.

To wit [webmasterworld.com]