XSL Transform Control Display Name

R

Ravi

My XML looks like:

<abc>
<def type="apple"> 1 </def>
<def type="peach"> 2 </def>
<def type="orange"> 3 </def>
<def type="banana"> 4 </def>
<def type="plum"> 5 </def>
</abc>

I can write the transform to have this display as

apple 1
peach 2
orange 3
banana 4
plum 5

However I want to create more flexibility in displaying the names. So
say I have an external text file something like:

apple Xapple
peach Xpeach
orange Xorange
banana Xbanana
plum Xplum

the first column is the internal names i.e. actual names as found in the
XML and the second column is user-defined (external names and hence
variant). So I want the XML to be displayed as

Xapple 1
Xpeach 2
Xorange 3
Xbanana 4
Xplum 5

i.e. I want to get the external name corresponding to the internal name
from the file and use that external name to display the value of the
element. This way a user can control the names used in the display by
merely editing the external text file and not having to work with
XML/XSLT. Any suggestions/pointers will be highly appreciated.

TIA.
 
R

Ravi

Ravi said:
My XML looks like:

<abc>
<def type="apple"> 1 </def>
<def type="peach"> 2 </def>
<def type="orange"> 3 </def>
<def type="banana"> 4 </def>
<def type="plum"> 5 </def>
</abc>

I can write the transform to have this display as

apple 1
peach 2
orange 3
banana 4
plum 5

However I want to create more flexibility in displaying the names. So
say I have an external text file something like:

apple Xapple
peach Xpeach
orange Xorange
banana Xbanana
plum Xplum

the first column is the internal names i.e. actual names as found in the
XML and the second column is user-defined (external names and hence
variant). So I want the XML to be displayed as

Xapple 1
Xpeach 2
Xorange 3
Xbanana 4
Xplum 5

i.e. I want to get the external name corresponding to the internal name
from the file and use that external name to display the value of the
element. This way a user can control the names used in the display by
merely editing the external text file and not having to work with
XML/XSLT. Any suggestions/pointers will be highly appreciated.

I thought of using Javascript (cannot read an external file so I
hardcoded the internal-external mappings into an associative array as
shown below.

<xsl:script language="javascript" implements-prefix="user">
<![CDATA[
function GetExternal(internal)
{
var my_cars= new Array()
my_cars["apple"]="Xapple";
my_cars["peach"]="Xpeach";
my_cars["orange"]="Xorange";
my_cars["banana"]="Xbanana";
my_cars["plum"]="Xplum";
return my_cars[internal];
}
]]>
</xsl:script>



and when I want to do the display I do

<xsl:template match="def">
<xsl:variable name="tmp"><xsl:value-of select="@type"/> </xsl:variable>
<xsl:value-of select="user:GetExternal($tmp)" />
<xsl:value-of select="."/>
</xsl:template>


but it does not work saying xsl:script cannot be declared in stylesheet
(or namespace?). Can someone kindly shed some light on how to use
javascript in xsl? Or can this kind of mapping be done at the XSL level
itself? Your comments are highly appreciated.

Thanks.
 
P

Patrick TJ McPhee

% However I want to create more flexibility in displaying the names. So
% say I have an external text file something like:
%
% apple Xapple
% peach Xpeach
% orange Xorange
% banana Xbanana
% plum Xplum

You can encode the external file in xml and access it using document()
in an XPath expression.

fruit.xml:

<fruit>
<name type='apple'>Xapple</name>
<name type='peach'>Xpeach</name>
<name type='orange'>Xorange</name>
<name type='banana'>Xbanana</name>
<name type='plum'>Xplum</name>
</fruit>

Then instead of
<xsl:template match='def'>
<xsl:value-of select='@type'/>
...
<xsl:template match='def'>

you could write

<xsl:template match='def'>
<xsl:variable name='type' select='@type'/>
<xsl:value-of select='document("fruit.xml")/fruit/name[@type=$type]'/>
...
<xsl:template match='def'>

You could have the list of names as part of your stylesheet if that's
more convenient

<mf:fruit>
<mf:name type='apple'>Xapple</mf:name>
<mf:name type='peach'>Xpeach</mf:name>
<mf:name type='orange'>Xorange</mf:name>
<mf:name type='banana'>Xbanana</mf:name>
<mf:name type='plum'>Xplum</mf:name>
</mf:fruit>

<xsl:template match='def'>
<xsl:variable name='type' select='@type'/>
<xsl:value-of select='document("")/mf:fruit/mf:name[@type=$type]'/>
...
<xsl:template match='def'>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top